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 | atomic: ["boolean"],
|
19 | jsonIndent: ["number"]
|
20 | });
|
21 | };
|
22 |
|
23 |
|
24 | const newExt = ".__new__";
|
25 |
|
26 | const serializeToJsonMaybe = (data, jsonIndent) => {
|
27 | let indent = jsonIndent;
|
28 | if (typeof indent !== "number") {
|
29 | indent = 2;
|
30 | }
|
31 |
|
32 | if (typeof data === "object" && !Buffer.isBuffer(data) && data !== null) {
|
33 | return JSON.stringify(data, null, indent);
|
34 | }
|
35 |
|
36 | return data;
|
37 | };
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | const writeFileSync = (path, data, options) => {
|
44 | try {
|
45 | fs.writeFileSync(path, data, options);
|
46 | } catch (err) {
|
47 | if (err.code === "ENOENT") {
|
48 |
|
49 | dir.createSync(pathUtil.dirname(path));
|
50 | fs.writeFileSync(path, data, options);
|
51 | } else {
|
52 | throw err;
|
53 | }
|
54 | }
|
55 | };
|
56 |
|
57 | const writeAtomicSync = (path, data, options) => {
|
58 |
|
59 |
|
60 |
|
61 | writeFileSync(path + newExt, data, options);
|
62 |
|
63 | fs.renameSync(path + newExt, path);
|
64 | };
|
65 |
|
66 | const writeSync = (path, data, options) => {
|
67 | const opts = options || {};
|
68 | const processedData = serializeToJsonMaybe(data, opts.jsonIndent);
|
69 |
|
70 | let writeStrategy = writeFileSync;
|
71 | if (opts.atomic) {
|
72 | writeStrategy = writeAtomicSync;
|
73 | }
|
74 | writeStrategy(path, processedData, { mode: opts.mode });
|
75 | };
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | const writeFileAsync = (path, data, options) => {
|
82 | return new Promise((resolve, reject) => {
|
83 | fs.writeFile(path, data, options)
|
84 | .then(resolve)
|
85 | .catch(err => {
|
86 |
|
87 |
|
88 | if (err.code === "ENOENT") {
|
89 |
|
90 | dir
|
91 | .createAsync(pathUtil.dirname(path))
|
92 | .then(() => {
|
93 | return fs.writeFile(path, data, options);
|
94 | })
|
95 | .then(resolve, reject);
|
96 | } else {
|
97 |
|
98 | reject(err);
|
99 | }
|
100 | });
|
101 | });
|
102 | };
|
103 |
|
104 | const writeAtomicAsync = (path, data, options) => {
|
105 | return new Promise((resolve, reject) => {
|
106 |
|
107 |
|
108 |
|
109 | writeFileAsync(path + newExt, data, options)
|
110 | .then(() => {
|
111 |
|
112 | return fs.rename(path + newExt, path);
|
113 | })
|
114 | .then(resolve, reject);
|
115 | });
|
116 | };
|
117 |
|
118 | const writeAsync = (path, data, options) => {
|
119 | const opts = options || {};
|
120 | const processedData = serializeToJsonMaybe(data, opts.jsonIndent);
|
121 |
|
122 | let writeStrategy = writeFileAsync;
|
123 | if (opts.atomic) {
|
124 | writeStrategy = writeAtomicAsync;
|
125 | }
|
126 | return writeStrategy(path, processedData, { mode: opts.mode });
|
127 | };
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | exports.validateInput = validateInput;
|
134 | exports.sync = writeSync;
|
135 | exports.async = writeAsync;
|