fos

file-or-stdin

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

Showing:

Popularity

Downloads/wk

75

GitHub Stars

0

Maintenance

Last Commit

3yrs ago

Contributors

0

Package

Dependencies

3

Size (min+gzip)

4.4KB

License

ISC

Type Definitions

Tree-Shakeable

No?

Categories

Readme

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 - 2019 Shinnosuke Watanabe

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100