@zwa73/utils 1.0.111 → 1.0.113

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,2 +1,2 @@
1
1
  import { UtilFunc } from "./UtilFunctions";
2
- export declare const outcome: typeof UtilFunc.outcome, matchProc: typeof UtilFunc.matchProc, sucesProc: typeof UtilFunc.sucesProc, isFailed: typeof UtilFunc.isFailed, isSafeNumber: typeof UtilFunc.isSafeNumber, isSuccess: typeof UtilFunc.isSuccess, likeNone: typeof UtilFunc.likeNone, expect: typeof UtilFunc.expect, assertType: typeof UtilFunc.assertType, assertLiteral: typeof UtilFunc.assertLiteral, deepClone: typeof UtilFunc.deepClone, sleep: typeof UtilFunc.sleep, stringifyJToken: typeof UtilFunc.stringifyJToken, getTime: typeof UtilFunc.getTime, mapEntries: typeof UtilFunc.mapEntries, composeMixinable: typeof UtilFunc.composeMixinable, dedent: typeof UtilFunc.dedent, throwError: typeof UtilFunc.throwError;
2
+ export declare const outcome: typeof UtilFunc.outcome, matchProc: typeof UtilFunc.matchProc, sucesProc: typeof UtilFunc.sucesProc, isFailed: typeof UtilFunc.isFailed, isSafeNumber: typeof UtilFunc.isSafeNumber, isSuccess: typeof UtilFunc.isSuccess, likeNone: typeof UtilFunc.likeNone, expect: typeof UtilFunc.expect, assertType: typeof UtilFunc.assertType, assertLiteral: typeof UtilFunc.assertLiteral, deepClone: typeof UtilFunc.deepClone, sleep: typeof UtilFunc.sleep, stringifyJToken: typeof UtilFunc.stringifyJToken, getTime: typeof UtilFunc.getTime, mapEntries: typeof UtilFunc.mapEntries, dedent: typeof UtilFunc.dedent, throwError: typeof UtilFunc.throwError;
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.throwError = exports.dedent = exports.composeMixinable = exports.mapEntries = exports.getTime = exports.stringifyJToken = exports.sleep = exports.deepClone = exports.assertLiteral = exports.assertType = exports.expect = exports.likeNone = exports.isSuccess = exports.isSafeNumber = exports.isFailed = exports.sucesProc = exports.matchProc = exports.outcome = void 0;
3
+ exports.throwError = exports.dedent = exports.mapEntries = exports.getTime = exports.stringifyJToken = exports.sleep = exports.deepClone = exports.assertLiteral = exports.assertType = exports.expect = exports.likeNone = exports.isSuccess = exports.isSafeNumber = exports.isFailed = exports.sucesProc = exports.matchProc = exports.outcome = void 0;
4
4
  const UtilFunctions_1 = require("./UtilFunctions");
5
5
  const UtilSymbol_1 = require("./UtilSymbol");
6
- exports.outcome = UtilFunctions_1.UtilFunc.outcome, exports.matchProc = UtilFunctions_1.UtilFunc.matchProc, exports.sucesProc = UtilFunctions_1.UtilFunc.sucesProc, exports.isFailed = UtilFunctions_1.UtilFunc.isFailed, exports.isSafeNumber = UtilFunctions_1.UtilFunc.isSafeNumber, exports.isSuccess = UtilFunctions_1.UtilFunc.isSuccess, exports.likeNone = UtilFunctions_1.UtilFunc.likeNone, exports.expect = UtilFunctions_1.UtilFunc.expect, exports.assertType = UtilFunctions_1.UtilFunc.assertType, exports.assertLiteral = UtilFunctions_1.UtilFunc.assertLiteral, exports.deepClone = UtilFunctions_1.UtilFunc.deepClone, exports.sleep = UtilFunctions_1.UtilFunc.sleep, exports.stringifyJToken = UtilFunctions_1.UtilFunc.stringifyJToken, exports.getTime = UtilFunctions_1.UtilFunc.getTime, exports.mapEntries = UtilFunctions_1.UtilFunc.mapEntries, exports.composeMixinable = UtilFunctions_1.UtilFunc.composeMixinable, exports.dedent = UtilFunctions_1.UtilFunc.dedent, exports.throwError = UtilFunctions_1.UtilFunc.throwError;
6
+ exports.outcome = UtilFunctions_1.UtilFunc.outcome, exports.matchProc = UtilFunctions_1.UtilFunc.matchProc, exports.sucesProc = UtilFunctions_1.UtilFunc.sucesProc, exports.isFailed = UtilFunctions_1.UtilFunc.isFailed, exports.isSafeNumber = UtilFunctions_1.UtilFunc.isSafeNumber, exports.isSuccess = UtilFunctions_1.UtilFunc.isSuccess, exports.likeNone = UtilFunctions_1.UtilFunc.likeNone, exports.expect = UtilFunctions_1.UtilFunc.expect, exports.assertType = UtilFunctions_1.UtilFunc.assertType, exports.assertLiteral = UtilFunctions_1.UtilFunc.assertLiteral, exports.deepClone = UtilFunctions_1.UtilFunc.deepClone, exports.sleep = UtilFunctions_1.UtilFunc.sleep, exports.stringifyJToken = UtilFunctions_1.UtilFunc.stringifyJToken, exports.getTime = UtilFunctions_1.UtilFunc.getTime, exports.mapEntries = UtilFunctions_1.UtilFunc.mapEntries, exports.dedent = UtilFunctions_1.UtilFunc.dedent, exports.throwError = UtilFunctions_1.UtilFunc.throwError;
7
7
  if (false) {
8
8
  let aaser = (0, exports.assertLiteral)({ a: 1 });
9
9
  let a = null;
@@ -3,6 +3,11 @@ import { LogLevel } from "./UtilLogger";
3
3
  import { FailedLike, None, StatusSymbol, Success, SuccessLike, Timeout } from "./UtilSymbol";
4
4
  declare const HashMethodList: readonly ["md5", "sha1", "sha256", "sha512", "sha3", "blake2", "blake3"];
5
5
  type HashMethod = typeof HashMethodList[number];
6
+ /**执行选项 */
7
+ type ExecOpt = Partial<{
8
+ outlvl: LogLevel;
9
+ errlvl: LogLevel;
10
+ }>;
6
11
  type SuccessOut<T> = Outcome<Success, T>;
7
12
  type TimeoutOut<T> = Outcome<Timeout, Promise<T>>;
8
13
  /**常用函数 */
@@ -14,7 +19,7 @@ export declare class UtilFunc {
14
19
  /**初始化对象的字段
15
20
  * 会改变obj
16
21
  * @param obj - 所要初始化的对象
17
- * @param field - 所要初始化的字段
22
+ * @param field - 所要初始化的字段
18
23
  * @param defaultVal - 默认值
19
24
  * @returns 最终值
20
25
  */
@@ -50,12 +55,13 @@ export declare class UtilFunc {
50
55
  * @returns
51
56
  */
52
57
  static sleep<T>(timeMs: number, result: T): Promise<T>;
53
- /**封装的 cp.exec 执行一段指令 指令完成后返回 Promise
58
+ /**封装的 cp.spawn 执行一段指令,指令运行时实时返回输出
54
59
  * @param command - 指令文本
55
- * @param outlvl - 普通输出的日志等级
56
- * @param errlvl - 错误的日志等级
60
+ * @param opt - 可选参数
61
+ * @param opt.outlvl - 普通输出的日志等级
62
+ * @param opt.errlvl - 错误的日志等级
57
63
  */
58
- static exec(command: string, outlvl?: LogLevel, errlvl?: LogLevel): Promise<{
64
+ static exec(command: string, opt?: ExecOpt): Promise<{
59
65
  stdout: string;
60
66
  stderr: string;
61
67
  }>;
@@ -52,7 +52,7 @@ class UtilFunc {
52
52
  /**初始化对象的字段
53
53
  * 会改变obj
54
54
  * @param obj - 所要初始化的对象
55
- * @param field - 所要初始化的字段
55
+ * @param field - 所要初始化的字段
56
56
  * @param defaultVal - 默认值
57
57
  * @returns 最终值
58
58
  */
@@ -103,23 +103,32 @@ class UtilFunc {
103
103
  }, timeMs);
104
104
  });
105
105
  }
106
- /**封装的 cp.exec 执行一段指令 指令完成后返回 Promise
106
+ /**封装的 cp.spawn 执行一段指令,指令运行时实时返回输出
107
107
  * @param command - 指令文本
108
- * @param outlvl - 普通输出的日志等级
109
- * @param errlvl - 错误的日志等级
108
+ * @param opt - 可选参数
109
+ * @param opt.outlvl - 普通输出的日志等级
110
+ * @param opt.errlvl - 错误的日志等级
110
111
  */
111
- static exec(command, outlvl, errlvl) {
112
+ static exec(command, opt) {
112
113
  return new Promise((resolve, reject) => {
113
- cp.exec(command, (error, stdout, stderr) => {
114
- if (error)
115
- reject(error);
116
- else {
117
- if (stdout && outlvl)
118
- UtilLogger_1.SLogger.log(outlvl, stdout);
119
- if (stderr && errlvl)
120
- UtilLogger_1.SLogger.log(errlvl, stderr);
121
- resolve({ stdout, stderr });
122
- }
114
+ const child = cp.spawn(command, { shell: true });
115
+ let stdout = '';
116
+ let stderr = '';
117
+ child.stdout.on('data', (data) => {
118
+ stdout += data;
119
+ if (opt?.outlvl)
120
+ UtilLogger_1.SLogger.log(opt?.outlvl, data.toString());
121
+ });
122
+ child.stderr.on('data', (data) => {
123
+ stderr += data;
124
+ if (opt?.errlvl)
125
+ UtilLogger_1.SLogger.log(opt?.errlvl, data.toString());
126
+ });
127
+ child.on('error', reject);
128
+ child.on('close', (code) => {
129
+ if (code !== 0)
130
+ UtilLogger_1.SLogger.warn(`UtilFunc.exec 命令:"${command}" 结束 代码为:${code}`);
131
+ resolve({ stdout, stderr });
123
132
  });
124
133
  });
125
134
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.111",
3
+ "version": "1.0.113",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -32,7 +32,7 @@
32
32
  "devDependencies": {
33
33
  "@types/jest": "^29.5.12",
34
34
  "@types/node": "^18.16.3",
35
- "@zwa73/dev-utils": "^1.0.13",
35
+ "@zwa73/dev-utils": "^1.0.22",
36
36
  "jest": "^29.7.0",
37
37
  "ts-jest": "^29.1.2",
38
38
  "tsc-alias": "^1.8.8",
@@ -56,7 +56,7 @@ async function main(){
56
56
  const hasOut = showUpgradeMessages(prevVersion, currentVersion);
57
57
 
58
58
  versionTable.utils.version = currentVersion;
59
- await UtilFT.writeJSONFile(dataPath, versionTable);
59
+ await UtilFT.writeJSONFile(VERSION_PATH, versionTable);
60
60
 
61
61
  if(!hasOut) return;
62
62
  const rl = readline.createInterface({
@@ -18,7 +18,6 @@ export const {
18
18
  stringifyJToken,
19
19
  getTime,
20
20
  mapEntries,
21
- composeMixinable,
22
21
  dedent,
23
22
  throwError,
24
23
  } = UtilFunc;
@@ -8,6 +8,12 @@ import { Catch, DeferAsync, LogTimeAsync } from "./UtilDecorators";
8
8
  const HashMethodList = ["md5","sha1","sha256","sha512","sha3","blake2","blake3"] as const;
9
9
  type HashMethod = typeof HashMethodList[number];
10
10
 
11
+ /**执行选项 */
12
+ type ExecOpt = Partial<{
13
+ outlvl:LogLevel,
14
+ errlvl:LogLevel,
15
+ }>
16
+
11
17
  type SuccessOut<T> = Outcome<Success,T>;
12
18
  type TimeoutOut<T> = Outcome<Timeout,Promise<T>>;
13
19
  /**永不完成的Promise单例 */
@@ -33,7 +39,7 @@ static getTime(): number {
33
39
  /**初始化对象的字段
34
40
  * 会改变obj
35
41
  * @param obj - 所要初始化的对象
36
- * @param field - 所要初始化的字段
42
+ * @param field - 所要初始化的字段
37
43
  * @param defaultVal - 默认值
38
44
  * @returns 最终值
39
45
  */
@@ -105,26 +111,35 @@ static async sleep<T>(timeMs: number, result?:T): Promise<T|undefined> {
105
111
  });
106
112
  }
107
113
 
108
- /**封装的 cp.exec 执行一段指令 指令完成后返回 Promise
114
+ /**封装的 cp.spawn 执行一段指令,指令运行时实时返回输出
109
115
  * @param command - 指令文本
110
- * @param outlvl - 普通输出的日志等级
111
- * @param errlvl - 错误的日志等级
116
+ * @param opt - 可选参数
117
+ * @param opt.outlvl - 普通输出的日志等级
118
+ * @param opt.errlvl - 错误的日志等级
112
119
  */
113
- static exec(command: string,outlvl?:LogLevel,errlvl?:LogLevel) {
114
- return new Promise<{ stdout:string, stderr:string }>
115
- ((resolve, reject) => {
116
- cp.exec(command, (error, stdout, stderr) => {
117
- if (error)
118
- reject(error);
119
- else{
120
- if(stdout && outlvl) SLogger.log(outlvl,stdout);
121
- if(stderr && errlvl) SLogger.log(errlvl,stderr);
122
- resolve({ stdout, stderr });
123
- }
124
- });
125
- });
126
- }
120
+ static exec(command: string, opt?:ExecOpt) {
121
+ return new Promise<{ stdout: string, stderr: string }>((resolve, reject) => {
122
+ const child = cp.spawn(command,{shell:true});
123
+
124
+ let stdout = '';
125
+ let stderr = '';
126
+
127
+ child.stdout.on('data', (data) => {
128
+ stdout += data;
129
+ if (opt?.outlvl) SLogger.log(opt?.outlvl, data.toString());
130
+ });
127
131
 
132
+ child.stderr.on('data', (data) => {
133
+ stderr += data;
134
+ if (opt?.errlvl) SLogger.log(opt?.errlvl, data.toString());
135
+ });
136
+ child.on('error', reject);
137
+ child.on('close', (code) => {
138
+ if (code!==0) SLogger.warn(`UtilFunc.exec 命令:"${command}" 结束 代码为:${code}`);
139
+ resolve({ stdout, stderr });
140
+ });
141
+ });
142
+ }
128
143
 
129
144
  /**获得一个永不完成的Promise单例 */
130
145
  static getNeverResolvedPromise<T>():Promise<T>{
@@ -153,7 +168,7 @@ static async repeatPromise<T>(procFn:()=>Promise<T>,verifyFn?:PromiseVerifyFn<T>
153
168
  verifyFn = ()=>Success;
154
169
 
155
170
  //进行中的请求
156
- const plist:Promise<
171
+ const plist:Promise<
157
172
  SuccessOut<PromiseResult<T>>|
158
173
  TimeoutOut<PromiseResult<T>>
159
174
  >[] = [];
@@ -377,9 +392,9 @@ static mapEntries<T extends object>
377
392
  * @returns 转换完成的字符串
378
393
  */
379
394
  static stringifyJToken(token:JToken|IJData,space:string|number|null|undefined="\t"){
380
- if(space==null)
381
- space=undefined;
382
- return JSON.stringify(token,null,space);
395
+ if(space==null)
396
+ space=undefined;
397
+ return JSON.stringify(token,null,space);
383
398
  }
384
399
 
385
400
  /**代办表 用于队列处理等待 */