JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 6
  • Score
    100M100P100Q34525F
  • License ISC

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

Package Exports

  • file-or-stdout

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (file-or-stdout) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

file-or-stdout

npm version Build Status Coverage Status

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

const {readFileSync} = require('fs');
const fileOrStdout = require('file-or-stdout');

(async () => {
  await fileOrStdout('path/to/a/file', 'Hi');
  readFileSync('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 or a falsy value
data: string or Buffer
options: Object or string (fs.writeFile options)
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('file.txt', 'hello');

  fs.readFileSync('file.txt', 'utf8'); //=> 'hello'
  isFileWritten; //=> true
})();

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

License

ISC License © 2018 Shinnosuke Watanabe