Files
marathon-todo/scripts/dev-all.mjs
2026-03-26 11:33:03 +01:00

63 lines
1.3 KiB
JavaScript

import { spawn } from 'node:child_process';
function spawnNpmScript(scriptName) {
const npmExecPath = process.env.npm_execpath;
if (npmExecPath) {
return spawn(process.execPath, [npmExecPath, 'run', scriptName], {
stdio: 'inherit',
env: process.env,
});
}
const npmCommand = process.platform === 'win32' ? 'npm.cmd' : 'npm';
return spawn(npmCommand, ['run', scriptName], {
stdio: 'inherit',
env: process.env,
shell: process.platform === 'win32',
});
}
const backend = spawnNpmScript('dev:backend');
const frontend = spawnNpmScript('dev:frontend');
let shuttingDown = false;
function shutdown(code = 0) {
if (shuttingDown) {
return;
}
shuttingDown = true;
if (backend && !backend.killed) {
backend.kill('SIGTERM');
}
if (frontend && !frontend.killed) {
frontend.kill('SIGTERM');
}
process.exit(code);
}
backend.on('exit', (code) => {
shutdown(code ?? 0);
});
frontend.on('exit', (code) => {
shutdown(code ?? 0);
});
backend.on('error', (error) => {
console.error('[dev:all] backend failed:', error);
shutdown(1);
});
frontend.on('error', (error) => {
console.error('[dev:all] frontend failed:', error);
shutdown(1);
});
process.on('SIGINT', () => shutdown(0));
process.on('SIGTERM', () => shutdown(0));