@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
cjs 72 lines 3.06 KB
Raw
1 /* oxlint-disable t3code/no-global-process-runtime -- Packaging must compare the build host with the executable target. */
2 const { spawnSync } = require("node:child_process");
3 const path = require("node:path");
4 const fs = require("node:fs");
5 const os = require("node:os");
6
7 // afterPack runs before signing and installer creation, against the final ASAR layout.
8 module.exports = async ({ appOutDir, electronPlatformName, arch, packager }) => {
9 if (!["darwin", "win32", "linux"].includes(electronPlatformName)) {
10 console.log(`[server-compile-cache] Skipping unsupported target ${electronPlatformName}`);
11 return;
12 }
13 // Generating the cache runs the bundle, and a PR preview's bundle must never run next to signing secrets.
14 if (process.env.T3CODE_DESKTOP_UNTRUSTED_BUNDLE === "true") {
15 console.log("[server-compile-cache] Skipping untrusted bundle");
16 return;
17 }
18 const targetArch = ["ia32", "x64", "armv7l", "arm64", "universal"][arch];
19 const runsWithRosetta =
20 electronPlatformName === "darwin" &&
21 process.platform === "darwin" &&
22 process.arch === "arm64" &&
23 targetArch === "x64";
24 if (
25 electronPlatformName !== process.platform ||
26 (targetArch !== process.arch && !runsWithRosetta)
27 ) {
28 console.log(
29 `[server-compile-cache] Skipping unexecutable target ${electronPlatformName}/${targetArch}`,
30 );
31 return;
32 }
33 const product = packager.appInfo.productFilename;
34 const resources =
35 electronPlatformName === "darwin"
36 ? path.join(appOutDir, `${product}.app`, "Contents", "Resources")
37 : path.join(appOutDir, "resources");
38 const executable =
39 electronPlatformName === "darwin"
40 ? path.join(appOutDir, `${product}.app`, "Contents", "MacOS", product)
41 : path.join(
42 appOutDir,
43 electronPlatformName === "win32" ? `${product}.exe` : packager.executableName,
44 );
45 const helper = path.join(
46 resources,
47 "app.asar",
48 "apps/desktop/dist-electron/serverCompileCache.cjs",
49 );
50 const env = { ...process.env, ELECTRON_RUN_AS_NODE: "1" };
51 for (const name of Object.keys(env)) {
52 if (name.startsWith("NODE_") || name.startsWith("T3CODE_")) delete env[name];
53 }
54 const state = fs.mkdtempSync(path.join(os.tmpdir(), "t3-release-cache-"));
55 env.T3CODE_HOME = state;
56 let result;
57 try {
58 // Run a script file, not -e: -e defines a global __filename of "[eval]", which
59 // @effect/platform-node passes to createRequire and fails on.
60 const runner = path.join(state, "prepare-server-compile-cache-runner.cjs");
61 fs.writeFileSync(
62 runner,
63 `require(${JSON.stringify(helper)}).generatePreparedServerCache(${JSON.stringify(resources)}).then(()=>process.exit(0),e=>{console.error(e);process.exit(1)})`,
64 );
65 result = spawnSync(executable, [runner], { env, encoding: "utf8", timeout: 120000 });
66 } finally {
67 fs.rmSync(state, { recursive: true, force: true });
68 }
69 if (result.status !== 0)
70 throw new Error(`Server compile cache generation failed: ${result.error || result.stderr}`);
71 console.log("[server-compile-cache] Prepared server bytecode with packaged Electron");
72 };