1module.exports = spawn 2 3var _spawn = require('child_process').spawn 4var EventEmitter = require('events').EventEmitter 5var npwr = require('./no-progress-while-running.js') 6 7function willCmdOutput (stdio) { 8 if (stdio === 'inherit') return true 9 if (!Array.isArray(stdio)) return false 10 for (var fh = 1; fh <= 2; ++fh) { 11 if (stdio[fh] === 'inherit') return true 12 if (stdio[fh] === 1 || stdio[fh] === 2) return true 13 } 14 return false 15} 16 17function spawn (cmd, args, options) { 18 var cmdWillOutput = willCmdOutput(options && options.stdio) 19 20 if (cmdWillOutput) npwr.startRunning() 21 var raw = _spawn(cmd, args, options) 22 var cooked = new EventEmitter() 23 24 raw.on('error', function (er) { 25 if (cmdWillOutput) npwr.stopRunning() 26 er.file = cmd 27 cooked.emit('error', er) 28 }).on('close', function (code, signal) { 29 if (cmdWillOutput) npwr.stopRunning() 30 // Create ENOENT error because Node.js v0.8 will not emit 31 // an `error` event if the command could not be found. 32 if (code === 127) { 33 var er = new Error('spawn ENOENT') 34 er.code = 'ENOENT' 35 er.errno = 'ENOENT' 36 er.syscall = 'spawn' 37 er.file = cmd 38 cooked.emit('error', er) 39 } else { 40 cooked.emit('close', code, signal) 41 } 42 }) 43 44 cooked.stdin = raw.stdin 45 cooked.stdout = raw.stdout 46 cooked.stderr = raw.stderr 47 cooked.kill = function (sig) { return raw.kill(sig) } 48 49 return cooked 50} 51