Created
July 26, 2013 18:52
-
-
Save sixolet/6091321 to your computer and use it in GitHub Desktop.
This is a thing that I often use to wrap child_process in Meteor (or variations on it)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Exec = {}; | |
var Fiber = Npm.require('fibers'); | |
var Future = Npm.require('fibers/future'); | |
var Process = Npm.require('child_process'); | |
Exec.spawn = function (command, args, options) { | |
var out = ""; | |
var err = ""; | |
var ret = new Future; | |
options = options || {}; | |
var proc = Process.spawn(command, args, options); | |
proc.stdout.setEncoding('utf8'); | |
proc.stderr.setEncoding('utf8'); | |
if (options.captureOut) { | |
proc.stdout.on('data', Meteor.bindEnvironment(function (data) { | |
if (options.log) | |
console.log(data); | |
out += data; | |
if (typeof options.captureOut === 'function') { | |
options.captureOut(data); | |
} | |
}, function (err) { | |
Log.warn(err); | |
})); | |
} | |
if (options.captureErr) { | |
proc.stderr.on('data', Meteor.bindEnvironment(function (data) { | |
if (options.log) | |
console.log(data); | |
err += data; | |
if (typeof options.captureErr === 'function') { | |
options.captureErr(data); | |
} | |
}, function (err) { | |
Log.warn(err); | |
})); | |
} | |
proc.on('exit', function (code) { | |
ret.return({ | |
stdout: out, | |
stderr: err, | |
code: code | |
}); | |
}); | |
ret.proc = proc; | |
return ret; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In newer versions of Node (>=0.8) you should use
close
instead ofexit
:Also, I'm not sure if it helps, but I've wrapped the
close
callback in aMeteor.bindEnvironment
: