file-or-stdin
Read a file, or read stdin if no files are specified
Last updated 2 years ago by shinnn .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install file-or-stdin 
SYNC missed versions from official npm registry.

file-or-stdin

npm version Build Status Coverage Status

Read a file, or read stdin if no files are specified

// echo "Hello!" | node example.js
const fileOrStdin = require('file-or-stdin');

(async () => {
  (await fileOrStdin('path/to/a/file')).toString() // file contents;
  (await fileOrStdin(null)).toString(); //=> 'Hello!'
})();

Installation

Use npm.

npm install file-or-stdin

API

const fileOrStdin = require('file-or-stdin');

fileOrStdin(filePath [, options])

filePath: string or a falsy value
options: Object (fs.readFile options) or string (encoding)
Return: Promise<Buffer> or Promise<string>

When the first argument is a file path, it reads the given file and returns a promise of the file contents.

When the first argument is a falsy value, it reads stdin and returns a promise of the buffered stdin data.

// echo "nodejs" | node example.js
(async () => {
  await fileOrStdin('', 'utf8'); //=> 'nodejs'
})();
// echo "nodejs" | node example.js
(async () => {
  await fileOrStdin('', 'base64'); //=> 'bm9kZWpz'
})();

License

ISC License © 2018 Shinnosuke Watanabe

Current Tags

  • 2.0.0-2                                ...           latest (2 years ago)

10 Versions

  • 2.0.0-2                                ...           2 years ago
  • 2.0.0-1                                ...           2 years ago
  • 2.0.0-0                                ...           2 years ago
  • 1.0.3                                ...           2 years ago
  • 1.0.2                                ...           3 years ago
  • 1.0.1                                ...           3 years ago
  • 1.0.0                                ...           4 years ago
  • 0.0.4                                ...           4 years ago
  • 0.0.1                                ...           4 years ago
  • 0.0.0                                ...           4 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 2
Last Month 3
Dependencies (3)
Dev Dependencies (5)

Copyright 2014 - 2017 © taobao.org |