Files
NianAIGC/scripts/run-go-command.mjs
T
2026-10-12 13:57:23 +08:00

37 lines
1.1 KiB
JavaScript

#!/usr/bin/env node
import { spawn, spawnSync } from "node:child_process";
import { fileURLToPath } from "node:url";
const [, , command, ...args] = process.argv;
if (!command) {
console.error("Usage: node scripts/run-go-command.mjs <go-command> [args...]");
process.exit(2);
}
const backendDirectory = fileURLToPath(new URL("../backend/", import.meta.url));
if (command === "build") {
const check = spawnSync(process.execPath, [fileURLToPath(new URL("./sync-go-migrations.mjs", import.meta.url)), "--check"], { stdio: "inherit" });
if (check.error || check.status !== 0) process.exit(check.status ?? 1);
}
const child = spawn("go", [command, ...args], {
cwd: backendDirectory,
env: {
...process.env,
CGO_ENABLED: process.env.CGO_ENABLED || "0",
},
stdio: "inherit",
});
child.on("error", (error) => {
console.error(`[go:${command}] ${error.message}`);
process.exit(1);
});
child.on("exit", (code, signal) => {
if (signal) {
console.error(`[go:${command}] terminated by ${signal}`);
process.exit(1);
}
process.exit(code ?? 1);
});