Skip to content

cojs/child-process

Repository files navigation

Build Status

Co Child Process

Spawn a child process using co. Kind of like exec, except you can write to stdin and there aren't as many options like timeout or encoding.

Example

Minify a Javascript in a child process. Useful when the JS files are large and you don't want uglifyjs blocking the event loop.

varfs=require('fs')varco=require('co')varspawn=require('co-child-process')// resolve the location of the binaryvaruglifyjs=require.resolve('uglify-js/bin/uglifyjs')co(function*(){varstream=fs.createReadStream(__filename)varminified=yieldspawn(uglifyjs,['-'// tells uglifyjs to check for `stdin`]).pump(stream)// since you can't pipe into the generator})()

API

var output = spawn(command, [args], [options])

A wrapper around child_process.spawn. The arguments are simply passed and are not touched.

Output is always a string, for now.

var output = spawn(args...).pump(stream)

Pump a stream into the child process' stdin. Kind of like .pipe() but in the opposite direction.

Must be called on the same tick.

var output = spawn(args...).end(body)

Write a buffer or string body to stdin.

varout=yieldspawn(uglifyjs,['-']).end('var a = 1;')

Must be called on the same tick.

About

easily spawn a child process with co

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •