write-file-atomically
DefinitelyTyped icon, indicating that this package has TypeScript declarations provided by the separate @types/write-file-atomically package

2.0.0 • Public • Published

write-file-atomically

npm version Build Status Coverage Status

Promisified version of write-file-atomic:

an extension for node's fs.writeFile that makes its operation atomic and allows you set ownership (uid/gid of the file)

const {readFileSync} = require('fs');
const writeFileAtomically = require('write-file-atomically');
 
(async () => {
  await writeFileAtomically('file.txt', 'Hi!');
  readFileSync('file.txt', 'utf8'); //=> 'Hi!'
})();

Installation

Use npm.

npm install write-file-atomically

API

const writeFileAtomically = require('write-file-atomically');

writeFileAtomically(filename, data [, options])

filename: string, Buffer or URL (a file path where the file to be written)
data: string or Buffer (file contents)
options: Object or string (directly used as write-file-atomic options)
Return: Promise

It asynchronously writes data to the given file path in an atomic manner:

The file is initially named filename + "." + murmurhex(__filename, process.pid, ++invocations). If writeFile completes successfully then, if passed the chown option it will change the ownership of the file. Finally it renames the file back to the filename you specified.

License

ISC License © 2018 Shinnosuke Watanabe

Package Sidebar

Install

npm i write-file-atomically

Weekly Downloads

59

Version

2.0.0

License

ISC

Unpacked Size

6.67 kB

Total Files

4

Last publish

Collaborators

  • shinnn