Permalink
Browse files

interspersing stream and string pipes works

  • Loading branch information...
1 parent de0c919 commit b097f918bf7102c72321c65ebee24d29c27ab76b @substack committed Jan 30, 2012
Showing with 28 additions and 15 deletions.
  1. +21 −12 procstreams.js
  2. +7 −3 tests/test-pipe.js
View
@@ -200,23 +200,32 @@ procStream._prototype = {
return dest;
}
, pipe: function(cmd, args, options) {
+ var source = this,
+ dest;
+
if (typeof cmd === 'object' && cmd.write) {
options = args || {};
- this.stdout.pipe(cmd, { end : false });
- this.stderr.pipe(cmd, { end : false });
- this.on('exit', function () {
- if (cmd.end && options.end !== false) cmd.end()
+
+ var dest = new EventEmitter;
+ dest.stdin = cmd;
+ dest.stdout = new Stream;
+ dest.stderr = new Stream;
+
+ cmd.on('data', dest.stdout.emit.bind(dest.stdout, 'data'));
+ cmd.on('end', function () {
+ dest.stdout.emit('end');
+ dest.stderr.emit('end');
+ dest.emit('exit');
});
- return cmd;
- }
-
- var source = this
- , dest = procStream.apply(null, arguments);
-
- if(typeof args != 'string' && !Array.isArray(args)) {
- options = args;
+ dest = procStream.enhance(dest);
+ } else {
+ dest = procStream.apply(null, arguments);
+ if(typeof args != 'string' && !Array.isArray(args)) {
+ options = args;
+ }
}
+
return procPipe.call(source, dest, options);
}
}
View
@@ -103,9 +103,13 @@ exec('cat tests/fixtures/10lines.txt | grep "even" | wc -l'
var grepData = ''
grep.write = function (buf) { grepData += buf }
grep.end = function () {
- grep.emit('data', grepData.split('\n').filter(function (line) {
- return line.match(/even/)
- }).join('\n'))
+ grep.emit('data', grepData
+ .split('\n')
+ .map(function (line) { return line + '\n' })
+ .filter(function (line) { return line.match(/even/) })
+ .join('')
+ )
+
grep.emit('end')
}

0 comments on commit b097f91

Please sign in to comment.