Created
February 2, 2012 10:45
-
-
Save bjouhier/1722850 to your computer and use it in GitHub Desktop.
Streamlined function to concatenate several input files into one output file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| "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', _); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added
usingReadable,usingWritableandpumpto the API, to capture common patterns and avoid proliferation oftry/finallyblocks in the code. Makes the code a bit leaner.