1write-file-atomic
2-----------------
3
4This is an extension for node's `fs.writeFile` that makes its operation
5atomic and allows you set ownership (uid/gid of the file).
6
7### var writeFileAtomic = require('write-file-atomic')<br>writeFileAtomic(filename, data, [options], [callback])
8
9* filename **String**
10* data **String** | **Buffer**
11* options **Object** | **String**
12  * chown **Object** default, uid & gid of existing file, if any
13    * uid **Number**
14    * gid **Number**
15  * encoding **String** | **Null** default = 'utf8'
16  * fsync **Boolean** default = true
17  * mode **Number** default, from existing file, if any
18  * tmpfileCreated **Function** called when the tmpfile is created
19* callback **Function**
20
21Atomically and asynchronously writes data to a file, replacing the file if it already
22exists.  data can be a string or a buffer.
23
24The file is initially named `filename + "." + murmurhex(__filename, process.pid, ++invocations)`.
25Note that `require('worker_threads').threadId` is used in addition to `process.pid` if running inside of a worker thread.
26If writeFile completes successfully then, if passed the **chown** option it will change
27the ownership of the file. Finally it renames the file back to the filename you specified. If
28it encounters errors at any of these steps it will attempt to unlink the temporary file and then
29pass the error back to the caller.
30If multiple writes are concurrently issued to the same file, the write operations are put into a queue and serialized in the order they were called, using Promises. Writes to different files are still executed in parallel.
31
32If provided, the **chown** option requires both **uid** and **gid** properties or else
33you'll get an error.  If **chown** is not specified it will default to using
34the owner of the previous file.  To prevent chown from being ran you can
35also pass `false`, in which case the file will be created with the current user's credentials.
36
37If **mode** is not specified, it will default to using the permissions from
38an existing file, if any.  Expicitly setting this to `false` remove this default, resulting
39in a file created with the system default permissions.
40
41If options is a String, it's assumed to be the **encoding** option. The **encoding** option is ignored if **data** is a buffer. It defaults to 'utf8'.
42
43If the **fsync** option is **false**, writeFile will skip the final fsync call.
44
45If the **tmpfileCreated** option is specified it will be called with the name of the tmpfile when created.
46
47Example:
48
49```javascript
50writeFileAtomic('message.txt', 'Hello Node', {chown:{uid:100,gid:50}}, function (err) {
51  if (err) throw err;
52  console.log('It\'s saved!');
53});
54```
55
56This function also supports async/await:
57
58```javascript
59(async () => {
60  try {
61    await writeFileAtomic('message.txt', 'Hello Node', {chown:{uid:100,gid:50}});
62    console.log('It\'s saved!');
63  } catch (err) {
64    console.error(err);
65    process.exit(1);
66  }
67})();
68```
69
70### var writeFileAtomicSync = require('write-file-atomic').sync<br>writeFileAtomicSync(filename, data, [options])
71
72The synchronous version of **writeFileAtomic**.
73