@zwa73/utils 1.0.235 → 1.0.240

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.
@@ -1,3 +1,3 @@
1
1
  export * from './Hbs';
2
- export type { PromoseQueueOption, DListMiddleNode, DListHeadNode, DListTailNode, DListNode, DListMaybeNode, BridgeInterface } from "@zwa73/js-utils";
3
- export { Stream, Spool, SmartCache, PromiseQueue, Piper, DLinkedList, Bridge } from "@zwa73/js-utils";
2
+ export type { PromoseQueueOption, EventData, DListMiddleNode, DListHeadNode, DListTailNode, DListNode, DListMaybeNode, BridgeInterface } from "@zwa73/js-utils";
3
+ export { Stream, Spool, SmartCache, PromiseQueue, Piper, EventSystem, DLinkedList, Bridge } from "@zwa73/js-utils";
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.Bridge = exports.DLinkedList = exports.Piper = exports.PromiseQueue = exports.SmartCache = exports.Spool = exports.Stream = void 0;
17
+ exports.Bridge = exports.DLinkedList = exports.EventSystem = exports.Piper = exports.PromiseQueue = exports.SmartCache = exports.Spool = exports.Stream = void 0;
18
18
  __exportStar(require("./Hbs"), exports);
19
19
  var js_utils_1 = require("@zwa73/js-utils");
20
20
  Object.defineProperty(exports, "Stream", { enumerable: true, get: function () { return js_utils_1.Stream; } });
@@ -22,6 +22,7 @@ Object.defineProperty(exports, "Spool", { enumerable: true, get: function () { r
22
22
  Object.defineProperty(exports, "SmartCache", { enumerable: true, get: function () { return js_utils_1.SmartCache; } });
23
23
  Object.defineProperty(exports, "PromiseQueue", { enumerable: true, get: function () { return js_utils_1.PromiseQueue; } });
24
24
  Object.defineProperty(exports, "Piper", { enumerable: true, get: function () { return js_utils_1.Piper; } });
25
+ Object.defineProperty(exports, "EventSystem", { enumerable: true, get: function () { return js_utils_1.EventSystem; } });
25
26
  Object.defineProperty(exports, "DLinkedList", { enumerable: true, get: function () { return js_utils_1.DLinkedList; } });
26
27
  Object.defineProperty(exports, "Bridge", { enumerable: true, get: function () { return js_utils_1.Bridge; } });
27
28
  //#endregion
@@ -47,6 +47,11 @@ type WriteJsonFileOpt = Partial<{
47
47
  compressThreshold: number;
48
48
  /**不自动修改扩展名为json */
49
49
  forceExt: boolean;
50
+ /**检查重复, 避免重复写入
51
+ * 只在第一次尝试写入时读取一次文件内容, 后续将会使用缓存判断是否重复
52
+ * 若是其他进程改动了文件内容, 将无法检测到
53
+ */
54
+ checkDuplicate: boolean;
50
55
  }>;
51
56
  /**文件工具 */
52
57
  export declare namespace UtilFT {
@@ -129,8 +134,10 @@ export declare namespace UtilFT {
129
134
  * @param opt.compress - 使用紧凑风格 将会用/\uF121\uF122\uF123.+\uF121\uF122\uF123/作为特殊标记, 原始文本内若出现相同格式将会产生错误
130
135
  * @param opt.forceExt - 不自动修改扩展名为json
131
136
  * @param opt.compressThreshold - 紧凑风格 压缩阈值 默认100
137
+ * @param opt.checkDuplicate - 检查重复输出 避免重复输出
138
+ * @returns 是否完成写入, 出错或检出重复均会返回false
132
139
  */
133
- function writeJSONFile(filePath: string, token: JToken, opt?: WriteJsonFileOpt): Promise<void>;
140
+ function writeJSONFile(filePath: string, token: JToken, opt?: WriteJsonFileOpt): Promise<boolean>;
134
141
  /**保证路径为某个风格
135
142
  * @param filePath - 输入路径
136
143
  * @param style - 目标风格 undefined 时不改变
@@ -188,6 +188,7 @@ var UtilFT;
188
188
  }
189
189
  }
190
190
  UtilFT.loadJSONFile = loadJSONFile;
191
+ const writeJSONFileDupMap = {};
191
192
  /**写入JSON文件
192
193
  * @async
193
194
  * @param filePath - 文件路径
@@ -196,6 +197,8 @@ var UtilFT;
196
197
  * @param opt.compress - 使用紧凑风格 将会用/\uF121\uF122\uF123.+\uF121\uF122\uF123/作为特殊标记, 原始文本内若出现相同格式将会产生错误
197
198
  * @param opt.forceExt - 不自动修改扩展名为json
198
199
  * @param opt.compressThreshold - 紧凑风格 压缩阈值 默认100
200
+ * @param opt.checkDuplicate - 检查重复输出 避免重复输出
201
+ * @returns 是否完成写入, 出错或检出重复均会返回false
199
202
  */
200
203
  async function writeJSONFile(filePath, token, opt) {
201
204
  const str = UtilFunctions_1.UtilFunc.stringifyJToken(token, opt);
@@ -204,13 +207,24 @@ var UtilFT;
204
207
  // 判断文件路径是否存在 不存在则创建
205
208
  if (!(await pathExists(filePath)))
206
209
  await createPath(filePath);
210
+ // 检查内容是否重复
211
+ if (opt?.checkDuplicate) {
212
+ if (writeJSONFileDupMap[filePath] === undefined)
213
+ writeJSONFileDupMap[filePath] = await fs.promises.readFile(filePath, 'utf-8');
214
+ if (writeJSONFileDupMap[filePath] == str)
215
+ return false;
216
+ else
217
+ writeJSONFileDupMap[filePath] = str;
218
+ }
207
219
  // 写入文件
208
220
  try {
209
221
  await fs.promises.writeFile(filePath, str);
210
222
  UtilLogger_1.SLogger.verbose(`${filePath} writeJSONFile 成功`);
223
+ return true;
211
224
  }
212
225
  catch (err) {
213
226
  UtilLogger_1.SLogger.error(`${filePath} writeJSONFile 错误`, err);
227
+ return false;
214
228
  }
215
229
  }
216
230
  UtilFT.writeJSONFile = writeJSONFile;
@@ -11,6 +11,8 @@ type ExecOpt = Partial<{
11
11
  nodeModules: string;
12
12
  /**工作路径 */
13
13
  cwd: string;
14
+ /**额外环境变量 */
15
+ env: string;
14
16
  }>;
15
17
  /**常用函数 */
16
18
  declare class _UtilFunc {
@@ -34,6 +36,7 @@ declare class _UtilFunc {
34
36
  * @param opt.errlvl - 错误的日志等级
35
37
  * @param opt.nodeModules - nodeModules文件夹路径
36
38
  * @param opt.cwd - 执行目录
39
+ * @param opt.env - 额外环境变量
37
40
  */
38
41
  static exec(command: string, opt?: ExecOpt): Promise<{
39
42
  stdout: string;
@@ -42,5 +45,5 @@ declare class _UtilFunc {
42
45
  /**获取当前公网ipv4 */
43
46
  static getPublicIpv4(): Promise<string>;
44
47
  }
45
- export declare const UtilFunc: import("@zwa73/modular-mixer").ComposedClass<typeof _UtilFunc, typeof JsFunc, "__jsUtils", "match" | "failed" | "success" | "prototype" | "genUUID" | "range" | "getTime" | "initField" | "initObject" | "afterward" | "sleep" | "getNeverResolvedPromise" | "retryPromise" | "timelimitPromise" | "mapEntries" | "eachField" | "stringifyJToken" | "assertType" | "assertLiteral" | "deepClone" | "isSafeNumber" | "dedent" | "throwError" | "getFuncLoc" | "cachePool" | "memoize" | "asyncize" | "lazyFunction" | "splitToChunk" | "mergeFromChunk" | "structEqual" | "concurrent" | "dynamicImport" | "outcome" | "isStatus" | "eitherize" | "parseSrtTime" | "formatSrtTime" | "parseSrt" | "createSrt" | "ivk" | "s2l" | "l2s">;
48
+ export declare const UtilFunc: import("@zwa73/modular-mixer").ComposedClass<typeof _UtilFunc, typeof JsFunc, "__jsUtils", "match" | "failed" | "success" | "prototype" | "genUUID" | "range" | "getTime" | "initField" | "initObject" | "afterward" | "sleep" | "getNeverResolvedPromise" | "retryPromise" | "timelimitPromise" | "mapEntries" | "eachField" | "stringifyJToken" | "assertType" | "assertLiteral" | "deepClone" | "isSafeNumber" | "dedent" | "throwError" | "getFuncLoc" | "cachePool" | "memoize" | "asyncize" | "lazyFunction" | "splitToChunk" | "mergeFromChunk" | "structEqual" | "concurrent" | "dynamicImport" | "createInjectable" | "outcome" | "isStatus" | "eitherize" | "parseSrtTime" | "formatSrtTime" | "parseSrt" | "createSrt" | "ivk" | "s2l" | "l2s">;
46
49
  export type UtilFunc = typeof UtilFunc;
@@ -72,11 +72,12 @@ class _UtilFunc {
72
72
  * @param opt.errlvl - 错误的日志等级
73
73
  * @param opt.nodeModules - nodeModules文件夹路径
74
74
  * @param opt.cwd - 执行目录
75
+ * @param opt.env - 额外环境变量
75
76
  */
76
77
  static async exec(command, opt) {
77
78
  return new Promise(async (resolve, reject) => {
78
79
  // 创建一个新的环境变量对象,并将项目的 node_modules/.bin 目录添加到 PATH 环境变量中
79
- const env = Object.create(process.env);
80
+ const env = Object.assign(Object.create(process.env), opt?.env ?? {});
80
81
  const penv = opt?.nodeModules
81
82
  ? path_1.default.join(UtilFileTools_1.UtilFT.currosizePath(opt.nodeModules), '.bin')
82
83
  : UtilFileTools_1.UtilFT.currosizePath(await exports.UtilFunc.memoize(UtilFileTools_1.UtilFT.findNodeModulesDir)(process.cwd(), '.bin') ?? '');
@@ -1 +1 @@
1
- export type { JToken, JValue, JArray, JObject, IJData, AnyFunc, Keyable, PartialOption, ProperSubset, ProperSubsetCheck, IncludeCheck, UnionCheck, Literal, LiteralCheck, AllExtends, AssignObject, Writeable, Inverted, FixedLengthTuple, Increment, RangeTuple, AnyString, UnionToIntersection, ExclusiveRecord, ExclusiveJObject, PromiseStatus, StatusVerifyFn, Await, FuncPropNames, ExtendThen, RequiredOnly, WithPrefix, Outcome, Either, Matchable, MatchableFlag, ExtractOutcome, ExtractMatchable, NeedInit, PRecord, MPromise, SPromise, SMPromise, NotPromise, AnyRecord, MReturnType, CmtTuple, Flasy, SrtSegment, ILogger, LogLevel, Asyncize } from "@zwa73/js-utils";
1
+ export type { JToken, JValue, JArray, JObject, IJData, AnyFunc, Keyable, DeepReadonly, PartialOption, ProperSubset, ProperSubsetCheck, IncludeCheck, UnionCheck, Literal, LiteralCheck, AllExtends, AssignObject, Writeable, Inverted, FixedLengthTuple, Increment, RangeTuple, AnyString, UnionToIntersection, ExclusiveRecord, ExclusiveJObject, PromiseStatus, StatusVerifyFn, Await, FuncPropNames, ExtendThen, RequiredOnly, WithPrefix, Outcome, Either, Matchable, MatchableFlag, ExtractOutcome, ExtractMatchable, NeedInit, PRecord, MPromise, SPromise, SMPromise, NotPromise, AnyRecord, MReturnType, CmtTuple, Flasy, SrtSegment, ILogger, LogLevel, Asyncize } from "@zwa73/js-utils";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.235",
3
+ "version": "1.0.240",
4
4
  "description": "my utils",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {