From 0e34a806233e9db8cdb80655dab5767d80c4ca45 Mon Sep 17 00:00:00 2001 From: Simon Mollweide Date: Sun, 27 May 2018 14:12:25 +0200 Subject: [PATCH] fix(child-process): pressing ctrl-c kills all child processes --- src/commandListener/index.js | 7 ++++++- src/commandListener/index.spec.js | 10 +++++++++- src/runNpmScript/index.js | 8 +++++--- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/commandListener/index.js b/src/commandListener/index.js index ceb000c..7812af9 100644 --- a/src/commandListener/index.js +++ b/src/commandListener/index.js @@ -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'); @@ -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(); }; diff --git a/src/commandListener/index.spec.js b/src/commandListener/index.spec.js index 9edab4e..52f64bd 100644 --- a/src/commandListener/index.spec.js +++ b/src/commandListener/index.spec.js @@ -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'); @@ -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) => { diff --git a/src/runNpmScript/index.js b/src/runNpmScript/index.js index 3071e24..d349710 100644 --- a/src/runNpmScript/index.js +++ b/src/runNpmScript/index.js @@ -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 = () => {} }) { @@ -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();