Files
jsbsim-service/tools/dev.mjs
2026-04-28 17:50:42 +08:00

79 lines
1.8 KiB
JavaScript

import { spawn } from "node:child_process";
import net from "node:net";
const commands = [
{
name: "service",
command: "node",
args: ["service/src/server.js"],
port: 4317
},
{
name: "web",
command: "npm.cmd",
args: ["run", "web:dev"],
port: 5173
}
];
const occupied = [];
for (const command of commands) {
if (await isPortInUse(command.port)) {
occupied.push(command);
}
}
if (occupied.length > 0) {
for (const command of occupied) {
console.error(`[dev] port ${command.port} is already in use; not starting ${command.name}.`);
}
console.error("[dev] stop the old process first, then run npm.cmd run dev again.");
process.exit(1);
}
const children = commands.map(({ name, command, args }) => {
const child = spawn(command, args, {
cwd: process.cwd(),
stdio: ["ignore", "pipe", "pipe"],
shell: false
});
child.stdout.on("data", (data) => writePrefixed(name, data));
child.stderr.on("data", (data) => writePrefixed(name, data));
child.on("exit", (code) => {
if (code !== 0 && code !== null) {
console.error(`[${name}] exited with code ${code}`);
}
});
return child;
});
for (const signal of ["SIGINT", "SIGTERM"]) {
process.on(signal, () => {
for (const child of children) {
child.kill(signal);
}
process.exit(0);
});
}
function writePrefixed(name, data) {
const lines = data.toString().split(/\r?\n/).filter(Boolean);
for (const line of lines) {
console.log(`[${name}] ${line}`);
}
}
function isPortInUse(port) {
return new Promise((resolve) => {
const server = net.createServer();
server.once("error", () => resolve(true));
server.once("listening", () => {
server.close(() => resolve(false));
});
server.listen(port, "127.0.0.1");
});
}