write-file-atomically
v2.0.0
Published
Promisified version of write-file-atomic
Downloads
903
Maintainers
Readme
write-file-atomically
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
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