JSPM

output-file-sync

1.1.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 797199
  • Score
    100M100P100Q185271F
  • License MIT

Synchronously write a file and create its ancestor directories if needed

Package Exports

  • output-file-sync

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 (output-file-sync) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

output-file-sync

NPM version Build Status Build status Coverage Status Dependency Status devDependency Status

Synchronously write a file and create its ancestor directories if needed

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

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

Difference from fs.outputFileSync

This module is very similar to fs-extra's fs.outputFileSync but they are 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 fs = require('fs');
    const outputFileSync = require('output-file-sync');
    outputFileSync('foo/bar', 'content', {mode: 33260});
    
    fs.statSync('foo').mode; //=> 33260

Installation

Use npm.

npm install output-file-sync

API

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

outputFileSync(path, data [, options])

path: String
data: String or Buffer
options: Object or String (options for fs.writeFile and mkdirp)
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 fs = require('fs');
const outputFileSync = require('output-file-sync');

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

fs.statSync('foo/bar/baz').isDirectory(); //=> true
fs.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.

let dir = outputFileSync('foo/bar/baz.txt', 'Hello');
dir; //=> Same value as `path.resolve('foo')`

options

All options for fs.writeFile and mkdirp are available.

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

options.fileMode

Set modes of a file, overriding mode option.

options.dirMode

Set 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'

License

Copyright (c) 2014 - 2015 Shinnosuke Watanabe

Licensed under the MIT License.