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

projectScripts.ts

57 lines1.8 KB

1import type { ProjectId, ProjectScript, ServerSettings } from "@t3tools/contracts";
2​
3/** Missing entries preserve existing actions; null explicitly resets a checkout to machine defaults. */
4export function resolveProjectScripts(
5 settings: Pick<ServerSettings, "defaultProjectScripts" | "projectScriptOverrides">,
6 project: { id: ProjectId; scripts: readonly ProjectScript[] },
7): readonly ProjectScript[] {
8 const override = settings.projectScriptOverrides[project.id];
9 if (override === null) return settings.defaultProjectScripts;
10 return (
11 override ?? (project.scripts.length > 0 ? project.scripts : settings.defaultProjectScripts)
12 );
14​
15export function projectScriptsInheritDefaults(
16 settings: Pick<ServerSettings, "projectScriptOverrides">,
17 project: { id: ProjectId; scripts: readonly ProjectScript[] },
18): boolean {
19 const override = settings.projectScriptOverrides[project.id];
20 return override === null || (override === undefined && project.scripts.length === 0);
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;