pipe-program
Easily create a pipe-able program.
npm install pipe-program --save
yarn add pipe-program
Why
In order to encourage smaller, focused programs, it should be easy to compose programs by using the unix pipe "|" operator. Your program should read from stdin and write to stdout unless it's actually meant to write to the file system or do some other IO.
Ideally your program would just be a function and something else takes care of all the reading and writing from the process.stdin and stdout.
The pipe-program
package is that "something else".
Simple example
{ return stdin + ' This is stdout.'} myFunction // echo -n "This is stdin." | node simple-example.js// "This is stdin. This is stdout." will be printed.
Async example
/** * This will read the file passed in through stdin */ { return } myAsyncFunction // echo -n "package.json" | node async-example.js// The contents of package.json (if it exists) will be printed.
Stream example
pipe-program
is designed to work with streams. If your function
returns one it will be piped to stdout.
{ return } myStreamFunction // echo -n "package.json" | node stream-example.js// The contents of package.json (if it exists) will be printed.
Errors
All errors, both async and sync, will be written to process.stdout
.
If you return a rejected promise it will attempt to pass the error to stdout.