Permalink
Browse files

data method for collecting proc output

  • Loading branch information...
1 parent f61596a commit 64a0bda95f9878c84513f386790c84415f8edc6c @polotek committed Jan 14, 2012
Showing with 36 additions and 1 deletion.
  1. +17 −0 collector.js
  2. +19 −1 procstreams.js
View
@@ -0,0 +1,17 @@
+var Stream = require('stream').Stream
+ , inherits = require('inherits');
+
+var Collector = function() {
+ this.data = '';
+ this.writable = true;
+}
+inherits(Collector, Stream)
+Collector.prototype.write = function(d) {
+ this.data +=d;
+ this.emit('data', d);
+}
+Collector.prototype.end = function() {
+ this.writable = false;
+ this.emit('end');
+}
+exports.Collector = Collector;
View
@@ -2,7 +2,8 @@ var slice = Array.prototype.slice
, EventEmitter = require('events').EventEmitter
, spawn = require('child_process').spawn
, inherits = require('inherits')
- , utils = require('./protochains');
+ , utils = require('./protochains')
+ , Collector = require('./collector').Collector;
function procPipe(dest, options) {
options = options || {}
@@ -87,6 +88,17 @@ function normalizeArguments(cmd, args, opts, callback) {
}
}
+function collect() {
+ var stdout = this.stdout.pipe(new Collector())
+ , stderr = this.stderr.pipe(new Collector());
+
+ this.on('exit', function(err, signal) {
+ if(err === 0) {
+ this.emit('_output', stdout.data, stderr.data);
+ }
+ }.bind(this));
+}
+
function procStream(cmd, args, opts, callback) {
if(!cmd) { throw new Error('Missing command'); }
@@ -121,6 +133,8 @@ function procStream(cmd, args, opts, callback) {
if(typeof callback == 'function') { proc.on('exit', callback); }
+ // TODO: This should be immediate instead of nextTick. But it fails
+ // for some reason
process.nextTick(function() { proc.emit('start'); });
return proc;
}
@@ -136,6 +150,10 @@ procStream._prototype = {
return this;
}
+ , data: function data(fn) {
+ this.on('_output', fn);
+ this.once('start', collect)
+ }
, and: function and() {
var args = slice.call(arguments)
, dest = new procPromise(args);

0 comments on commit 64a0bda

Please sign in to comment.