The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { ProjectId, ProjectScript, ServerSettings } from "@t3tools/contracts"; |
| 2 | |
| 3 | type ProjectScriptSettings = Pick< |
| 4 | ServerSettings, |
| 5 | | "defaultProjectScripts" |
| 6 | | "projectScriptOverrides" |
| 7 | | "projectSettingsOverrides" |
| 8 | | "projectSettingsFolded" |
| 9 | >; |
| 10 | |
| 11 | export 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); |
| 21 | } |
| 22 | |
| 23 | interface ProjectScriptRuntimeEnvInput { |
| 24 | project: { |
| 25 | cwd: string; |
| 26 | }; |
| 27 | worktreePath?: string | null; |
| 28 | extraEnv?: Record<string, string>; |
| 29 | } |
| 30 | |
| 31 | export function projectScriptCwd(input: { |
| 32 | project: { |
| 33 | cwd: string; |
| 34 | }; |
| 35 | worktreePath?: string | null; |
| 36 | }): string { |
| 37 | return input.worktreePath ?? input.project.cwd; |
| 38 | } |
| 39 | |
| 40 | export function projectScriptRuntimeEnv( |
| 41 | input: ProjectScriptRuntimeEnvInput, |
| 42 | ): Record<string, string> { |
| 43 | const env: Record<string, string> = { |
| 44 | T3CODE_PROJECT_ROOT: input.project.cwd, |
| 45 | }; |
| 46 | if (input.worktreePath) { |
| 47 | env.T3CODE_WORKTREE_PATH = input.worktreePath; |
| 48 | } |
| 49 | if (input.extraEnv) { |
| 50 | return { ...env, ...input.extraEnv }; |
| 51 | } |
| 52 | return env; |
| 53 | } |
| 54 | |
| 55 | export function setupProjectScript(scripts: readonly ProjectScript[]): ProjectScript | null { |
| 56 | return scripts.find((script) => script.runOnWorktreeCreate) ?? null; |
| 57 | } |
| 58 | |
| 59 | export function settleProjectScript(scripts: readonly ProjectScript[]): ProjectScript | null { |
| 60 | return scripts.find((script) => script.runOnSettle === true) ?? null; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Leading spaces omit scripts from Bash history with ignorespace/ignoreboth, |
| 65 | * zsh with HIST_IGNORE_SPACE, and fish. Only the first line is prefixed, so later |
| 66 | * entries from multiline input may still enter history. PowerShell/PSReadLine history is not suppressed. |
| 67 | */ |
| 68 | export function projectScriptTerminalInput(command: string): string { |
| 69 | return ` ${command}\r`; |
| 70 | } |