mirror of
https://github.com/VibedByKaKi/t3-code-android-nightly.git
synced 2026-11-26 07:33:26 +01:00
72 lines
3.1 KiB
JavaScript
72 lines
3.1 KiB
JavaScript
/* oxlint-disable t3code/no-global-process-runtime -- Packaging must compare the build host with the executable target. */
|
|
const { spawnSync } = require("node:child_process");
|
|
const path = require("node:path");
|
|
const fs = require("node:fs");
|
|
const os = require("node:os");
|
|
|
|
// afterPack runs before signing and installer creation, against the final ASAR layout.
|
|
module.exports = async ({ appOutDir, electronPlatformName, arch, packager }) => {
|
|
if (!["darwin", "win32", "linux"].includes(electronPlatformName)) {
|
|
console.log(`[server-compile-cache] Skipping unsupported target ${electronPlatformName}`);
|
|
return;
|
|
}
|
|
// Generating the cache runs the bundle, and a PR preview's bundle must never run next to signing secrets.
|
|
if (process.env.T3CODE_DESKTOP_UNTRUSTED_BUNDLE === "true") {
|
|
console.log("[server-compile-cache] Skipping untrusted bundle");
|
|
return;
|
|
}
|
|
const targetArch = ["ia32", "x64", "armv7l", "arm64", "universal"][arch];
|
|
const runsWithRosetta =
|
|
electronPlatformName === "darwin" &&
|
|
process.platform === "darwin" &&
|
|
process.arch === "arm64" &&
|
|
targetArch === "x64";
|
|
if (
|
|
electronPlatformName !== process.platform ||
|
|
(targetArch !== process.arch && !runsWithRosetta)
|
|
) {
|
|
console.log(
|
|
`[server-compile-cache] Skipping unexecutable target ${electronPlatformName}/${targetArch}`,
|
|
);
|
|
return;
|
|
}
|
|
const product = packager.appInfo.productFilename;
|
|
const resources =
|
|
electronPlatformName === "darwin"
|
|
? path.join(appOutDir, `${product}.app`, "Contents", "Resources")
|
|
: path.join(appOutDir, "resources");
|
|
const executable =
|
|
electronPlatformName === "darwin"
|
|
? path.join(appOutDir, `${product}.app`, "Contents", "MacOS", product)
|
|
: path.join(
|
|
appOutDir,
|
|
electronPlatformName === "win32" ? `${product}.exe` : packager.executableName,
|
|
);
|
|
const helper = path.join(
|
|
resources,
|
|
"app.asar",
|
|
"apps/desktop/dist-electron/serverCompileCache.cjs",
|
|
);
|
|
const env = { ...process.env, ELECTRON_RUN_AS_NODE: "1" };
|
|
for (const name of Object.keys(env)) {
|
|
if (name.startsWith("NODE_") || name.startsWith("T3CODE_")) delete env[name];
|
|
}
|
|
const state = fs.mkdtempSync(path.join(os.tmpdir(), "t3-release-cache-"));
|
|
env.T3CODE_HOME = state;
|
|
let result;
|
|
try {
|
|
// Run a script file, not -e: -e defines a global __filename of "[eval]", which
|
|
// @effect/platform-node passes to createRequire and fails on.
|
|
const runner = path.join(state, "prepare-server-compile-cache-runner.cjs");
|
|
fs.writeFileSync(
|
|
runner,
|
|
`require(${JSON.stringify(helper)}).generatePreparedServerCache(${JSON.stringify(resources)}).then(()=>process.exit(0),e=>{console.error(e);process.exit(1)})`,
|
|
);
|
|
result = spawnSync(executable, [runner], { env, encoding: "utf8", timeout: 120000 });
|
|
} finally {
|
|
fs.rmSync(state, { recursive: true, force: true });
|
|
}
|
|
if (result.status !== 0)
|
|
throw new Error(`Server compile cache generation failed: ${result.error || result.stderr}`);
|
|
console.log("[server-compile-cache] Prepared server bytecode with packaged Electron");
|
|
};
|