1 | "use strict";
|
2 |
|
3 | const pathUtil = require("path");
|
4 | const fs = require("./utils/fs");
|
5 | const validate = require("./utils/validate");
|
6 | const dir = require("./dir");
|
7 |
|
8 | const 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 |
|
25 | const newExt = ".__new__";
|
26 |
|
27 | const 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 |
|
42 |
|
43 |
|
44 | const writeFileSync = (path, data, options) => {
|
45 | try {
|
46 | fs.writeFileSync(path, data, options);
|
47 | } catch (err) {
|
48 | if (err.code === "ENOENT") {
|
49 |
|
50 | dir.createSync(pathUtil.dirname(path));
|
51 | fs.writeFileSync(path, data, options);
|
52 | } else {
|
53 | throw err;
|
54 | }
|
55 | }
|
56 | };
|
57 |
|
58 | const writeAtomicSync = (path, data, options) => {
|
59 |
|
60 |
|
61 |
|
62 | writeFileSync(path + newExt, data, options);
|
63 |
|
64 | fs.renameSync(path + newExt, path);
|
65 | };
|
66 |
|
67 | const 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 |
|
80 |
|
81 |
|
82 | const writeFileAsync = (path, data, options) => {
|
83 | return new Promise((resolve, reject) => {
|
84 | fs.writeFile(path, data, options)
|
85 | .then(resolve)
|
86 | .catch(err => {
|
87 |
|
88 |
|
89 | if (err.code === "ENOENT") {
|
90 |
|
91 | dir
|
92 | .createAsync(pathUtil.dirname(path))
|
93 | .then(() => {
|
94 | return fs.writeFile(path, data, options);
|
95 | })
|
96 | .then(resolve, reject);
|
97 | } else {
|
98 |
|
99 | reject(err);
|
100 | }
|
101 | });
|
102 | });
|
103 | };
|
104 |
|
105 | const writeAtomicAsync = (path, data, options) => {
|
106 | return new Promise((resolve, reject) => {
|
107 |
|
108 |
|
109 |
|
110 | writeFileAsync(path + newExt, data, options)
|
111 | .then(() => {
|
112 |
|
113 | return fs.rename(path + newExt, path);
|
114 | })
|
115 | .then(resolve, reject);
|
116 | });
|
117 | };
|
118 |
|
119 | const 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 |
|
132 |
|
133 |
|
134 | exports.validateInput = validateInput;
|
135 | exports.sync = writeSync;
|
136 | exports.async = writeAsync;
|