pipe-program
Create a pipe-able program. stdin goes to your function, your function return goes to stdout.
Last updated 2 years ago by justindfuller .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install pipe-program 
SYNC missed versions from official npm registry.

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

import pipeProgram from 'pipe-program'

function myFunction (stdin) {
  return stdin + ' This is stdout.'
}

export default pipeProgram(myFunction)

// echo -n "This is stdin." | node simple-example.js
// "This is stdin. This is stdout." will be printed.

Async example

import path from 'path'
import pipeProgram from 'pipe-program'
import { readFileAsync } from 'fs-extra-promise'

/**
 * This will read the file passed in through stdin
 */
function myAsyncFunction (stdin) {
  return readFileAsync(path.resolve(__dirname, stdin))
}

export default pipeProgram(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.

import path from 'path'
import { createReadStream } from 'fs'
import pipeProgram from 'pipe-program'

function myStreamFunction (stdin) {
  return createReadStream(path.resolve(__dirname, stdin), {
    encoding: 'utf8'
  })
}

export default pipeProgram(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.

Current Tags

  • 0.0.2                                ...           latest (2 years ago)

2 Versions

  • 0.0.2                                ...           2 years ago
  • 0.0.1                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 0
Last Month 2
Dependencies (1)
Dev Dependencies (2)
Dependents (0)
None

Copyright 2014 - 2017 © taobao.org |