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

projectScripts.ts

78 lines2.5 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​
23interface ProjectScriptRuntimeEnvInput {
24 project: {
25 cwd: string;
26 };
27 worktreePath?: string | null;
28 extraEnv?: Record<string, string>;
30​
31export function projectScriptCwd(input: {
32 project: {
33 cwd: string;
34 };
35 worktreePath?: string | null;
36}): string {
37 return input.worktreePath ?? input.project.cwd;
39​
40export 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;
54​
55export function setupProjectScript(scripts: readonly ProjectScript[]): ProjectScript | null {
56 return scripts.find((script) => script.runOnWorktreeCreate) ?? null;
58​
59export function projectScriptMenuLabel(script: ProjectScript): string {
60 const roles = [
61 ...(script.runOnWorktreeCreate ? ["setup"] : []),
62 ...(script.runOnSettle ? ["on settle"] : []),
63 ];
64 return roles.length === 0 ? script.name : `${script.name} (${roles.join(", ")})`;
66​
67export function settleProjectScript(scripts: readonly ProjectScript[]): ProjectScript | null {
68 return scripts.find((script) => script.runOnSettle === true) ?? null;
70​
71/**
72 * Leading spaces omit scripts from Bash history with ignorespace/ignoreboth,
73 * zsh with HIST_IGNORE_SPACE, and fish. Only the first line is prefixed, so later
74 * entries from multiline input may still enter history. PowerShell/PSReadLine history is not suppressed.
75 */
76export function projectScriptTerminalInput(command: string): string {
77 return ` ${command}\r`;