Refactor development script to use dedicated module
This commit is contained in:
25
scripts/dev.mjs
Normal file
25
scripts/dev.mjs
Normal file
@@ -0,0 +1,25 @@
|
||||
import { spawn } from "node:child_process";
|
||||
|
||||
const commands = [
|
||||
["API", ["run", "dev", "-w", "@mitwirkung/api"]],
|
||||
["WEB", ["run", "dev", "-w", "@mitwirkung/web"]],
|
||||
];
|
||||
|
||||
const children = commands.map(([name, args]) => {
|
||||
const child = spawn("npm", args, { stdio: ["inherit", "pipe", "pipe"] });
|
||||
const forward = (stream, target) => stream.on("data", (data) => {
|
||||
for (const line of data.toString().split("\n")) {
|
||||
if (line) target.write(`[${name}] ${line}\n`);
|
||||
}
|
||||
});
|
||||
forward(child.stdout, process.stdout);
|
||||
forward(child.stderr, process.stderr);
|
||||
return child;
|
||||
});
|
||||
|
||||
const stop = () => children.forEach((child) => child.kill("SIGTERM"));
|
||||
process.on("SIGINT", stop);
|
||||
process.on("SIGTERM", stop);
|
||||
|
||||
const results = await Promise.all(children.map((child) => new Promise((resolve) => child.on("exit", resolve))));
|
||||
process.exit(results.some((code) => code !== 0 && code !== null) ? 1 : 0);
|
||||
Reference in New Issue
Block a user