@zwa73/utils 1.0.214 → 1.0.215
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/UtilFileTools.js +38 -26
- package/package.json +1 -1
package/dist/UtilFileTools.js
CHANGED
|
@@ -142,37 +142,49 @@ var UtilFT;
|
|
|
142
142
|
}
|
|
143
143
|
UtilFT.ensurePathExistsSync = ensurePathExistsSync;
|
|
144
144
|
function loadJSONFileSync(filePath, opt) {
|
|
145
|
-
|
|
146
|
-
filePath
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
if (
|
|
153
|
-
|
|
154
|
-
|
|
145
|
+
try {
|
|
146
|
+
if (opt?.forceExt !== true && pathe_1.default.extname(filePath) !== '.json')
|
|
147
|
+
filePath += '.json';
|
|
148
|
+
const str = pathExistsSync(filePath)
|
|
149
|
+
? fs.readFileSync(filePath, "utf-8")
|
|
150
|
+
: "";
|
|
151
|
+
// 如果不存在则返回默认值
|
|
152
|
+
if (str == "" || str == null) {
|
|
153
|
+
if (opt?.default !== undefined)
|
|
154
|
+
return opt.default;
|
|
155
|
+
return {};
|
|
156
|
+
}
|
|
157
|
+
if (opt?.json5)
|
|
158
|
+
return JSON5.parse(str);
|
|
159
|
+
return JSON.parse(str);
|
|
160
|
+
}
|
|
161
|
+
catch (e) {
|
|
162
|
+
UtilLogger_1.SLogger.error(`loadJSONFileSync 错误 filePath:${filePath}`);
|
|
163
|
+
throw e;
|
|
155
164
|
}
|
|
156
|
-
if (opt?.json5)
|
|
157
|
-
return JSON5.parse(str);
|
|
158
|
-
return JSON.parse(str);
|
|
159
165
|
}
|
|
160
166
|
UtilFT.loadJSONFileSync = loadJSONFileSync;
|
|
161
167
|
async function loadJSONFile(filePath, opt) {
|
|
162
|
-
|
|
163
|
-
filePath
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
if (
|
|
170
|
-
|
|
171
|
-
|
|
168
|
+
try {
|
|
169
|
+
if (opt?.forceExt !== true && pathe_1.default.extname(filePath) !== '.json')
|
|
170
|
+
filePath += '.json';
|
|
171
|
+
const str = (await pathExists(filePath))
|
|
172
|
+
? await fs.promises.readFile(filePath, "utf-8")
|
|
173
|
+
: "";
|
|
174
|
+
// 如果不存在则返回默认值
|
|
175
|
+
if (str == "" || str == null) {
|
|
176
|
+
if (opt?.default !== undefined)
|
|
177
|
+
return opt.default;
|
|
178
|
+
return {};
|
|
179
|
+
}
|
|
180
|
+
if (opt?.json5)
|
|
181
|
+
return JSON5.parse(str);
|
|
182
|
+
return JSON.parse(str);
|
|
183
|
+
}
|
|
184
|
+
catch (e) {
|
|
185
|
+
UtilLogger_1.SLogger.error(`loadJSONFile 错误 filePath:${filePath}`);
|
|
186
|
+
throw e;
|
|
172
187
|
}
|
|
173
|
-
if (opt?.json5)
|
|
174
|
-
return JSON5.parse(str);
|
|
175
|
-
return JSON.parse(str);
|
|
176
188
|
}
|
|
177
189
|
UtilFT.loadJSONFile = loadJSONFile;
|
|
178
190
|
/**写入JSON文件
|