Skip to content

Instantly share code, notes, and snippets.

@bjouhier
Created February 2, 2012 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjouhier/1722850 to your computer and use it in GitHub Desktop.
Save bjouhier/1722850 to your computer and use it in GitHub Desktop.
Streamlined function to concatenate several input files into one output file
"use strict";
if (!require('streamline/module')(module)) return;
var fs = require('fs');
var streams = require('streamline/lib/streams/server/streams');
// cat ins > out
exports.chomp = function(ins, out, _) {
streams.usingWritable(_, fs.createWriteStream(out), function(_, outStream) {
for (var i = 0; i < ins.length; i++) {
streams.usingReadable(_, fs.createReadStream(ins[i]), function(_, inStream) {
streams.pump(_, inStream, outStream);
});
}
});
}
// test it with cat *.js > out.txt
var names = fs.readdir(__dirname, _).filter(function(name) {
return name.match(/.*\.js$/);
}).map(function(name) {
return __dirname + '/' + name;
})
exports.chomp(names, __dirname + '/out.txt', _);
@bjouhier
Copy link
Author

bjouhier commented Feb 4, 2012

Added usingReadable, usingWritable and pump to the API, to capture common patterns and avoid proliferation of try/finally blocks in the code. Makes the code a bit leaner.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment