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

projectScripts.ts

90 lines3.0 KB

1import type { ProjectId, ProjectScript, ServerSettings } from "@t3tools/contracts";
2​
3type ProjectScriptSettings = Pick<
4 ServerSettings,
5 | "defaultProjectScripts"
6 | "projectScriptOverrides"
7 | "projectSettingsOverrides"
8 | "projectSettingsFolded"
9>;
10​
11export function resolveProjectScripts(
12 settings: ProjectScriptSettings,
13 project: { id: ProjectId; scripts: readonly ProjectScript[] },
14): readonly ProjectScript[] {
15 const override = settings.projectSettingsOverrides[project.id]?.defaultProjectScripts;
16 if (override !== undefined) return override;
17 if (settings.projectSettingsFolded) return settings.defaultProjectScripts;
18 const legacy = settings.projectScriptOverrides[project.id];
19 if (legacy === null) return settings.defaultProjectScripts;
20 return legacy ?? (project.scripts.length > 0 ? project.scripts : settings.defaultProjectScripts);
22​
23export function projectScriptsInheritDefaults(
24 settings: ProjectScriptSettings,
25 project: { id: ProjectId; scripts: readonly ProjectScript[] },
26): boolean {
27 if (settings.projectSettingsOverrides[project.id]?.defaultProjectScripts !== undefined) {
28 return false;
29 }
30 if (settings.projectSettingsFolded) return true;
31 const legacy = settings.projectScriptOverrides[project.id];
32 return legacy === null || (legacy === undefined && project.scripts.length === 0);
34​
35interface ProjectScriptRuntimeEnvInput {
36 project: {
37 cwd: string;
38 };
39 worktreePath?: string | null;
40 extraEnv?: Record<string, string>;
42​
43export function projectScriptCwd(input: {
44 project: {
45 cwd: string;
46 };
47 worktreePath?: string | null;
48}): string {
49 return input.worktreePath ?? input.project.cwd;
51​
52export function projectScriptRuntimeEnv(
53 input: ProjectScriptRuntimeEnvInput,
54): Record<string, string> {
55 const env: Record<string, string> = {
56 T3CODE_PROJECT_ROOT: input.project.cwd,
57 };
58 if (input.worktreePath) {
59 env.T3CODE_WORKTREE_PATH = input.worktreePath;
60 }
61 if (input.extraEnv) {
62 return { ...env, ...input.extraEnv };
63 }
64 return env;
66​
67export function setupProjectScript(scripts: readonly ProjectScript[]): ProjectScript | null {
68 return scripts.find((script) => script.runOnWorktreeCreate) ?? null;
70​
71export function projectScriptMenuLabel(script: ProjectScript): string {
72 const roles = [
73 ...(script.runOnWorktreeCreate ? ["setup"] : []),
74 ...(script.runOnSettle ? ["on settle"] : []),
75 ];
76 return roles.length === 0 ? script.name : `${script.name} (${roles.join(", ")})`;
78​
79export function settleProjectScript(scripts: readonly ProjectScript[]): ProjectScript | null {
80 return scripts.find((script) => script.runOnSettle === true) ?? null;
82​
83/**
84 * Leading spaces omit scripts from Bash history with ignorespace/ignoreboth,
85 * zsh with HIST_IGNORE_SPACE, and fish. Only the first line is prefixed, so later
86 * entries from multiline input may still enter history. PowerShell/PSReadLine history is not suppressed.
87 */
88export function projectScriptTerminalInput(command: string): string {
89 return ` ${command}\r`;