index.js
function buildSpawn (cmd, args, opts, callback) {
  var proc = spawn(cmd, args, opts)
  var buffer = new Buffer('')
  var cmdError = {}

  cmd = cmd + ' ' + args.join(' ')

  if (proc.stdout) {
    proc.stdout.on('data', function indexOnData (data) {
      buffer = Buffer.concat([buffer, data])
    })
  }

  proc
    .on('error', function spawnOnError (err) {
      cmdError = new CommandError({
        command: cmd,
        message: err.message ? err.message : undefined,
        stack: err.stack ? err.stack : undefined,
        buffer: buffer ? buffer : undefined,
        status: err.status ? err.status : 1
      })
    })
    .on('close', function spawnOnClose (code) {
      if (code === 0) {
        callback(null, buffer.toString().trim(), code, buffer)
        return
      }

      cmdError = new CommandError({
        command: cmd,
        message: cmdError.message ? cmdError.message : undefined,
        stack: cmdError.stack ? cmdError.stack : undefined,
        buffer: cmdError.buffer ? cmdError.buffer : buffer,
        status: cmdError.status ? cmdError.status : code
      })
      callback(cmdError, undefined, code, undefined)
    })

  return proc
}
function buildSpawn (cmd, args, opts, callback) {
  var proc = spawn(cmd, args, opts)
  var buffer = new Buffer('')
  var cmdError = {}

  cmd = cmd + ' ' + args.join(' ')

  if (proc.stdout) {
    proc.stdout.on('data', function indexOnData (data) {
      buffer = Buffer.concat([buffer, data])
    })
  }

  proc
    .on('error', function spawnOnError (err) {
      cmdError = new CommandError({
        command: cmd,
        message: err.message ? err.message : undefined,
        stack: err.stack ? err.stack : undefined,
        buffer: buffer ? buffer : undefined,
        status: err.status ? err.status : 1
      })
    })
    .on('close', function spawnOnClose (code) {
      if (code === 0) {
        callback(null, buffer.toString().trim(), code, buffer)
        return
      }

      cmdError = new CommandError({
        command: cmd,
        message: cmdError.message ? cmdError.message : undefined,
        stack: cmdError.stack ? cmdError.stack : undefined,
        buffer: cmdError.buffer ? cmdError.buffer : buffer,
        status: cmdError.status ? cmdError.status : code
      })
      callback(cmdError, undefined, code, undefined)
    })

  return proc
}
function buildSpawn ( cmd , args , opts , callback ) { var proc = spawn ( cmd , args , opts ) var buffer = new Buffer ( '' ) var cmdError = { } cmd = cmd + ' ' + args . join ( ' ' ) if ( proc . stdout ) { proc . stdout . on ( 'data' , function indexOnData ( data ) { buffer = Buffer . concat ( [ buffer , data ] ) } ) } proc . on ( 'error' , function spawnOnError ( err ) { cmdError = new CommandError ( { command : cmd , message : err . message ? err . message : undefined , stack : err . stack ? err . stack : undefined , buffer : buffer ? buffer : undefined , status : err . status ? err . status : 1 } ) } ) . on ( 'close' , function spawnOnClose ( code ) { if ( code === 0 ) { callback ( null , buffer . toString ( ) . trim ( ) , code , buffer ) return } cmdError = new CommandError ( { command : cmd , message : cmdError . message ? cmdError . message : undefined , stack : cmdError . stack ? cmdError . stack : undefined , buffer : cmdError . buffer ? cmdError . buffer : buffer , status : cmdError . status ? cmdError . status : code } ) callback ( cmdError , undefined , code , undefined ) } ) return proc }
> Handle cross-spawn.

@param  {String} `cmd`
@param  {Array} `args`
@param  {Object} `opts`
@param  {Function} `callback`
@return {Stream} actually what `child_process.spawn` returns
@api private
> Handle cross - spawn .