Skip to content

Commit

Permalink
fix(watch): close all processes on sigint
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Jul 22, 2020
1 parent fb061d8 commit 2f923e0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/cli/task-serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export const taskServe = async (config: Config) => {
console.log(``);

config.sys.onProcessInterrupt(() => {
devServer && devServer.close();
if (devServer) {
config.logger.debug(`dev server close: ${devServer.browserUrl}`);
devServer.close();
}
});
};
3 changes: 2 additions & 1 deletion src/cli/task-watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export const taskWatch = async (coreCompiler: CoreCompiler, config: Config) => {
}

config.sys.onProcessInterrupt(() => {
compiler.destroy();
config.logger.debug(`close watch`);
compiler && compiler.destroy();
});

const rmVersionCheckerLog = watcher.on('buildFinish', async () => {
Expand Down
21 changes: 18 additions & 3 deletions src/sys/node/node-sys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import fs from 'graceful-fs';
import { NodeLazyRequire } from './node-lazy-require';
import { NodeResolveModule } from './node-resolve-module';
import { NodeWorkerController } from './node-worker-controller';
import { normalizePath, requireFunc, buildError } from '@utils';
import { normalizePath, requireFunc, buildError, isFunction } from '@utils';
import path from 'path';
import type TypeScript from 'typescript';

Expand Down Expand Up @@ -249,8 +249,10 @@ export function createNodeSysNoWatch(c: { process?: any } = {}) {
},
nextTick: prcs.nextTick,
normalizePath,
onProcessInterrupt(cb) {
onInterruptsCallbacks.push(cb);
onProcessInterrupt: cb => {
if (!onInterruptsCallbacks.includes(cb)) {
onInterruptsCallbacks.push(cb);
}
},
platformPath: path,
readdir(p) {
Expand Down Expand Up @@ -446,5 +448,18 @@ export function createNodeSysNoWatch(c: { process?: any } = {}) {
'workbox-build': ['4.3.1', '4.3.1'],
});

prcs.on('SIGINT', () => {
while (true) {
const cb = onInterruptsCallbacks.pop();
if (isFunction(cb)) {
try {
cb();
} catch (e) {}
} else {
break;
}
}
});

return sys;
}

0 comments on commit 2f923e0

Please sign in to comment.