Files
modelstudioai__cli/tools/release/lib/binary-compile.mjs
T

66 lines
1.9 KiB
JavaScript
Raw Normal View History

2026-07-23 17:34:30 +08:00
/**
2026-07-23 18:12:42 +08:00
* Single-target Bun compile helper. Must be run with Bun on PATH:
2026-07-23 17:34:30 +08:00
* bun tools/release/lib/binary-compile.mjs --entry <path> --outfile <path> --target <bun-target>
*
* Uses `bun build --compile` (CLI). The Bun.build({ compile }) API on ≤1.2.19
* can exit 0 without writing outfile; CI pins 1.2.19 so we stay on the CLI.
2026-07-23 18:12:42 +08:00
*
2026-07-23 17:34:30 +08:00
* Called by binary-build.mjs (Node orchestration stays on Node).
*/
2026-07-23 18:12:42 +08:00
import { existsSync } from "node:fs";
import { spawnSync } from "node:child_process";
2026-07-23 17:34:30 +08:00
function parseArgs(argv) {
let entry = null;
let outfile = null;
let target = null;
for (let index = 0; index < argv.length; index++) {
const arg = argv[index];
if (arg === "--entry") entry = argv[++index];
else if (arg === "--outfile") outfile = argv[++index];
else if (arg === "--target") target = argv[++index];
else if (arg === "--help" || arg === "-h") {
process.stdout.write(
"Usage: bun tools/release/lib/binary-compile.mjs --entry <path> --outfile <path> --target <bun-target>\n",
);
process.exit(0);
} else {
throw new Error(`Unknown argument: ${arg}`);
}
}
if (!entry || !outfile || !target) {
throw new Error("Required: --entry, --outfile, --target");
}
return { entry, outfile, target };
}
const { entry, outfile, target } = parseArgs(process.argv.slice(2));
2026-07-23 18:12:42 +08:00
const result = spawnSync(
"bun",
[
"build",
entry,
"--compile",
"--outfile",
2026-07-23 17:34:30 +08:00
outfile,
2026-07-23 18:12:42 +08:00
"--target",
target,
"--define",
'process.env.BAILIAN_COMPILED="1"',
],
{ encoding: "utf-8", stdio: ["ignore", "pipe", "pipe"] },
);
2026-07-23 17:34:30 +08:00
2026-07-23 18:12:42 +08:00
if (result.stdout) process.stdout.write(result.stdout);
if (result.stderr) process.stderr.write(result.stderr);
if (result.status !== 0) {
process.exit(result.status ?? 1);
}
if (!existsSync(outfile)) {
console.error(`bun build --compile exited 0 but outfile missing: ${outfile}`);
2026-07-23 17:34:30 +08:00
process.exit(1);
}