Skip to content
This repository has been archived by the owner on Nov 12, 2021. It is now read-only.

Commit

Permalink
Revert "fix: unnecessary terminate logic"
Browse files Browse the repository at this point in the history
This reverts commit 141341a.
  • Loading branch information
Guillaume Chau committed Oct 22, 2019
1 parent f0b8207 commit b3ca562
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down Expand Up @@ -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()
}
Expand Down
4 changes: 4 additions & 0 deletions src/util/platform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// OS
exports.isWindows = process.platform === 'win32'
exports.isMacintosh = process.platform === 'darwin'
exports.isLinux = process.platform === 'linux'
42 changes: 42 additions & 0 deletions src/util/terminate.js
Original file line number Diff line number Diff line change
@@ -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 }
}
12 changes: 12 additions & 0 deletions src/util/terminate.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b3ca562

Please sign in to comment.