Skip to content

Instantly share code, notes, and snippets.

@thibauts
Last active August 3, 2018 11:44
Show Gist options
  • Save thibauts/5b92c7d4f367b8deefde to your computer and use it in GitHub Desktop.
Save thibauts/5b92c7d4f367b8deefde to your computer and use it in GitHub Desktop.
Takes a stream of filenames on stdin and outputs the file contents on stdout each time a new filename is pushed.
/*
* Takes a stream of filenames on stdin and outputs
* the file contents on stdout each time a new filename is pushed.
*/
var _ = require('highland');
var fs = require('fs');
function foo(filename) {
return _(fs.createReadStream(filename)); // `sequence()` expects an highland stream so let's wrap
}
/*
* We first split on line breaks, then map each filename to a stream of the file contents.
* `sequence()` expects a stream of streams and concatenates it in a single output stream so we're fine :)
* See http://highlandjs.org/#sequence
*/
_(process.stdin).split().map(foo).sequence().pipe(process.stdout);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment