From b3ca562c605b5ef7f0ee2afecf10376eeb5fba8d Mon Sep 17 00:00:00 2001 From: Guillaume Chau Date: Tue, 22 Oct 2019 02:33:07 +0200 Subject: [PATCH] Revert "fix: unnecessary terminate logic" This reverts commit 141341ad64195d62de219b7be75daf735d266965. --- src/run.js | 4 +++- src/util/platform.js | 4 ++++ src/util/terminate.js | 42 ++++++++++++++++++++++++++++++++++++++++++ src/util/terminate.sh | 12 ++++++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 src/util/platform.js create mode 100644 src/util/terminate.js create mode 100755 src/util/terminate.sh diff --git a/src/run.js b/src/run.js index d4601b7..e411105 100644 --- a/src/run.js +++ b/src/run.js @@ -3,6 +3,7 @@ const pty = require('node-pty') const chalk = require('chalk') const consola = require('consola') const nodeCleanup = require('node-cleanup') +const { terminate } = require('./util/terminate') /** @typedef {import('node-pty').IPty} IPty */ @@ -142,7 +143,8 @@ exports.runScript = (script, folder, streaming) => { exports.killAll = () => { for (const child of children) { - child.kill() + terminate(child, process.cwd()) + .then(() => child.kill()) } children.clear() } diff --git a/src/util/platform.js b/src/util/platform.js new file mode 100644 index 0000000..f63af7a --- /dev/null +++ b/src/util/platform.js @@ -0,0 +1,4 @@ +// OS +exports.isWindows = process.platform === 'win32' +exports.isMacintosh = process.platform === 'darwin' +exports.isLinux = process.platform === 'linux' diff --git a/src/util/terminate.js b/src/util/terminate.js new file mode 100644 index 0000000..eda2d8e --- /dev/null +++ b/src/util/terminate.js @@ -0,0 +1,42 @@ +const util = require('util') +const cp = require('child_process') +const path = require('path') +const { + isWindows, + isLinux, + isMacintosh, +} = require('./platform') + +const execFile = util.promisify(cp.execFile) +const spawn = util.promisify(cp.spawn) + +exports.terminate = async function (childProcess, cwd) { + if (isWindows) { + try { + let options = { + stdio: ['pipe', 'pipe', 'ignore'], + } + if (cwd) { + options.cwd = cwd + } + await execFile('taskkill', ['/T', '/F', '/PID', childProcess.pid.toString()], options) + } catch (err) { + return { success: false, error: err } + } + } else if (isLinux || isMacintosh) { + try { + let cmd = path.resolve(__dirname, './terminate.sh') + let result = await spawn(cmd, [childProcess.pid.toString()], { + cwd, + }) + if (result.error) { + return { success: false, error: result.error } + } + } catch (err) { + return { success: false, error: err } + } + } else { + childProcess.kill('SIGKILL') + } + return { success: true } +} diff --git a/src/util/terminate.sh b/src/util/terminate.sh new file mode 100755 index 0000000..d3b8121 --- /dev/null +++ b/src/util/terminate.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +terminateTree() { + for cpid in $(/usr/bin/pgrep -P $1); do + terminateTree $cpid + done + kill -9 $1 > /dev/null 2>&1 +} + +for pid in $*; do + terminateTree $pid +done