Giter VIP home page Giter VIP logo

output-file's Introduction

output-file

npm version Build Status codecov

Write a file and create its ancestor directories if needed

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's fs.outputFile, 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 npm.

npm install output-file

API

const outputFile = require('output-file');

outputFile(path, data [, options])

path: string | Buffer | Uint8Array | URL
data: string | Buffer | TypedArray | DataView
options: Object (options) or string (file encoding)
Return: Promise<undefined>

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.

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() and fs.promises.mkdir(), 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.

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

License

ISC License ยฉ 2018 - 2019 Shinnosuke Watanabe

output-file's People

Contributors

shinnn avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.