All Projects → shinnn → output-file-sync

shinnn / output-file-sync

Licence: ISC license
Synchronously write a file and create its ancestor directories if needed

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to output-file-sync

NavigationRouter
A router implementation designed for complex modular apps, written in Swift
Stars: ✭ 89 (+535.71%)
Mutual labels:  modular
encryptFS
A simple file system that encrypts files individually.
Stars: ✭ 15 (+7.14%)
Mutual labels:  filesystem
cephgeorep
An efficient unidirectional remote backup daemon for CephFS.
Stars: ✭ 27 (+92.86%)
Mutual labels:  synchronous
snapos
Snapcast OS
Stars: ✭ 73 (+421.43%)
Mutual labels:  synchronous
Lexical.FileSystem
Virtual IFileSystem interfaces, and implementations.
Stars: ✭ 24 (+71.43%)
Mutual labels:  filesystem
agile
🌌 Global State and Logic Library for JavaScript/Typescript applications
Stars: ✭ 90 (+542.86%)
Mutual labels:  modular
fusell-seed
FUSE (the low-level interface) file system boilerplate 📂 🔌 💾
Stars: ✭ 13 (-7.14%)
Mutual labels:  filesystem
chokidar-socket-emitter
a simple chokidar watcher which emits events to all connected socket.io clients
Stars: ✭ 28 (+100%)
Mutual labels:  filesystem
actions
Software without side-effects. Redo and Undo.
Stars: ✭ 23 (+64.29%)
Mutual labels:  modular
xbox-winfsp
Brings native support for Xbox filesystems (FATX, STFS & GDFX/XGD/XDVDFS) to Windows.
Stars: ✭ 37 (+164.29%)
Mutual labels:  filesystem
Save-System-for-Unity
Save System for Unity with AOT (IL2CPP) and assets references support.
Stars: ✭ 116 (+728.57%)
Mutual labels:  filesystem
Imm2Virtual
This is a GUI (for Windows 64 bit) for a procedure to virtualize your EWF(E01), DD (raw), AFF disk image file without converting it, directly with VirtualBox, forensically proof.
Stars: ✭ 40 (+185.71%)
Mutual labels:  filesystem
Vudit
A file viewer for Android
Stars: ✭ 40 (+185.71%)
Mutual labels:  filesystem
xplr
A hackable, minimal, fast TUI file explorer
Stars: ✭ 2,271 (+16121.43%)
Mutual labels:  filesystem
EasyChat
A modular Garry's Mod chat addon for both users and developers.
Stars: ✭ 74 (+428.57%)
Mutual labels:  modular
laravel-cms-core
Laravel CMS Core
Stars: ✭ 23 (+64.29%)
Mutual labels:  modular
unionfs
Use multiple fs modules at once
Stars: ✭ 170 (+1114.29%)
Mutual labels:  filesystem
btcfs
Use the blockchain as a filesystem in Plan9
Stars: ✭ 20 (+42.86%)
Mutual labels:  filesystem
fuse-nfs-crossbuild-scripts
fuse-nfs for windows using dokany
Stars: ✭ 35 (+150%)
Mutual labels:  filesystem
mindjs
Minimalistic, pure Node.js framework superpowered with Dependency Injection 💡 💻 🚀
Stars: ✭ 17 (+21.43%)
Mutual labels:  modular

output-file-sync

npm version Build Status Coverage Status

Synchronously write a file and create its ancestor directories if needed

import {readFileSync} from 'fs';
import outputFileSync from '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.
    import {statSync} from 'fs';
    import outputFileSync from '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

import outputFileSync from '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.

import {statSync} from 'fs';
import outputFileSync from '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 does:

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

License

ISC License © 2017 - 2019 Watanabe Shinnosuke

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].