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

setup-worktree.ts

56 lines2.4 KB

1// @effect-diagnostics nodeBuiltinImport:off - runs before `vp i`, so only Node built-ins exist.
2/**
3 * Worktree setup, run by the t3.json "Setup Worktree" action as
4 * `node scripts/setup-worktree.ts`. Plain Node keeps one command working in
5 * every shell T3 Code spawns (zsh, bash, fish, PowerShell): it installs
6 * dependencies, links the main checkout's gitignored env files into this
7 * worktree, then warms the web dependency cache.
8 */
9import * as NodeChildProcess from "node:child_process";
10import * as NodeFS from "node:fs";
11import * as NodePath from "node:path";
12​
13const ENV_FILES = [".env", NodePath.join("infra", "relay", ".env")];
14​
15const projectRoot = process.env.T3CODE_PROJECT_ROOT;
16if (!projectRoot) {
17 throw new Error("T3CODE_PROJECT_ROOT is not set. Run this through the t3.json setup action.");
19const worktree = NodePath.dirname(import.meta.dirname);
20​
21// `shell` resolves `vp` through PATH, including Windows command shims.
22// Setup has no one answering install prompts. Pass pnpm's confirmation flag
23// through vp and leave stdin closed so other prompts fail instead of hanging.
24// Install exactly the locked dependencies so setup never rewrites the lockfile.
25// A stale lockfile must fail setup rather than silently resolving a new one.
26const install = NodeChildProcess.spawnSync("vp i --frozen-lockfile -- --yes", {
27 cwd: worktree,
28 shell: true,
29 stdio: ["ignore", "inherit", "inherit"],
30});
31if (install.status !== 0) process.exit(install.status ?? 1);
32​
33// Env files live as real files in the main checkout; worktrees only get
34// symlinks to them. Only a symlink is ever replaced, so a real env file is
35// never deleted, including when this runs in the main checkout itself.
36for (const file of ENV_FILES) {
37 const source = NodePath.join(projectRoot, file);
38 const sourceStat = NodeFS.lstatSync(source, { throwIfNoEntry: false });
39 if (!sourceStat) continue;
40 if (!sourceStat.isFile()) {
41 process.stderr.write(`Skipping ${file}: ${source} is not a regular file.\n`);
42 continue;
43 }
44 const target = NodePath.join(worktree, file);
45 const existing = NodeFS.lstatSync(target, { throwIfNoEntry: false });
46 if (existing && !existing.isSymbolicLink()) continue;
47 if (existing) NodeFS.rmSync(target);
48 NodeFS.symlinkSync(source, target);
50​
51const warm = NodeChildProcess.spawnSync(
52 process.execPath,
53 [NodePath.join(worktree, "apps", "web", "scripts", "warm-dep-cache.ts")],
54 { cwd: worktree, stdio: "inherit" },
55);
56process.exit(warm.status ?? 1);