Skip to content

Commit

Permalink
fix(child-process): pressing ctrl-c kills all child processes
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Mollweide committed May 27, 2018
1 parent 5612bec commit 0e34a80
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/commandListener/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const path = require('path');
const keypress = require('keypress');
const chalk = require('chalk');
const { getUiState } = require('../store');
const { getUiState, getState } = require('../store');
const getLernaPackages = require('../getLernaPackages');
const { getProgram } = require('../commander');
const getPackage = require('../getPackage');
Expand All @@ -16,6 +16,11 @@ let currentSelectedHistory = 0;

const onCtrlC = () => {
buffer = '';
const state = getState();
Object.keys(state).forEach(terminalId => {
// stop all child processes
state[terminalId].terminal.stop();
});
process.exit();
};

Expand Down
10 changes: 9 additions & 1 deletion src/commandListener/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* global jest, afterEach */
/* eslint global-require: 0*/
const commandListener = require('./index');
const { getUiState } = require('../store');
const { getUiState, getState } = require('../store');
const getLernaPackages = require('../getLernaPackages');
const { getProgram } = require('../commander');
const getPackage = require('../getPackage');
Expand Down Expand Up @@ -133,6 +133,14 @@ describe('commandListener', () => {
getUiState.mockImplementation(() => ({
onChange() {},
}));
getState.mockImplementation(() => ({
packageA: {
terminal: {
stop() {}
},
log: []
}
}));
global.process = Object.assign(_process, {
stdin: Object.assign(_process.stdin, {
on: (value, cb) => {
Expand Down
8 changes: 5 additions & 3 deletions src/runNpmScript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const spawn = require('cross-spawn');
* @param {Function} options.onExit - callback which will be fired if childProcess was killed
* @param {Function} options.onRecieve - callback which will be fired if childProcess recieved an message
* @param {Function} options.onError - callback which will be fired if childProcess recieved an error message
* @param {Function} options.onError - callback which will be fired if childProcess recieved an error message
* @returns {Object} returns an object including an start and stop method
**/
function runNpmScript({ scriptName, packagePath, onExit = () => {}, onRecieve = () => {}, onError = () => {} }) {
Expand All @@ -39,9 +38,12 @@ function runNpmScript({ scriptName, packagePath, onExit = () => {}, onRecieve =
return Object.assign(childProcessObj, {
stop() {
childProcessObj.isRunning = false;
/* istanbul ignore next */
try {
process.kill(-childProcess.pid);
} catch (err) {}
process.kill(childProcess.pid, 'SIGINT');
} catch (err) {
console.error(`process.kill(SIGINT): ${err}`);
}
},
start() {
this.stop();
Expand Down

0 comments on commit 0e34a80

Please sign in to comment.