Skip to content

Promisified version of write-file-atomic

License

Notifications You must be signed in to change notification settings

shinnn/write-file-atomically

Repository files navigation

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