UNPKG

699 BJavaScriptView Raw
1"use strict";
2
3const cp = require('child_process');
4const once = require('../function/once');
5
6
7module.exports = function(cmd /*, options, chain*/) {
8 var args = Array.from(arguments);
9 var chain = once(args.pop());
10 cmd = args.shift();
11 var options = args.shift() || {};
12
13 if(Array.isArray(options))
14 options = { args : options};
15
16 options.stdio = ['inherit', 'inherit', 'inherit'];
17
18 try {
19 var ps = cp.spawn(cmd, options.args || [], options);
20
21 ps.on('error', chain);
22
23 ps.on('close', function(exit) {
24 var err = null;
25 if(exit !== 0)
26 err = "Bad exit code " + exit;
27 return chain(err, exit);
28 });
29 } catch(err) {
30 chain(err);
31 }
32};
33