UNPKG

4.07 kBJavaScriptView Raw
1"use strict";
2
3const pathUtil = require("path");
4const fs = require("./utils/fs");
5const validate = require("./utils/validate");
6const dir = require("./dir");
7
8const validateInput = (methodName, path, data, options) => {
9 const methodSignature = `${methodName}(path, data, [options])`;
10 validate.argument(methodSignature, "path", path, ["string"]);
11 validate.argument(methodSignature, "data", data, [
12 "string",
13 "buffer",
14 "object",
15 "array"
16 ]);
17 validate.options(methodSignature, "options", options, {
18 mode: ["string", "number"],
19 atomic: ["boolean"],
20 jsonIndent: ["number"]
21 });
22};
23
24// Temporary file extensions used for atomic file overwriting.
25const newExt = ".__new__";
26
27const serializeToJsonMaybe = (data, jsonIndent) => {
28 let indent = jsonIndent;
29 if (typeof indent !== "number") {
30 indent = 2;
31 }
32
33 if (typeof data === "object" && !Buffer.isBuffer(data) && data !== null) {
34 return JSON.stringify(data, null, indent);
35 }
36
37 return data;
38};
39
40// ---------------------------------------------------------
41// SYNC
42// ---------------------------------------------------------
43
44const writeFileSync = (path, data, options) => {
45 try {
46 fs.writeFileSync(path, data, options);
47 } catch (err) {
48 if (err.code === "ENOENT") {
49 // Means parent directory doesn't exist, so create it and try again.
50 dir.createSync(pathUtil.dirname(path));
51 fs.writeFileSync(path, data, options);
52 } else {
53 throw err;
54 }
55 }
56};
57
58const writeAtomicSync = (path, data, options) => {
59 // we are assuming there is file on given path, and we don't want
60 // to touch it until we are sure our data has been saved correctly,
61 // so write the data into temporary file...
62 writeFileSync(path + newExt, data, options);
63 // ...next rename temp file to replace real path.
64 fs.renameSync(path + newExt, path);
65};
66
67const writeSync = (path, data, options) => {
68 const opts = options || {};
69 const processedData = serializeToJsonMaybe(data, opts.jsonIndent);
70
71 let writeStrategy = writeFileSync;
72 if (opts.atomic) {
73 writeStrategy = writeAtomicSync;
74 }
75 writeStrategy(path, processedData, { mode: opts.mode });
76};
77
78// ---------------------------------------------------------
79// ASYNC
80// ---------------------------------------------------------
81
82const writeFileAsync = (path, data, options) => {
83 return new Promise((resolve, reject) => {
84 fs.writeFile(path, data, options)
85 .then(resolve)
86 .catch(err => {
87 // First attempt to write a file ended with error.
88 // Check if this is not due to nonexistent parent directory.
89 if (err.code === "ENOENT") {
90 // Parent directory doesn't exist, so create it and try again.
91 dir
92 .createAsync(pathUtil.dirname(path))
93 .then(() => {
94 return fs.writeFile(path, data, options);
95 })
96 .then(resolve, reject);
97 } else {
98 // Nope, some other error, throw it.
99 reject(err);
100 }
101 });
102 });
103};
104
105const writeAtomicAsync = (path, data, options) => {
106 return new Promise((resolve, reject) => {
107 // We are assuming there is file on given path, and we don't want
108 // to touch it until we are sure our data has been saved correctly,
109 // so write the data into temporary file...
110 writeFileAsync(path + newExt, data, options)
111 .then(() => {
112 // ...next rename temp file to real path.
113 return fs.rename(path + newExt, path);
114 })
115 .then(resolve, reject);
116 });
117};
118
119const writeAsync = (path, data, options) => {
120 const opts = options || {};
121 const processedData = serializeToJsonMaybe(data, opts.jsonIndent);
122
123 let writeStrategy = writeFileAsync;
124 if (opts.atomic) {
125 writeStrategy = writeAtomicAsync;
126 }
127 return writeStrategy(path, processedData, { mode: opts.mode });
128};
129
130// ---------------------------------------------------------
131// API
132// ---------------------------------------------------------
133
134exports.validateInput = validateInput;
135exports.sync = writeSync;
136exports.async = writeAsync;