The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | // @effect-diagnostics nodeBuiltinImport:off - installs the hook before running project tasks. |
| 2 | import * as NodeFSP from "node:fs/promises"; |
| 3 | import * as NodeOS from "node:os"; |
| 4 | import * as NodePath from "node:path"; |
| 5 | import * as NodeURL from "node:url"; |
| 6 | |
| 7 | if (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."); |
| 9 | const root = NodePath.resolve(import.meta.dirname, "../../.."); |
| 10 | const hookConfig = NodePath.join(NodeOS.homedir(), ".config/vite-plus/run-hook.json"); |
| 11 | await NodeFSP.mkdir(NodePath.dirname(hookConfig), { recursive: true }); |
| 12 | await 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 | ); |
| 19 | const { endpoint } = JSON.parse( |
| 20 | await NodeFSP.readFile(NodePath.join(root, ".task-cache.json"), "utf8"), |
| 21 | ); |
| 22 | const token = process.env.T3_TASK_CACHE_READ_TOKEN; |
| 23 | if (!token || token.length < 32) throw new Error("Missing task-cache read credential"); |
| 24 | const credentialFile = NodePath.join(NodeOS.homedir(), ".config/t3-task-cache/credentials.json"); |
| 25 | await NodeFSP.mkdir(NodePath.dirname(credentialFile), { recursive: true, mode: 0o700 }); |
| 26 | await NodeFSP.writeFile( |
| 27 | credentialFile, |
| 28 | JSON.stringify({ [endpoint]: { name: "ci-reader", token } }), |
| 29 | { mode: 0o600 }, |
| 30 | ); |
| 31 | const githubEnv = process.env.GITHUB_ENV; |
| 32 | if (!githubEnv) throw new Error("Missing GitHub Actions environment file"); |
| 33 | const directory = process.env.RUNNER_TEMP; |
| 34 | if (!directory) throw new Error("Missing GitHub Actions temporary directory"); |
| 35 | const shellSetup = NodePath.join(directory, "t3-task-cache.sh"); |
| 36 | const quote = (value: string) => "'" + value.replaceAll("'", "'\"'\"'") + "'"; |
| 37 | const 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. |
| 39 | await 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 | ); |
| 49 | await NodeFSP.appendFile(githubEnv, `BASH_ENV=${shellSetup}\n`); |