packages/cli: nicer handling of waiting for bundler to exit

This commit is contained in:
Patrik Oldsberg
2020-05-18 11:29:54 +02:00
parent cefdc422ac
commit a1efd0d76d
4 changed files with 20 additions and 17 deletions
-3
View File
@@ -22,7 +22,4 @@ export default async (cmd: Command) => {
entry: 'src/index',
statsJsonEnabled: cmd.stats,
});
// Wait for interrupt signal
await new Promise(() => {});
};
+2 -3
View File
@@ -18,11 +18,10 @@ import { serveBundle } from '../../lib/bundler';
import { Command } from 'commander';
export default async (cmd: Command) => {
await serveBundle({
const waitForExit = await serveBundle({
entry: 'src/index',
checksEnabled: cmd.check,
});
// Wait for interrupt signal
await new Promise(() => {});
await waitForExit();
};
+2 -3
View File
@@ -18,11 +18,10 @@ import { serveBundle } from '../../lib/bundler';
import { Command } from 'commander';
export default async (cmd: Command) => {
await serveBundle({
const waitForExit = await serveBundle({
entry: 'dev/index',
checksEnabled: cmd.check,
});
// Wait for interrupt signal
await new Promise(() => {});
await waitForExit();
};
+16 -8
View File
@@ -29,7 +29,7 @@ export async function serveBundle(options: ServeOptions) {
const port = await choosePort(host, defaultPort);
if (!port) {
return;
throw new Error(`Invalid or no port set: '${port}'`);
}
const protocol = yn(process.env.HTTPS, { default: false }) ? 'https' : 'http';
@@ -57,15 +57,23 @@ export async function serveBundle(options: ServeOptions) {
return;
}
for (const signal of ['SIGINT', 'SIGTERM'] as const) {
process.on(signal, () => {
server.close();
process.exit();
});
}
openBrowser(urls.localUrlForBrowser);
resolve();
});
});
const waitForExit = async () => {
for (const signal of ['SIGINT', 'SIGTERM'] as const) {
process.on(signal, () => {
server.close();
// exit instead of resolve. The process is shutting down and resolving a promise here logs an error
process.exit();
});
}
// Block indefinitely and wait for the interrupt signal
return new Promise(() => {});
};
return waitForExit;
}