Created
March 12, 2014 05:18
-
-
Save rjz/9501304 to your computer and use it in GitHub Desktop.
Streaming from stdin or a 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
// Depends on `through` | |
// | |
// $ npm install through | |
// | |
// Usage: | |
// | |
// $ echo 'hello' | node stdin-and-fs-stream.js | |
// $ echo 'hello' > tmp && node stdin-and-fs-stream.js tmp | |
// | |
var fs = require('fs'), | |
through = require('through'); | |
var tr = through(function (buf) { | |
console.log(buf.toString()); | |
}); | |
var stream; | |
if (process.argv.length > 2) { | |
stream = fs.createReadStream(process.argv[2]); | |
} | |
else { | |
stream = process.stdin; | |
setImmediate(function () { | |
stream.push(null); | |
}); | |
} | |
stream.pipe(tr).pipe(process.stdout); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment