
Security News
PyPI Now Supports iOS and Android Wheels for Mobile Python Development
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
output-file-sync
Advanced tools
Synchronously write a file and create its ancestor directories if needed
The output-file-sync npm package is a utility for writing files synchronously, ensuring that the directory structure is created if it does not exist. This is particularly useful for tasks that require file output in build processes or scripts where you need to ensure the file system is in a specific state before proceeding.
Write File Synchronously
This feature allows you to write data to a file synchronously. If the directory structure does not exist, it will be created automatically.
const outputFileSync = require('output-file-sync');
outputFileSync('path/to/file.txt', 'Hello, World!');
Write JSON Data
This feature allows you to write JSON data to a file synchronously. The JSON data is stringified and formatted with indentation for readability.
const outputFileSync = require('output-file-sync');
const data = { key: 'value' };
outputFileSync('path/to/file.json', JSON.stringify(data, null, 2));
Append Data to File
While output-file-sync does not directly support appending data, you can use Node.js's built-in fs module in conjunction with output-file-sync to ensure the file exists before appending data.
const outputFileSync = require('output-file-sync');
const fs = require('fs');
const data = 'Appended data';
fs.appendFileSync('path/to/file.txt', data);
fs-extra is a popular package that extends the native Node.js fs module with additional methods, including methods for writing files synchronously and ensuring the directory structure exists. It provides more comprehensive file system utilities compared to output-file-sync.
write-file-atomic is a package that ensures files are written atomically, which means the file is guaranteed to be written completely or not at all. This is useful for preventing partial writes and ensuring data integrity. It also supports creating directories if they do not exist.
mkdirp is a package focused on creating directories recursively. While it does not handle file writing directly, it can be used in conjunction with the native fs module to ensure the directory structure exists before writing files.
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!'
This module is very similar to fs-extra's fs.outputFileSync
method, but different in the following points:
const {statSync} = require('fs');
const outputFileSync = require('output-file-sync');
outputFileSync('foo/bar', 'content', {mode: 33260});
statSync('foo').mode; //=> 33260
npm install output-file-sync
const outputFileSync = require('output-file-sync');
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
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.
Set the mode of a file, overriding mode
option.
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'
ISC License © 2017 - 2018 Shinnosuke Watanabe
FAQs
Synchronously write a file and create its ancestor directories if needed
We found that output-file-sync demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.