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