1// @effect-diagnostics nodeBuiltinImport:off - installs the hook before running project tasks.
2import * as NodeFSP from "node:fs/promises";
3import * as NodeOS from "node:os";
4import * as NodePath from "node:path";
5import * as NodeURL from "node:url";
6
7if (process.env.GITHUB_REPOSITORY !== "pingdotgg/t3code" || process.env.GITHUB_ACTIONS !== "true")
8 throw new Error("This setup is only for the maintainer repository's GitHub Actions.");
9const root = NodePath.resolve(import.meta.dirname, "../../..");
10const hookConfig = NodePath.join(NodeOS.homedir(), ".config/vite-plus/run-hook.json");
11await NodeFSP.mkdir(NodePath.dirname(hookConfig), { recursive: true });
12await NodeFSP.writeFile(
13 hookConfig,
14 JSON.stringify({
15 rootMarker: ".task-cache.json",
16 module: NodePath.join(root, "infra/task-cache/src/runHook.ts"),
17 }),
18);
19const { endpoint } = JSON.parse(
20 await NodeFSP.readFile(NodePath.join(root, ".task-cache.json"), "utf8"),
21);
22const token = process.env.T3_TASK_CACHE_READ_TOKEN;
23if (!token || token.length < 32) throw new Error("Missing task-cache read credential");
24const credentialFile = NodePath.join(NodeOS.homedir(), ".config/t3-task-cache/credentials.json");
25await NodeFSP.mkdir(NodePath.dirname(credentialFile), { recursive: true, mode: 0o700 });
26await NodeFSP.writeFile(
27 credentialFile,
28 JSON.stringify({ [endpoint]: { name: "ci-reader", token } }),
29 { mode: 0o600 },
30);
31const githubEnv = process.env.GITHUB_ENV;
32if (!githubEnv) throw new Error("Missing GitHub Actions environment file");
33const directory = process.env.RUNNER_TEMP;
34if (!directory) throw new Error("Missing GitHub Actions temporary directory");
35const shellSetup = NodePath.join(directory, "t3-task-cache.sh");
36const quote = (value: string) => "'" + value.replaceAll("'", "'\"'\"'") + "'";
37const preload = NodeURL.pathToFileURL(NodePath.join(root, "infra/task-cache/src/preload.mjs")).href;
38// GitHub prohibits NODE_OPTIONS in GITHUB_ENV; bash exports it to task subprocesses instead.
39await NodeFSP.writeFile(
40 shellSetup,
41 [
42 process.env.BASH_ENV && process.env.BASH_ENV !== shellSetup
43 ? `. ${quote(process.env.BASH_ENV)}`
44 : "",
45 `export NODE_OPTIONS="\${NODE_OPTIONS:+$NODE_OPTIONS }"${quote(`--import=${preload}`)}`,
46 "",
47 ].join("\n"),
48);
49await NodeFSP.appendFile(githubEnv, `BASH_ENV=${shellSetup}\n`);