You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
876 B
40 lines
876 B
const { spawn } = require('child_process')
|
|
let pidMap = {}
|
|
|
|
/**
|
|
* options: { onSuccess: null | function, onError: null | function, onClose: null | function }
|
|
*/
|
|
function start(command, args, options = {}) {
|
|
const childProgress = spawn(command, args)
|
|
pidMap[childProgress.pid] = childProgress
|
|
|
|
childProgress.stdout.on('data' data => {
|
|
options.onSuccess && options.onSuccess(data)
|
|
})
|
|
|
|
childProgress.stderr.on('data', data => {
|
|
options.onError && options.onError(data)
|
|
})
|
|
|
|
childProgress.on('close', code => {
|
|
options.onClose && options.onClose(code)
|
|
})
|
|
|
|
return childProgress.pid
|
|
}
|
|
|
|
function kill(pid) {
|
|
const childProgress = pidMap[pid]
|
|
let status = false
|
|
if (childProgress && !childProgress.killed) {
|
|
status = childProgress.kill()
|
|
}
|
|
childProgress = null
|
|
delete pidMap[pid]
|
|
return status
|
|
}
|
|
|
|
module.exports = {
|
|
start,
|
|
kill,
|
|
}
|