Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

projectScripts.ts

106 lines3.4 KB

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 runOnSettle: boolean;
17 readonly previewUrl: Exclude<ProjectScript["previewUrl"], undefined> | null;
18 readonly autoOpenPreview: boolean;
20​
21export function buildProjectScript(id: string, input: ProjectScriptInput): ProjectScript {
22 return {
23 id,
24 name: input.name,
25 command: input.command,
26 icon: input.icon,
27 runOnWorktreeCreate: input.runOnWorktreeCreate,
28 ...(input.runOnWorktreeCreate && input.waitForSetup ? { async: false } : {}),
29 ...(input.runOnSettle ? { runOnSettle: true } : {}),
30 ...(input.previewUrl === null
31 ? {}
32 : {
33 previewUrl: input.previewUrl,
34 autoOpenPreview: input.autoOpenPreview,
35 }),
36 };
38​
39export function releaseClaimedRoles(
40 script: ProjectScript,
41 saved: ProjectScriptInput,
42): ProjectScript {
43 const releaseSetup = saved.runOnWorktreeCreate && script.runOnWorktreeCreate;
44 const releaseSettle = saved.runOnSettle && script.runOnSettle === true;
45 if (!releaseSetup && !releaseSettle) return script;
46 return {
47 ...script,
48 ...(releaseSetup ? { runOnWorktreeCreate: false } : {}),
49 ...(releaseSettle ? { runOnSettle: false } : {}),
50 };
52​
53function normalizeScriptId(value: string): string {
54 const cleaned = value
55 .trim()
56 .toLowerCase()
57 .replace(/[^a-z0-9]+/g, "-")
58 .replace(/^-+|-+$/g, "");
59 if (cleaned.length === 0) {
60 return "script";
61 }
62 if (cleaned.length <= MAX_SCRIPT_ID_LENGTH) {
63 return cleaned;
64 }
65 return cleaned.slice(0, MAX_SCRIPT_ID_LENGTH).replace(/-+$/g, "") || "script";
67​
68export function commandForProjectScript(scriptId: string): KeybindingCommand | null {
69 const command = `script.${scriptId}.run`;
70 return isScriptRunCommand(command) ? command : null;
72​
73export function projectScriptIdFromCommand(command: string): string | null {
74 const trimmed = command.trim();
75 if (!isScriptRunCommand(trimmed)) {
76 return null;
77 }
78 const [prefix, , suffix] = SCRIPT_RUN_COMMAND_PATTERN.parts;
79 return trimmed.slice(prefix.literal.length, -suffix.literal.length);
81​
82export function nextProjectScriptId(name: string, existingIds: Iterable<string>): string {
83 const taken = new Set(Array.from(existingIds));
84 const baseId = normalizeScriptId(name);
85 if (!taken.has(baseId)) return baseId;
86​
87 let suffix = 2;
88 while (suffix < 10_000) {
89 const candidate = `${baseId}-${suffix}`;
90 const safeCandidate =
91 candidate.length <= MAX_SCRIPT_ID_LENGTH
92 ? candidate
93 : `${baseId.slice(0, Math.max(1, MAX_SCRIPT_ID_LENGTH - String(suffix).length - 1))}-${suffix}`;
94 if (!taken.has(safeCandidate)) {
95 return safeCandidate;
96 }
97 suffix += 1;
98 }
99​
100 return `${baseId}-${Date.now()}`.slice(0, MAX_SCRIPT_ID_LENGTH);
102​
103export function primaryProjectScript(scripts: ReadonlyArray<ProjectScript>): ProjectScript | null {
104 const regular = scripts.find((script) => !script.runOnWorktreeCreate && !script.runOnSettle);
105 return regular ?? scripts.find((script) => !script.runOnSettle) ?? null;