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
Write data to the file, or print data on stdout if no files are specified
const fs = require('fs');
const fileOrStdout = require('file-or-stdout');
fileOrStdout('path/to/a/file', 'Hi').then(() => {
fs.readFileSync('path/to/a/file', 'utf8'); //=> 'Hi'
});
fileOrStdout(null, 'Hi'); //=> print 'Hi' on stdout
Installation
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
When the first argument is a file path, it writes data on a file creating its ancestor directories if needed. The returned promise will be resolved with true
.
When the first argument is a falsy value, it prints data on a stdout. The returned promise will be resolved with false
.
fileOrStdout('file.txt', 'data').then(isFileWritten => {
fs.readFileSync('file.txt', 'utf8'); //=> 'data'
isFileWritten; //=> true
});
fileOrStdout('', 'data').then(isFileWritten => {
isFileWritten; //=> false
});
License
Copyright (c) 2016 Shinnosuke Watanabe
Licensed under the MIT License.