diff --git a/package.json b/package.json index 0778c87d..521b218e 100644 --- a/package.json +++ b/package.json @@ -94,7 +94,6 @@ "safe-json-stringify": "^1.1.0", "shutdown": "^0.3.0", "stream-http": "^2.8.0", - "subcomandante": "^1.0.5", "superagent": "^3.8.2", "truthy": "0.0.1" }, diff --git a/src/utils/exec.js b/src/utils/exec.js index 233b99cc..0ded8054 100644 --- a/src/utils/exec.js +++ b/src/utils/exec.js @@ -1,6 +1,6 @@ 'use strict' -const run = require('subcomandante') +const run = require('./subcomandante-lite') const once = require('once') const debug = require('debug') const log = debug('ipfsd-ctl:exec') diff --git a/src/utils/subcomandante-lite.js b/src/utils/subcomandante-lite.js new file mode 100644 index 00000000..85da609f --- /dev/null +++ b/src/utils/subcomandante-lite.js @@ -0,0 +1,45 @@ +'use strict' + +const runner = require('child_process') +const debug = require('debug') +const log = debug('ipfsd-ctl:sclite') + +const children = [] + +function removeChild (child) { + const i = children.indexOf(child) + if (i !== -1) { + children.slice(i, 1) + } +} + +function killAll () { + log('killing all children') + let child + while ((child = children.shift()) !== undefined) { + log(child.pid, 'killing') + child.kill() + } +} + +process.once('exit', killAll) +process.once('SIGTERM', killAll) +process.once('SIGINT', killAll) + +function run (cmd, args, opts) { + const child = runner.execFile(cmd, args, opts) + log(child.pid, 'new') + + children.push(child) + child.once('error', () => { + log(child.pid, 'error') + removeChild(child) + }) + child.once('exit', () => { + log(child.pid, 'exit') + removeChild(child) + }) + return child +} + +module.exports = run