HRM-10/HRM.UI/ClientApp/scripts/ng-serve.cjs

39 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2026-11-03 13:06:00 +06:00
// `npm start`: runs `ng serve` with whatever arguments it was given.
//
// HRM.UI's Startup proxies the SPA through `spa.UseAngularCliServer(npmScript: "start")`, which starts
// `npm start -- --port <n>` and waits for the old webpack dev server's "open your browser on <url>" line.
// The Vite dev server of the esbuild builder announces itself as "Local: <url>" instead, so this
// script repeats that address in the line UseAngularCliServer looks for. Without it, F5 / dotnet run
// would time out waiting for Angular even though the dev server is up.
const { spawn } = require('node:child_process');
const ng = require.resolve('@angular/cli/bin/ng.js');
const child = spawn(process.execPath, [ng, 'serve', ...process.argv.slice(2)], {
stdio: ['inherit', 'pipe', 'pipe']
});
let announced = false;
const ansi = /\x1b\[[0-9;]*m/g;
function relay(source, target) {
source.on('data', chunk => {
target.write(chunk);
if (announced) {
return;
}
const match = /Local:\s+(http\S+)/.exec(String(chunk).replace(ansi, ''));
if (match) {
announced = true;
process.stdout.write(`** Angular Live Development Server is listening, open your browser on ${match[1]} **\n`);
}
});
}
relay(child.stdout, process.stdout);
relay(child.stderr, process.stderr);
for (const signal of ['SIGINT', 'SIGTERM', 'SIGBREAK']) {
process.on(signal, () => child.kill(signal));
}
child.on('exit', code => process.exit(code ?? 0));