Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shinnn/file-or-stdin
Read a file, or read stdin if no files are specified
https://github.com/shinnn/file-or-stdin
Last synced: 27 days ago
JSON representation
Read a file, or read stdin if no files are specified
- Host: GitHub
- URL: https://github.com/shinnn/file-or-stdin
- Owner: shinnn
- License: isc
- Created: 2016-08-10T10:31:52.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2019-02-21T10:24:46.000Z (over 5 years ago)
- Last Synced: 2024-09-14T16:06:36.024Z (2 months ago)
- Language: JavaScript
- Size: 73.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# file-or-stdin
[![npm version](https://img.shields.io/npm/v/file-or-stdin.svg)](https://www.npmjs.com/package/file-or-stdin)
[![Build Status](https://travis-ci.com/shinnn/file-or-stdin.svg?branch=master)](https://travis-ci.com/shinnn/file-or-stdin)
[![Coverage Status](https://img.shields.io/coveralls/shinnn/file-or-stdin.svg)](https://coveralls.io/github/shinnn/file-or-stdin?branch=master)Read a file, or read [stdin](https://nodejs.org/api/process.html#process_process_stdin) if no files are specified
```javascript
// 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](https://docs.npmjs.com/cli/install) [npm](https://docs.npmjs.com/about-npm/).
```
npm install file-or-stdin
```## API
```javascript
const fileOrStdin = require('file-or-stdin');
```### fileOrStdin(*filePath* [, *options*])
*filePath*: `string` or a [falsy value](https://developer.mozilla.org/docs/Glossary/Falsy)
*options*: `Object` ([`fs.readFile`](https://nodejs.org/api/fs.html#fs_fs_readfile_path_options_callback) options) or `string` (encoding)
Return: `Promise` or `Promise`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](http://www.linfo.org/standard_input.html) and returns a promise of the buffered stdin data.
```javascript
// echo "nodejs" | node example.js
(async () => {
await fileOrStdin('', 'utf8'); //=> 'nodejs'
})();
``````javascript
// echo "nodejs" | node example.js
(async () => {
await fileOrStdin('', 'base64'); //=> 'bm9kZWpz'
})();
```## License
[ISC License](./LICENSE) © 2018 - 2019 Shinnosuke Watanabe