Skip to content
Source
Sign inSign up

prepare-server-compile-cache.cjs

67 lines2.8 KB

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