Skip to content

tunnckoCore/capture-spawn

Repository files navigation

Asynchronously capturing spawn output.

code climate standard code style travis build status coverage status dependency status

Install

npm i capture-spawn --save

Usage

For more use-cases see the tests

const captureSpawn = require('capture-spawn')

Capture output of asynchronous spawn.

Params

  • cp {Stream}: Child process spawn stream.
  • callback {Function}: Gets error, result or result buffer - cb(err, res, buf).
  • returns {Stream}: Passed cp stream.

Example

var captureSpawn = require('capture-spawn')
var spawn = require('cross-spawn-async')

var cp = spawn('echo', ['hello charlike', 'world'])
var stream = captureSpawn(cp, function callback (err, res, buf) {
  if (err) return console.error(err)
  console.log('result', res) // => 'hello charlike world\n'
  console.log('buffer', buf) // => <Buffer ...>
  console.log('result === buffer.toString()', buf.toString()) // => 'hello charlike world\n'
})
console.log(cp === stream) // => true

Error handling

Where buffer is the stderr output and code can be string or number.

var proc = spawn('node', ['not-exist', 'something'])
captureSpawn(proc, console.log)
// => SpawnError {
//   name: 'SpawnError',
//   message: '',
//   code: 1,
//   buffer: <Buffer 6d 6f 64 75 ...>
// }

Related

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
But before doing anything, please read the CONTRIBUTING.md guidelines.

tunnckoCore.tk keybase tunnckoCore tunnckoCore npm tunnckoCore twitter tunnckoCore github