Open History to see the last commit
1import {
2 MAX_SCRIPT_ID_LENGTH,
3 SCRIPT_RUN_COMMAND_PATTERN,
4 type KeybindingCommand,
5 type ProjectScript,
6} from "@t3tools/contracts";
7import * as Schema from "effect/Schema";
8const isScriptRunCommand = Schema.is(SCRIPT_RUN_COMMAND_PATTERN);
9
10export interface ProjectScriptInput {
11 readonly name: ProjectScript["name"];
12 readonly command: ProjectScript["command"];
13 readonly icon: ProjectScript["icon"];
14 readonly runOnWorktreeCreate: ProjectScript["runOnWorktreeCreate"];
15 readonly waitForSetup: boolean;
16 readonly previewUrl: Exclude<ProjectScript["previewUrl"], undefined> | null;
17 readonly autoOpenPreview: boolean;
18}
19
20export function buildProjectScript(id: string, input: ProjectScriptInput): ProjectScript {
21 return {
22 id,
23 name: input.name,
24 command: input.command,
25 icon: input.icon,
26 runOnWorktreeCreate: input.runOnWorktreeCreate,
27 ...(input.runOnWorktreeCreate && input.waitForSetup ? { async: false } : {}),
28 ...(input.previewUrl === null
29 ? {}
30 : {
31 previewUrl: input.previewUrl,
32 autoOpenPreview: input.autoOpenPreview,
33 }),
34 };
35}
36
37function normalizeScriptId(value: string): string {
38 const cleaned = value
39 .trim()
40 .toLowerCase()
41 .replace(/[^a-z0-9]+/g, "-")
42 .replace(/^-+|-+$/g, "");
43 if (cleaned.length === 0) {
44 return "script";
45 }
46 if (cleaned.length <= MAX_SCRIPT_ID_LENGTH) {
47 return cleaned;
48 }
49 return cleaned.slice(0, MAX_SCRIPT_ID_LENGTH).replace(/-+$/g, "") || "script";
50}
51
52/** Legacy script IDs may not support shortcuts; keep those scripts usable without one. */
53export function commandForProjectScript(scriptId: string): KeybindingCommand | null {
54 const command = `script.${scriptId}.run`;
55 return isScriptRunCommand(command) ? command : null;
56}
57
58export function projectScriptIdFromCommand(command: string): string | null {
59 const trimmed = command.trim();
60 if (!isScriptRunCommand(trimmed)) {
61 return null;
62 }
63 const [prefix, , suffix] = SCRIPT_RUN_COMMAND_PATTERN.parts;
64 return trimmed.slice(prefix.literal.length, -suffix.literal.length);
65}
66
67export function nextProjectScriptId(name: string, existingIds: Iterable<string>): string {
68 const taken = new Set(Array.from(existingIds));
69 const baseId = normalizeScriptId(name);
70 if (!taken.has(baseId)) return baseId;
71
72 let suffix = 2;
73 while (suffix < 10_000) {
74 const candidate = `${baseId}-${suffix}`;
75 const safeCandidate =
76 candidate.length <= MAX_SCRIPT_ID_LENGTH
77 ? candidate
78 : `${baseId.slice(0, Math.max(1, MAX_SCRIPT_ID_LENGTH - String(suffix).length - 1))}-${suffix}`;
79 if (!taken.has(safeCandidate)) {
80 return safeCandidate;
81 }
82 suffix += 1;
83 }
84
85 // This last-resort fallback only triggers after exhausting thousands of suffixes.
86 return `${baseId}-${Date.now()}`.slice(0, MAX_SCRIPT_ID_LENGTH);
87}
88
89export function primaryProjectScript(scripts: ReadonlyArray<ProjectScript>): ProjectScript | null {
90 const regular = scripts.find((script) => !script.runOnWorktreeCreate);
91 return regular ?? scripts[0] ?? null;
92}