@nsnanocat/util 2.5.13 → 2.5.15
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/package.json +1 -1
- package/polyfill/Storage.js +29 -36
package/package.json
CHANGED
package/polyfill/Storage.js
CHANGED
|
@@ -284,24 +284,24 @@ export class Storage {
|
|
|
284
284
|
* @param {string} dataFile 数据文件名 / Data file name.
|
|
285
285
|
* @returns {Record<string, any>}
|
|
286
286
|
*/
|
|
287
|
-
static #loaddata
|
|
288
|
-
if ($app
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
}
|
|
304
|
-
}
|
|
287
|
+
static #loaddata(dataFile) {
|
|
288
|
+
if ($app !== "Node.js") return {};
|
|
289
|
+
// 优先复用全局运行时对象,不存在时再按需加载。
|
|
290
|
+
// Prefer reusing global runtime modules and lazily require when missing.
|
|
291
|
+
// eslint-disable-next-line n/no-missing-require, n/prefer-node-protocol
|
|
292
|
+
if (!globalThis.fs) globalThis.fs = require("fs");
|
|
293
|
+
// eslint-disable-next-line n/no-missing-require, n/prefer-node-protocol
|
|
294
|
+
if (!globalThis.path) globalThis.path = require("path");
|
|
295
|
+
const dataFilePathCandidates = [...new Set([globalThis.path.resolve(dataFile), globalThis.path.resolve(process.cwd(), dataFile)])];
|
|
296
|
+
const dataFilePath = dataFilePathCandidates.find(filePath => globalThis.fs.existsSync(filePath));
|
|
297
|
+
if (!dataFilePath) return {};
|
|
298
|
+
try {
|
|
299
|
+
const jsonData = globalThis.fs.readFileSync(dataFilePath, "utf8");
|
|
300
|
+
return jsonData ? JSON.parse(jsonData) : {};
|
|
301
|
+
} catch (e) {
|
|
302
|
+
return {};
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
305
|
|
|
306
306
|
/**
|
|
307
307
|
* 将内存数据写入 Node.js 数据文件。
|
|
@@ -311,22 +311,15 @@ export class Storage {
|
|
|
311
311
|
* @param {string} [dataFile=this.dataFile] 数据文件名 / Data file name.
|
|
312
312
|
* @returns {void}
|
|
313
313
|
*/
|
|
314
|
-
static #writedata
|
|
315
|
-
if ($app
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
} else if (isRootDirDataFile) {
|
|
326
|
-
this.fs.writeFileSync(rootDirDataFilePath, jsondata);
|
|
327
|
-
} else {
|
|
328
|
-
this.fs.writeFileSync(curDirDataFilePath, jsondata);
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
};
|
|
314
|
+
static #writedata(dataFile = this.dataFile) {
|
|
315
|
+
if ($app !== "Node.js") return;
|
|
316
|
+
// 优先复用全局运行时对象,不存在时再按需加载。
|
|
317
|
+
// Prefer reusing global runtime modules and lazily require when missing.
|
|
318
|
+
if (!globalThis.fs) globalThis.fs = require("fs");
|
|
319
|
+
if (!globalThis.path) globalThis.path = require("path");
|
|
320
|
+
const dataFilePathCandidates = [...new Set([globalThis.path.resolve(dataFile), globalThis.path.resolve(process.cwd(), dataFile)])];
|
|
321
|
+
const dataFilePath = dataFilePathCandidates.find(filePath => globalThis.fs.existsSync(filePath)) ?? dataFilePathCandidates[0];
|
|
322
|
+
const jsonData = JSON.stringify(this.data);
|
|
323
|
+
globalThis.fs.writeFileSync(dataFilePath, jsonData);
|
|
324
|
+
}
|
|
332
325
|
}
|