@nsnanocat/util 2.5.15 → 2.5.17

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/getStorage.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import "./lib/argument.mjs";
2
2
  import { Console } from "./polyfill/Console.mjs";
3
3
  import { Lodash as _ } from "./polyfill/Lodash.mjs";
4
- import { Storage } from "./polyfill/Storage.js";
4
+ import { Storage } from "./polyfill/Storage.mjs";
5
5
 
6
6
  /**
7
7
  * 存储配置读取与合并结果。
@@ -5,11 +5,11 @@ export * from "./lib/notification.mjs";
5
5
  export * from "./lib/time.mjs";
6
6
  export * from "./lib/wait.mjs";
7
7
  export * from "./polyfill/Console.mjs";
8
- export * from "./polyfill/fetch.js";
8
+ export * from "./polyfill/fetch.mjs";
9
9
  export * from "./polyfill/Lodash.mjs";
10
10
  export * from "./polyfill/qs.mjs";
11
11
  export * from "./polyfill/StatusTexts.mjs";
12
- export * from "./polyfill/Storage.js";
12
+ export * from "./polyfill/Storage.mjs";
13
13
 
14
14
  /**
15
15
  * 已标准化的 `$argument` 快照。
package/package.json CHANGED
@@ -13,8 +13,9 @@
13
13
  ],
14
14
  "license": "Apache-2.0",
15
15
  "bugs": "https://github.com/NSNanoCat/util/issues",
16
- "main": "index.mjs",
16
+ "main": "index.js",
17
17
  "types": "types/nsnanocat-util.d.ts",
18
+ "type": "module",
18
19
  "scripts": {
19
20
  "tsc:build": "npx tsc",
20
21
  "test": "node --test test/*.test.js",
@@ -27,7 +28,7 @@
27
28
  "url": "git+https://github.com/NSNanoCat/util.git"
28
29
  },
29
30
  "files": [
30
- "index.mjs",
31
+ "index.js",
31
32
  "lib",
32
33
  "polyfill",
33
34
  "getStorage.mjs",
@@ -41,5 +42,5 @@
41
42
  "registry": "https://registry.npmjs.org/",
42
43
  "access": "public"
43
44
  },
44
- "version": "2.5.15"
45
+ "version": "2.5.17"
45
46
  }
@@ -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(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
- }
287
+ static #loaddata = dataFile => {
288
+ if ($app === "Node.js") {
289
+ this.fs = this.fs ? this.fs : require("node:fs");
290
+ this.path = this.path ? this.path : require("node:path");
291
+ const curDirDataFilePath = this.path.resolve(dataFile);
292
+ const rootDirDataFilePath = this.path.resolve(process.cwd(), dataFile);
293
+ const isCurDirDataFile = this.fs.existsSync(curDirDataFilePath);
294
+ const isRootDirDataFile = !isCurDirDataFile && this.fs.existsSync(rootDirDataFilePath);
295
+ if (isCurDirDataFile || isRootDirDataFile) {
296
+ const datPath = isCurDirDataFile ? curDirDataFilePath : rootDirDataFilePath;
297
+ try {
298
+ return JSON.parse(this.fs.readFileSync(datPath));
299
+ } catch (e) {
300
+ return {};
301
+ }
302
+ } else return {};
303
+ } else return {};
304
+ };
305
305
 
306
306
  /**
307
307
  * 将内存数据写入 Node.js 数据文件。
@@ -311,15 +311,22 @@ export class Storage {
311
311
  * @param {string} [dataFile=this.dataFile] 数据文件名 / Data file name.
312
312
  * @returns {void}
313
313
  */
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
- }
314
+ static #writedata = (dataFile = this.dataFile) => {
315
+ if ($app === "Node.js") {
316
+ this.fs = this.fs ? this.fs : require("node:fs");
317
+ this.path = this.path ? this.path : require("node:path");
318
+ const curDirDataFilePath = this.path.resolve(dataFile);
319
+ const rootDirDataFilePath = this.path.resolve(process.cwd(), dataFile);
320
+ const isCurDirDataFile = this.fs.existsSync(curDirDataFilePath);
321
+ const isRootDirDataFile = !isCurDirDataFile && this.fs.existsSync(rootDirDataFilePath);
322
+ const jsondata = JSON.stringify(this.data);
323
+ if (isCurDirDataFile) {
324
+ this.fs.writeFileSync(curDirDataFilePath, jsondata);
325
+ } else if (isRootDirDataFile) {
326
+ this.fs.writeFileSync(rootDirDataFilePath, jsondata);
327
+ } else {
328
+ this.fs.writeFileSync(curDirDataFilePath, jsondata);
329
+ }
330
+ }
331
+ };
325
332
  }
@@ -0,0 +1,6 @@
1
+ import { fetch as fetchRuntime } from "./fetch.mjs";
2
+ import type { Fetch } from "./fetch.d.ts";
3
+
4
+ export type { Fetch, FetchRequest, FetchResponse } from "./fetch.d.ts";
5
+
6
+ export const fetch: Fetch = fetchRuntime as Fetch;
@@ -3,9 +3,9 @@
3
3
  * Aggregated exports for polyfill modules.
4
4
  */
5
5
  export { Console } from "./Console.mjs";
6
- export { fetch } from "./fetch.js";
7
- export type { Fetch, FetchRequest, FetchResponse } from "./fetch.js";
6
+ export { fetch } from "./fetch.mjs";
7
+ export type { Fetch, FetchRequest, FetchResponse } from "./fetch.mjs";
8
8
  export { Lodash } from "./Lodash.mjs";
9
9
  export { qs } from "./qs.mjs";
10
10
  export { StatusTexts } from "./StatusTexts.mjs";
11
- export { Storage } from "./Storage.js";
11
+ export { Storage } from "./Storage.mjs";
package/polyfill/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  export * from "./Console.mjs";
2
- export * from "./fetch.js";
2
+ export * from "./fetch.mjs";
3
3
  export * from "./Lodash.mjs";
4
4
  export * from "./qs.mjs";
5
5
  export * from "./StatusTexts.mjs";
6
- export * from "./Storage.js";
6
+ export * from "./Storage.mjs";
File without changes
File without changes