Skip to content

Commit

Permalink
fix() add on exit hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Sep 25, 2019
1 parent 627fe56 commit 80e3758
Showing 1 changed file with 48 additions and 26 deletions.
74 changes: 48 additions & 26 deletions actions/start.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,42 +63,64 @@ export class StartAction extends BuildAction {
isDebugEnabled: boolean,
outDirName: string,
) {
const sourceRoot = getValueOrDefault(configuration, 'sourceRoot', appName);
const entryFile = getValueOrDefault(configuration, 'entryFile', appName);

let childProcessRef: any;
process.on(
'exit',
code => childProcessRef && killProcess(childProcessRef.pid, 'SIGINT'),
code => childProcessRef && killProcess(childProcessRef.pid),
);

return () => {
if (childProcessRef) {
childProcessRef.on('exit', () => {
childProcessRef = this.spawnChildProcess(
entryFile,
sourceRoot,
isDebugEnabled,
outDirName,
);
childProcessRef.on('exit', () => (childProcessRef = undefined));
});

childProcessRef.stdin && childProcessRef.stdin.pause();
killProcess(childProcessRef.pid, 'SIGINT');
killProcess(childProcessRef.pid);
} else {
childProcessRef = this.spawnChildProcess(
entryFile,
sourceRoot,
isDebugEnabled,
outDirName,
);
childProcessRef.on('exit', () => (childProcessRef = undefined));
}
const sourceRoot = getValueOrDefault(
configuration,
'sourceRoot',
appName,
);
const entryFile = getValueOrDefault(configuration, 'entryFile', appName);
};
}

let outputFilePath = join(outDirName, sourceRoot, entryFile);
if (!fs.existsSync(outputFilePath + '.js')) {
outputFilePath = join(outDirName, entryFile);
}
private spawnChildProcess(
entryFile: string,
sourceRoot: string,
isDebugEnabled: boolean,
outDirName: string,
) {
let outputFilePath = join(outDirName, sourceRoot, entryFile);
if (!fs.existsSync(outputFilePath + '.js')) {
outputFilePath = join(outDirName, entryFile);
}

let childProcessArgs: string[] = [];
const argsStartIndex = process.argv.indexOf('--');
if (argsStartIndex >= 0) {
childProcessArgs = process.argv.slice(argsStartIndex + 1);
}
const processArgs = [outputFilePath, ...childProcessArgs];
if (isDebugEnabled) {
processArgs.unshift('--inspect');
}
childProcessRef = spawn('node', processArgs, {
stdio: 'inherit',
shell: true,
});
};
let childProcessArgs: string[] = [];
const argsStartIndex = process.argv.indexOf('--');
if (argsStartIndex >= 0) {
childProcessArgs = process.argv.slice(argsStartIndex + 1);
}
const processArgs = [outputFilePath, ...childProcessArgs];
if (isDebugEnabled) {
processArgs.unshift('--inspect');
}
return spawn('node', processArgs, {
stdio: 'inherit',
shell: true,
});
}
}

0 comments on commit 80e3758

Please sign in to comment.