3.0.0 • Published 5 years ago

file-or-stdout v3.0.0

Weekly downloads
4
License
ISC
Repository
github
Last release
5 years ago

file-or-stdout

npm version Build Status Coverage Status

Write data to a file, or to stdout if no file is specified

const {readFile} = require('fs').promises;
const fileOrStdout = require('file-or-stdout');

(async () => {
  await fileOrStdout('path/to/a/file', 'Hi');
  await readFile('path/to/a/file', 'utf8'); //=> 'Hi'
})();

fileOrStdout(null, 'Hi'); // print 'Hi' on stdout

Installation

Use npm.

npm install file-or-stdout

API

const fileOrStdout = require('file-or-stdout');

fileOrStdout(filePath, data , options)

filePath: string Buffer Uint8Array URL or a falsy value
data: string Buffer Uint8Array
options: Object (output-file options) or string (encoding)
Return: Promise<boolean>

When the first argument is a file path, it writes data to a file after creating its missing ancestor directories. The returned Promise will be resolved with true.

When the first argument is a falsy value, it writes data to process.stdout. The returned Promise will be resolved with false.

(async () => {
  const isFileWritten = await fileOrStdout(new URL('file:///Users/your/file.txt'), 'hello');

  await fs.promises.readFile('/Users/your/file.txt', 'utf8'); //=> 'hello'
  isFileWritten; //=> true
})();

(async () => {
  const isFileWritten = await fileOrStdout('', 'hello');
  isFileWritten; //=> false
})();

Related project

  • file-or-stdin — Inverse of this module; read a file, or read stdin if no file is specified

License

ISC License © 2018 - 2019 Shinnosuke Watanabe

3.0.0

5 years ago

2.0.0-5

5 years ago

2.0.0-4

6 years ago

2.0.0-3

6 years ago

2.0.0-2

6 years ago

2.0.0-1

6 years ago

2.0.0-0

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.1

6 years ago

1.0.0

8 years ago

0.2.0

8 years ago

0.1.0

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago

0.0.0

8 years ago