Skip to content

Instantly share code, notes, and snippets.

@bjouhier bjouhier/chomp.js
Created Feb 2, 2012

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

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
You can’t perform that action at this time.