packages/cli: nicer handling of waiting for bundler to exit
This commit is contained in:
@@ -22,7 +22,4 @@ export default async (cmd: Command) => {
|
||||
entry: 'src/index',
|
||||
statsJsonEnabled: cmd.stats,
|
||||
});
|
||||
|
||||
// Wait for interrupt signal
|
||||
await new Promise(() => {});
|
||||
};
|
||||
|
||||
@@ -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();
|
||||
};
|
||||
|
||||
@@ -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();
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user