76 lines
2.5 KiB
TypeScript
76 lines
2.5 KiB
TypeScript
import type { ProjectId, ProjectScript, ServerSettings } from "@t3tools/contracts";
|
|
|
|
type ProjectScriptSettings = Pick<
|
|
ServerSettings,
|
|
| "defaultProjectScripts"
|
|
| "projectScriptOverrides"
|
|
| "projectSettingsOverrides"
|
|
| "projectSettingsFolded"
|
|
>;
|
|
|
|
/**
|
|
* The project's override wins, then environment defaults. Until the legacy
|
|
* fields have been folded into `projectSettingsOverrides`, the old map (null
|
|
* there meant "reset to machine defaults") and the aggregate's own scripts
|
|
* still count, so a server that has not run the fold yet behaves as before.
|
|
*/
|
|
export function resolveProjectScripts(
|
|
settings: ProjectScriptSettings,
|
|
project: { id: ProjectId; scripts: readonly ProjectScript[] },
|
|
): readonly ProjectScript[] {
|
|
const override = settings.projectSettingsOverrides[project.id]?.defaultProjectScripts;
|
|
if (override !== undefined) return override;
|
|
if (settings.projectSettingsFolded) return settings.defaultProjectScripts;
|
|
const legacy = settings.projectScriptOverrides[project.id];
|
|
if (legacy === null) return settings.defaultProjectScripts;
|
|
return legacy ?? (project.scripts.length > 0 ? project.scripts : settings.defaultProjectScripts);
|
|
}
|
|
|
|
export function projectScriptsInheritDefaults(
|
|
settings: ProjectScriptSettings,
|
|
project: { id: ProjectId; scripts: readonly ProjectScript[] },
|
|
): boolean {
|
|
if (settings.projectSettingsOverrides[project.id]?.defaultProjectScripts !== undefined) {
|
|
return false;
|
|
}
|
|
if (settings.projectSettingsFolded) return true;
|
|
const legacy = settings.projectScriptOverrides[project.id];
|
|
return legacy === null || (legacy === undefined && project.scripts.length === 0);
|
|
}
|
|
|
|
interface ProjectScriptRuntimeEnvInput {
|
|
project: {
|
|
cwd: string;
|
|
};
|
|
worktreePath?: string | null;
|
|
extraEnv?: Record<string, string>;
|
|
}
|
|
|
|
export function projectScriptCwd(input: {
|
|
project: {
|
|
cwd: string;
|
|
};
|
|
worktreePath?: string | null;
|
|
}): string {
|
|
return input.worktreePath ?? input.project.cwd;
|
|
}
|
|
|
|
export function projectScriptRuntimeEnv(
|
|
input: ProjectScriptRuntimeEnvInput,
|
|
): Record<string, string> {
|
|
const env: Record<string, string> = {
|
|
T3CODE_PROJECT_ROOT: input.project.cwd,
|
|
};
|
|
if (input.worktreePath) {
|
|
env.T3CODE_WORKTREE_PATH = input.worktreePath;
|
|
}
|
|
if (input.extraEnv) {
|
|
return { ...env, ...input.extraEnv };
|
|
}
|
|
return env;
|
|
}
|
|
|
|
export function setupProjectScript(scripts: readonly ProjectScript[]): ProjectScript | null {
|
|
return scripts.find((script) => script.runOnWorktreeCreate) ?? null;
|
|
}
|