output-file-sync
Synchronously write a file and create its ancestor directories if needed
Last updated 7 years ago by shinnn .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install output-file-sync 
SYNC missed versions from official npm registry.

output-file-sync

npm version Build Status Build status Coverage Status

Synchronously write a file and create its ancestor directories if needed

const {readFileSync} = require('fs');
const outputFileSync = require('output-file-sync');

outputFileSync('foo/bar/baz.txt', 'Hi!');
readFileSync('foo/bar/baz.txt', 'utf8'); //=> 'Hi!'

Difference from fs.outputFileSync

This module is very similar to fs-extra's fs.outputFileSync method, but different in the following points:

  1. output-file-sync returns the path of the directory created first. See the API document for more details.
  2. output-file-sync accepts mkdirp options.
    const {statSync} = require('fs');
    const outputFileSync = require('output-file-sync');
    
    outputFileSync('foo/bar', 'content', {mode: 33260});
    statSync('foo').mode; //=> 33260
    
  3. output-file-sync validates its arguments strictly, and prints highly informative error message.

Installation

Use npm.

npm install output-file-sync

API

const outputFileSync = require('output-file-sync');

outputFileSync(path, data [, options])

path: string
data: string, Buffer or Uint8Array
options: Object (options for fs.writeFileSync and mkdirp) or string (encoding)
Return: string if it creates more than one directories, otherwise null

It writes the data to a file synchronously. If ancestor directories of the file don't exist, it creates the directories before writing the file.

const {statSync} = require('fs');
const outputFileSync = require('output-file-sync');

// When the directory `foo/bar` exists
outputFileSync('foo/bar/baz/qux.txt', 'Hello', 'utf-8');

statSync('foo/bar/baz').isDirectory(); //=> true
statSync('foo/bar/baz/qux.txt').isFile(); //=> true

It returns the directory path just like mkdirp.sync:

Returns the first directory that had to be created, if any.

const dir = outputFileSync('foo/bar/baz.txt', 'Hello');
dir === path.resolve('foo'); //=> true

options

All options for fs.writeFileSync and mkdirp are available.

Additionally, you can pass fileMode and dirMode options to set different permission between the file and directories.

options.fileMode

Set the mode of a file, overriding mode option.

options.dirMode

Set the modes of directories, overriding mode option.

outputFileSync('dir/file', 'content', {dirMode: '0745', fileMode: '0644'});
fs.statSync('dir').mode.toString(8); //=> '40745'
fs.statSync('dir/file').mode.toString(8); //=> '100644'

Related project

License

ISC License © 2017 - 2018 Shinnosuke Watanabe

Current Tags

  • 2.0.1                                ...           latest (7 years ago)

6 Versions

  • 2.0.1                                ...           7 years ago
  • 2.0.0                                ...           8 years ago
  • 1.1.2                                ...           8 years ago
  • 1.1.1                                ...           9 years ago
  • 1.1.0                                ...           10 years ago
  • 1.0.0                                ...           10 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (3)
Dev Dependencies (7)

Copyright 2013 - present © cnpmjs.org | Home |