Open History to see the last commit
1import {
2 MAX_SCRIPT_ID_LENGTH,
3 SCRIPT_RUN_COMMAND_PATTERN,
4 type KeybindingCommand,
5 type ProjectId,
6 type ProjectScript,
7 type ServerSettings,
8} from "@t3tools/contracts";
9import { resolveProjectScripts } from "@t3tools/shared/projectScripts";
10import * as Schema from "effect/Schema";
11const isScriptRunCommand = Schema.is(SCRIPT_RUN_COMMAND_PATTERN);
12
13export interface ProjectScriptInput {
14 readonly name: ProjectScript["name"];
15 readonly command: ProjectScript["command"];
16 readonly icon: ProjectScript["icon"];
17 readonly runOnWorktreeCreate: ProjectScript["runOnWorktreeCreate"];
18 readonly waitForSetup: boolean;
19 readonly runOnSettle: boolean;
20 readonly stopOnSettle?: boolean;
21 readonly previewUrl: Exclude<ProjectScript["previewUrl"], undefined> | null;
22 readonly autoOpenPreview: boolean;
23}
24
25export function buildProjectScript(id: string, input: ProjectScriptInput): ProjectScript {
26 return {
27 id,
28 name: input.name,
29 command: input.command,
30 icon: input.icon,
31 runOnWorktreeCreate: input.runOnWorktreeCreate,
32 ...(input.runOnWorktreeCreate && input.waitForSetup ? { async: false } : {}),
33 ...(input.runOnSettle ? { runOnSettle: true } : {}),
34 ...(input.stopOnSettle ? { stopOnSettle: true } : {}),
35 ...(input.previewUrl === null
36 ? {}
37 : {
38 previewUrl: input.previewUrl,
39 autoOpenPreview: input.autoOpenPreview,
40 }),
41 };
42}
43
44export function releaseClaimedRoles(
45 script: ProjectScript,
46 saved: ProjectScriptInput,
47): ProjectScript {
48 const releaseSetup = saved.runOnWorktreeCreate && script.runOnWorktreeCreate;
49 const releaseSettle = saved.runOnSettle && script.runOnSettle === true;
50 if (!releaseSetup && !releaseSettle) return script;
51 return {
52 ...script,
53 ...(releaseSetup ? { runOnWorktreeCreate: false } : {}),
54 ...(releaseSettle ? { runOnSettle: false } : {}),
55 };
56}
57
58function normalizeScriptId(value: string): string {
59 const cleaned = value
60 .trim()
61 .toLowerCase()
62 .replace(/[^a-z0-9]+/g, "-")
63 .replace(/^-+|-+$/g, "");
64 if (cleaned.length === 0) {
65 return "script";
66 }
67 if (cleaned.length <= MAX_SCRIPT_ID_LENGTH) {
68 return cleaned;
69 }
70 return cleaned.slice(0, MAX_SCRIPT_ID_LENGTH).replace(/-+$/g, "") || "script";
71}
72
73export function commandForProjectScript(scriptId: string): KeybindingCommand | null {
74 const command = `script.${scriptId}.run`;
75 return isScriptRunCommand(command) ? command : null;
76}
77
78export function projectScriptIdFromCommand(command: string): string | null {
79 const trimmed = command.trim();
80 if (!isScriptRunCommand(trimmed)) {
81 return null;
82 }
83 const [prefix, , suffix] = SCRIPT_RUN_COMMAND_PATTERN.parts;
84 return trimmed.slice(prefix.literal.length, -suffix.literal.length);
85}
86
87/** Shortcuts are keyed by script id per environment, so new ids must avoid every project's. */
88export function projectScriptIdsInEnvironment(
89 settings: ServerSettings,
90 environmentProjects: ReadonlyArray<{ id: ProjectId; scripts: readonly ProjectScript[] }>,
91): string[] {
92 return [
93 ...settings.defaultProjectScripts,
94 ...Object.values(settings.projectSettingsOverrides).flatMap(
95 (entry) => entry.defaultProjectScripts ?? [],
96 ),
97 ...environmentProjects.flatMap((project) => resolveProjectScripts(settings, project)),
98 ].map((script) => script.id);
99}
100
101export function nextProjectScriptId(name: string, existingIds: Iterable<string>): string {
102 const taken = new Set(Array.from(existingIds));
103 const baseId = normalizeScriptId(name);
104 if (!taken.has(baseId)) return baseId;
105
106 let suffix = 2;
107 while (suffix < 10_000) {
108 const candidate = `${baseId}-${suffix}`;
109 const safeCandidate =
110 candidate.length <= MAX_SCRIPT_ID_LENGTH
111 ? candidate
112 : `${baseId.slice(0, Math.max(1, MAX_SCRIPT_ID_LENGTH - String(suffix).length - 1))}-${suffix}`;
113 if (!taken.has(safeCandidate)) {
114 return safeCandidate;
115 }
116 suffix += 1;
117 }
118
119 return `${baseId}-${Date.now()}`.slice(0, MAX_SCRIPT_ID_LENGTH);
120}
121
122export function primaryProjectScript(scripts: ReadonlyArray<ProjectScript>): ProjectScript | null {
123 const regular = scripts.find((script) => !script.runOnWorktreeCreate && !script.runOnSettle);
124 return regular ?? scripts.find((script) => !script.runOnSettle) ?? null;
125}