Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shinnn/output-file
Write a file and create its ancestor directories if needed
https://github.com/shinnn/output-file
ancestor-directories async javascript mkdir mkdirp nodejs promise writefile
Last synced: 27 days ago
JSON representation
Write a file and create its ancestor directories if needed
- Host: GitHub
- URL: https://github.com/shinnn/output-file
- Owner: shinnn
- License: isc
- Created: 2014-11-13T02:30:17.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2019-04-23T18:21:38.000Z (over 5 years ago)
- Last Synced: 2024-04-25T07:44:09.517Z (7 months ago)
- Topics: ancestor-directories, async, javascript, mkdir, mkdirp, nodejs, promise, writefile
- Language: JavaScript
- Size: 148 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# output-file
[![npm version](https://img.shields.io/npm/v/output-file.svg?style=flat)](https://www.npmjs.com/package/output-file)
[![Build Status](https://travis-ci.com/shinnn/output-file.svg?branch=master)](https://travis-ci.com/shinnn/output-file)
[![codecov](https://codecov.io/gh/shinnn/output-file/branch/master/graph/badge.svg)](https://codecov.io/gh/shinnn/output-file)Write a file and create its ancestor directories if needed
```javascript
const {readFile} = require('fs').promises;
const outputFile = require('output-file');(async () => {
await outputFile('foo/bar/baz.txt', 'Hi!');
await readFile('foo/bar/baz.txt', 'utf8'); //=> 'Hi!'
})();
```This module is very similar to [fs-extra](https://github.com/jprichardson/node-fs-extra)'s [`fs.outputFile`](https://github.com/jprichardson/node-fs-extra/blob/HEAD/docs/outputFile.md), but has the following features fs-extra doesn't have:
* Support for various non-string path types – `Buffer`, `Uint8Array` and `URL`
* An option to set mode of created directories## Installation
[Use](https://docs.npmjs.com/cli/install) [npm](https://docs.npmjs.com/about-npm/).
```
npm install output-file
```## API
```javascript
const outputFile = require('output-file');
```### outputFile(*path*, *data* [, *options*])
*path*: `string | Buffer | Uint8Array | URL`
*data*: `string | Buffer | TypedArray | DataView`
*options*: `Object` ([options](#options)) or `string` (file encoding)
Return: `Promise`It writes the data to a file asynchronously. If ancestor directories of a file don't exist, it creates those directories before writing a file.
```javascript
const {stat} = require('fs').promises;
const outputFile = require('output-file');// When the directory `foo/bar` exists
(async () => {
await outputFile('foo/bar/baz/qux.txt', 'Hello');(await stat('foo/bar/baz')).isDirectory(); //=> true
(await stat('foo/bar/baz/qux.txt')).isFile(); //=> true
})();
```#### options
All options for [`fs.promises.writeFile()`](https://nodejs.org/api/fs.html#fs_fspromises_writefile_file_data_options) and [`fs.promises.mkdir()`](https://nodejs.org/api/fs.html#fs_fspromises_mkdir_path_options), except for `mode` and `recursive`, are supported.
`recursive` option is enabled by default and cannot be disabled.
Instead of `mode` option, use the followings:
##### options.fileMode
Set mode of a file.
##### options.dirMode
Set mode of directories.
```javascript
const {stat} = require('fs').promises;
const outputFile = require('output-file');(async () => {
await outputFile('dir/file', 'content', {
dirMode: '0745',
fileMode: '0644'
});(await stat('dir')).mode.toString(8); //=> '40745'
(await stat('dir/file')).mode.toString(8); //=> '100644'
})();
```## Related project
* [output-file-sync](https://github.com/shinnn/output-file-sync) (synchronous version)
## License
[ISC License](./LICENSE) © 2018 - 2019 Shinnosuke Watanabe