Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

prepare-server-compile-cache.cjs

60 lines2.5 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 // 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 if (electronPlatformName !== process.platform || targetArch !== process.arch) {
16 console.log(
17 `[server-compile-cache] Skipping unexecutable target ${electronPlatformName}/${targetArch}`,
18 );
19 return;
20 }
21 const product = packager.appInfo.productFilename;
22 const resources =
23 electronPlatformName === "darwin"
24 ? path.join(appOutDir, `${product}.app`, "Contents", "Resources")
25 : path.join(appOutDir, "resources");
26 const executable =
27 electronPlatformName === "darwin"
28 ? path.join(appOutDir, `${product}.app`, "Contents", "MacOS", product)
29 : path.join(
30 appOutDir,
31 electronPlatformName === "win32" ? `${product}.exe` : packager.executableName,
32 );
33 const helper = path.join(
34 resources,
35 "app.asar",
36 "apps/desktop/dist-electron/serverCompileCache.cjs",
37 );
38 const env = { ...process.env, ELECTRON_RUN_AS_NODE: "1" };
39 for (const name of Object.keys(env)) {
40 if (name.startsWith("NODE_") || name.startsWith("T3CODE_")) delete env[name];
41 }
42 const state = fs.mkdtempSync(path.join(os.tmpdir(), "t3-release-cache-"));
43 env.T3CODE_HOME = state;
44 let result;
45 try {
46 result = spawnSync(
47 executable,
48 [
49 "-e",
50 `require(${JSON.stringify(helper)}).generatePreparedServerCache(${JSON.stringify(resources)}).then(()=>process.exit(0),e=>{console.error(e);process.exit(1)})`,
51 ],
52 { env, encoding: "utf8", timeout: 120000 },
53 );
54 } finally {
55 fs.rmSync(state, { recursive: true, force: true });
56 }
57 if (result.status !== 0)
58 throw new Error(`Server compile cache generation failed: ${result.error || result.stderr}`);
59 console.log("[server-compile-cache] Prepared server bytecode with packaged Electron");
60};