71 lines
2.5 KiB
JavaScript
71 lines
2.5 KiB
JavaScript
import { mkdirSync } from "node:fs";
|
|
import { spawnSync } from "node:child_process";
|
|
import { dirname, resolve } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
|
const bun = process.env.BUN_BIN || "bun";
|
|
|
|
const TARGET_MAP = {
|
|
"darwin-arm64": { bunTarget: "bun-darwin-arm64", outfile: "dist/bin/hose-use-darwin-arm64" },
|
|
"darwin-x64": { bunTarget: "bun-darwin-x64", outfile: "dist/bin/hose-use-darwin-x64" },
|
|
"linux-x64": { bunTarget: "bun-linux-x64", outfile: "dist/bin/hose-use-linux-x64" },
|
|
"linux-arm64": { bunTarget: "bun-linux-arm64", outfile: "dist/bin/hose-use-linux-arm64" },
|
|
"windows-x64": { bunTarget: "bun-windows-x64", outfile: "dist/bin/hose-use-windows-x64.exe" }
|
|
};
|
|
|
|
const args = process.argv.slice(2);
|
|
const isAll = args.includes("--all") || args.includes("--matrix");
|
|
const targetArg = args.find((arg) => arg.startsWith("--target="))?.replace("--target=", "");
|
|
|
|
if (isAll) {
|
|
for (const [name, config] of Object.entries(TARGET_MAP)) {
|
|
buildTarget(name, config.bunTarget, resolve(root, config.outfile));
|
|
}
|
|
} else if (targetArg) {
|
|
const config = TARGET_MAP[targetArg];
|
|
if (!config) {
|
|
throw new Error(`Unknown target: ${targetArg}. Available targets: ${Object.keys(TARGET_MAP).join(", ")}`);
|
|
}
|
|
buildTarget(targetArg, config.bunTarget, resolve(root, config.outfile));
|
|
} else {
|
|
const output = resolve(root, "dist/bin/hose-use");
|
|
buildTarget("host", null, output);
|
|
}
|
|
|
|
function buildTarget(name, bunTarget, output) {
|
|
mkdirSync(dirname(output), { recursive: true });
|
|
const buildArgs = ["build", "--compile"];
|
|
if (bunTarget) {
|
|
buildArgs.push(`--target=${bunTarget}`);
|
|
}
|
|
buildArgs.push("--outfile", output, "src/cli.ts");
|
|
|
|
console.log(`[build:binary] compiling target: ${name} -> ${output}`);
|
|
run(bun, buildArgs);
|
|
|
|
if (process.platform === "darwin" && (name.startsWith("darwin") || name === "host")) {
|
|
try {
|
|
run("/usr/bin/codesign", ["--remove-signature", output], { quiet: true });
|
|
} catch {
|
|
// ignore
|
|
}
|
|
try {
|
|
run("/usr/bin/codesign", ["--force", "--sign", "-", output], { quiet: true });
|
|
} catch {
|
|
// ignore
|
|
}
|
|
}
|
|
}
|
|
|
|
function run(command, args, options = {}) {
|
|
const result = spawnSync(command, args, {
|
|
cwd: root,
|
|
stdio: options.quiet ? ["ignore", "ignore", "ignore"] : "inherit"
|
|
});
|
|
if (result.error) throw result.error;
|
|
if (result.status !== 0) {
|
|
throw new Error(`${command} ${args.join(" ")} failed with status ${result.status}`);
|
|
}
|
|
}
|