What I would do is use Node.js Transform streams for your scripts, and use combine.js
to require
and pipe
those streams, based on command line arguments.
Example:
// stream1.js
var Transform = require('stream').Transform;
var stream1 = new Transform();
stream1._transform = function(chunk, encoding, done) {
this.push(chunk.toString() + 's1\r\n');
done();
};
module.exports = stream1;
// stream2.js
var Transform = require('stream').Transform;
var stream2 = new Transform();
stream2._transform = function(chunk, encoding, done) {
this.push(chunk.toString() + 's2\r\n');
done();
};
module.exports = stream2;
// combine.js
var stream1 = require('./' + process.argv[2]);
var stream2 = require('./' + process.argv[3]);
process.stdin.pipe(stream1).pipe(stream2).pipe(process.stdout);
That way running:
> echo "hi" | node stream1 stream2
Should output:
hi
s1
s2