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!'
})();
npm install write-file-atomically
const writeFileAtomically = require('write-file-atomically');
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.
ISC License © 2018 Shinnosuke Watanabe