-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6cb44ec
commit bf05306
Showing
1 changed file
with
45 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,45 @@ | ||
'use strict' | ||
|
||
const runner = require('child_process') | ||
const debug = require('debug')('subchild') | ||
|
||
const children = [] | ||
|
||
function removeChild (child) { | ||
const i = children.indexOf(child) | ||
if (i !== -1) { | ||
children.slice(i, 1) | ||
} | ||
} | ||
|
||
function killAll () { | ||
debug('killing all children') | ||
let child | ||
while((child = children.shift()) !== undefined) { | ||
debug(child.pid, 'killing') | ||
child.kill(); | ||
} | ||
} | ||
|
||
process.once('error', killAll) | ||
process.once('exit', killAll) | ||
process.once('SIGTERM', killAll) | ||
process.once('SIGINT', killAll) | ||
|
||
function run (cmd, args, opts) { | ||
const child = runner.execFile(cmd, args, opts) | ||
debug(child.pid, 'new') | ||
|
||
children.push(child); | ||
child.once('error', () => { | ||
debug(child.pid, 'error') | ||
removeChild(child) | ||
}) | ||
child.once('exit', () => { | ||
debug(child.pid, 'exit') | ||
removeChild(child); | ||
}) | ||
return child | ||
} | ||
|
||
module.exports = run | ||
'use strict' | ||
|
||
const runner = require('child_process') | ||
const debug = require('debug')('subchild') | ||
|
||
const children = [] | ||
|
||
function removeChild (child) { | ||
const i = children.indexOf(child) | ||
if (i !== -1) { | ||
children.slice(i, 1) | ||
} | ||
} | ||
|
||
function killAll () { | ||
debug('killing all children') | ||
let child | ||
while ((child = children.shift()) !== undefined) { | ||
debug(child.pid, 'killing') | ||
child.kill() | ||
} | ||
} | ||
|
||
process.once('error', killAll) | ||
process.once('exit', killAll) | ||
process.once('SIGTERM', killAll) | ||
process.once('SIGINT', killAll) | ||
|
||
function run (cmd, args, opts) { | ||
const child = runner.execFile(cmd, args, opts) | ||
debug(child.pid, 'new') | ||
|
||
children.push(child) | ||
child.once('error', () => { | ||
debug(child.pid, 'error') | ||
removeChild(child) | ||
}) | ||
child.once('exit', () => { | ||
debug(child.pid, 'exit') | ||
removeChild(child) | ||
}) | ||
return child | ||
} | ||
|
||
module.exports = run |