@zwa73/utils 1.0.145 → 1.0.147
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/ServiceManager.d.ts +0 -0
- package/dist/ServiceManager.js +1 -0
- package/dist/UtilDecorators.js +11 -12
- package/dist/UtilFfmpegTools.js +1 -1
- package/dist/UtilFileTools.d.ts +4 -0
- package/dist/UtilFileTools.js +17 -0
- package/dist/UtilFunctions.d.ts +16 -2
- package/dist/UtilFunctions.js +28 -7
- package/dist/UtilI18n.d.ts +1 -1
- package/dist/UtilI18n.js +4 -4
- package/dist/backup.js +2 -3
- package/package.json +3 -3
- package/scripts/postinstall.js +5 -5
- package/src/ServiceManager.ts +0 -0
- package/src/UtilFfmpegTools.ts +1 -1
- package/src/UtilFileTools.ts +15 -0
- package/src/UtilFunctions.ts +50 -10
- package/src/UtilI18n.ts +5 -5
- package/dist/ServiceAdapter.d.ts +0 -47
- package/dist/ServiceAdapter.js +0 -35
- package/src/ServiceAdapter.ts +0 -73
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
package/dist/UtilDecorators.js
CHANGED
|
@@ -9,7 +9,17 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
9
9
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.
|
|
12
|
+
exports.LogTime = LogTime;
|
|
13
|
+
exports.LogTimeAsync = LogTimeAsync;
|
|
14
|
+
exports.LogCall = LogCall;
|
|
15
|
+
exports.LogCallAsync = LogCallAsync;
|
|
16
|
+
exports.LogErr = LogErr;
|
|
17
|
+
exports.LogErrAsync = LogErrAsync;
|
|
18
|
+
exports.Defer = Defer;
|
|
19
|
+
exports.DeferAsync = DeferAsync;
|
|
20
|
+
exports.Catch = Catch;
|
|
21
|
+
exports.CatchAsync = CatchAsync;
|
|
22
|
+
exports.AwaitInited = AwaitInited;
|
|
13
23
|
const UtilFunctions_1 = require("./UtilFunctions");
|
|
14
24
|
const UtilLogger_1 = require("./UtilLogger");
|
|
15
25
|
/**用于打印方法运行时间
|
|
@@ -29,7 +39,6 @@ function LogTime(flag, suffixUID) {
|
|
|
29
39
|
return descriptor;
|
|
30
40
|
};
|
|
31
41
|
}
|
|
32
|
-
exports.LogTime = LogTime;
|
|
33
42
|
/**用于打印异步方法运行时间
|
|
34
43
|
* @param flag - 时间标签
|
|
35
44
|
* @param suffixUID - 是否尾随uid
|
|
@@ -47,7 +56,6 @@ function LogTimeAsync(flag, suffixUID) {
|
|
|
47
56
|
return descriptor;
|
|
48
57
|
};
|
|
49
58
|
}
|
|
50
|
-
exports.LogTimeAsync = LogTimeAsync;
|
|
51
59
|
/**用于打印方法的调用 */
|
|
52
60
|
function LogCall() {
|
|
53
61
|
return function (target, propertyKey, descriptor) {
|
|
@@ -60,7 +68,6 @@ function LogCall() {
|
|
|
60
68
|
return descriptor;
|
|
61
69
|
};
|
|
62
70
|
}
|
|
63
|
-
exports.LogCall = LogCall;
|
|
64
71
|
/**用于打印异步方法的调用 */
|
|
65
72
|
function LogCallAsync() {
|
|
66
73
|
return function (target, propertyKey, descriptor) {
|
|
@@ -73,7 +80,6 @@ function LogCallAsync() {
|
|
|
73
80
|
return descriptor;
|
|
74
81
|
};
|
|
75
82
|
}
|
|
76
|
-
exports.LogCallAsync = LogCallAsync;
|
|
77
83
|
/**用于打印方法的调用 */
|
|
78
84
|
function LogErr() {
|
|
79
85
|
return function (target, propertyKey, descriptor) {
|
|
@@ -90,7 +96,6 @@ function LogErr() {
|
|
|
90
96
|
return descriptor;
|
|
91
97
|
};
|
|
92
98
|
}
|
|
93
|
-
exports.LogErr = LogErr;
|
|
94
99
|
/**用于打印异步方法的调用 */
|
|
95
100
|
function LogErrAsync() {
|
|
96
101
|
return function (target, propertyKey, descriptor) {
|
|
@@ -107,7 +112,6 @@ function LogErrAsync() {
|
|
|
107
112
|
return descriptor;
|
|
108
113
|
};
|
|
109
114
|
}
|
|
110
|
-
exports.LogErrAsync = LogErrAsync;
|
|
111
115
|
/**try-finally包装 */
|
|
112
116
|
function Defer(deferLogic) {
|
|
113
117
|
return function (target, propertyKey, descriptor) {
|
|
@@ -124,7 +128,6 @@ function Defer(deferLogic) {
|
|
|
124
128
|
return descriptor;
|
|
125
129
|
};
|
|
126
130
|
}
|
|
127
|
-
exports.Defer = Defer;
|
|
128
131
|
/**异步的try-finally包装 */
|
|
129
132
|
function DeferAsync(deferLogic) {
|
|
130
133
|
return function (target, propertyKey, descriptor) {
|
|
@@ -142,7 +145,6 @@ function DeferAsync(deferLogic) {
|
|
|
142
145
|
return descriptor;
|
|
143
146
|
};
|
|
144
147
|
}
|
|
145
|
-
exports.DeferAsync = DeferAsync;
|
|
146
148
|
/**try-catch包装 */
|
|
147
149
|
function Catch(catchLogic) {
|
|
148
150
|
return function (target, propertyKey, descriptor) {
|
|
@@ -160,7 +162,6 @@ function Catch(catchLogic) {
|
|
|
160
162
|
return descriptor;
|
|
161
163
|
};
|
|
162
164
|
}
|
|
163
|
-
exports.Catch = Catch;
|
|
164
165
|
/**异步的try-catch包装 */
|
|
165
166
|
function CatchAsync(catchLogic) {
|
|
166
167
|
return function (target, propertyKey, descriptor) {
|
|
@@ -178,7 +179,6 @@ function CatchAsync(catchLogic) {
|
|
|
178
179
|
return descriptor;
|
|
179
180
|
};
|
|
180
181
|
}
|
|
181
|
-
exports.CatchAsync = CatchAsync;
|
|
182
182
|
/**等待完成init */
|
|
183
183
|
function AwaitInited(target, propertyKey, descriptor) {
|
|
184
184
|
const originalMethod = descriptor.value;
|
|
@@ -188,7 +188,6 @@ function AwaitInited(target, propertyKey, descriptor) {
|
|
|
188
188
|
};
|
|
189
189
|
return descriptor;
|
|
190
190
|
}
|
|
191
|
-
exports.AwaitInited = AwaitInited;
|
|
192
191
|
function AddNumberDecorator(n) {
|
|
193
192
|
return function (target, propertyKey, descriptor) {
|
|
194
193
|
const originalMethod = descriptor.value;
|
package/dist/UtilFfmpegTools.js
CHANGED
|
@@ -37,7 +37,7 @@ const UtilClass_1 = require("./UtilClass");
|
|
|
37
37
|
class SFfmpegTool {
|
|
38
38
|
/**静态构造函数 */
|
|
39
39
|
static init() {
|
|
40
|
-
const ffmpegPath = process
|
|
40
|
+
const ffmpegPath = process?.env?.FFMPEG_PATH;
|
|
41
41
|
if (ffmpegPath != null) {
|
|
42
42
|
const exepath = pathe_1.default.join(ffmpegPath, "ffmpeg.exe");
|
|
43
43
|
SFfmpegTool.setFfmpegPath(exepath);
|
package/dist/UtilFileTools.d.ts
CHANGED
|
@@ -143,6 +143,10 @@ export declare namespace UtilFT {
|
|
|
143
143
|
* @returns 新的符合当前系统风格的路径
|
|
144
144
|
*/
|
|
145
145
|
function currosizePath(filePath: string): string;
|
|
146
|
+
/**逐级寻找node_modules文件夹
|
|
147
|
+
* @param basePath - 基础位置
|
|
148
|
+
*/
|
|
149
|
+
function findNodeModulesDir(basePath: string): Promise<string | undefined>;
|
|
146
150
|
/**搜索路径符合正则表达式的文件
|
|
147
151
|
* @param dir - 起始目录
|
|
148
152
|
* @param traitRegex - 正则表达式
|
package/dist/UtilFileTools.js
CHANGED
|
@@ -242,6 +242,23 @@ var UtilFT;
|
|
|
242
242
|
return posixizePath(filePath);
|
|
243
243
|
}
|
|
244
244
|
UtilFT.currosizePath = currosizePath;
|
|
245
|
+
/**逐级寻找node_modules文件夹
|
|
246
|
+
* @param basePath - 基础位置
|
|
247
|
+
*/
|
|
248
|
+
async function findNodeModulesDir(basePath) {
|
|
249
|
+
let currentDir = basePath;
|
|
250
|
+
while (currentDir) {
|
|
251
|
+
const nodeModulesPath = pathe_1.default.join(currentDir, 'node_modules');
|
|
252
|
+
if (await UtilFT.pathExists(nodeModulesPath))
|
|
253
|
+
return nodeModulesPath;
|
|
254
|
+
const parentDir = pathe_1.default.dirname(currentDir);
|
|
255
|
+
if (parentDir === currentDir)
|
|
256
|
+
break;
|
|
257
|
+
currentDir = parentDir;
|
|
258
|
+
}
|
|
259
|
+
return undefined;
|
|
260
|
+
}
|
|
261
|
+
UtilFT.findNodeModulesDir = findNodeModulesDir;
|
|
245
262
|
/**搜索路径符合正则表达式的文件
|
|
246
263
|
* @param dir - 起始目录
|
|
247
264
|
* @param traitRegex - 正则表达式
|
package/dist/UtilFunctions.d.ts
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import { AnyFunc, ComposedClass, ComposedMixinable, ComposedRefMixinable, ExtractOutcome, IJData, JObject, JToken, Keyable, Literal, Matchable, MatchableFlag, Mixinable, Outcome, ReqVerifyFn, ProperSubsetCheck, RefMixinable, UnionToIntersection, Await, AnyRecord } from "./UtilInterfaces";
|
|
2
2
|
import { LogLevel } from "./UtilLogger";
|
|
3
3
|
import { Failed, FailedLike, None, StatusSymbol, Success, SuccessLike, Timeout } from "./UtilSymbol";
|
|
4
|
+
import { PRecord } from "..";
|
|
4
5
|
declare const HashMethodList: readonly ["md5", "sha1", "sha256", "sha512", "sha3", "blake2", "blake3"];
|
|
5
6
|
type HashMethod = typeof HashMethodList[number];
|
|
6
7
|
/**执行选项 */
|
|
7
8
|
type ExecOpt = Partial<{
|
|
8
9
|
outlvl: LogLevel;
|
|
9
10
|
errlvl: LogLevel;
|
|
11
|
+
/**node_modules文件夹目录, 用于设定环境变量 */
|
|
12
|
+
nodeModules: string;
|
|
10
13
|
}>;
|
|
11
14
|
/**序列化选项 */
|
|
12
15
|
type StringifyOpt = Partial<{
|
|
@@ -80,8 +83,9 @@ export declare class UtilFunc {
|
|
|
80
83
|
/**封装的 cp.spawn 执行一段指令,指令运行时实时返回输出
|
|
81
84
|
* @param command - 指令文本
|
|
82
85
|
* @param opt - 可选参数
|
|
83
|
-
* @param opt.outlvl
|
|
84
|
-
* @param opt.errlvl
|
|
86
|
+
* @param opt.outlvl - 普通输出的日志等级
|
|
87
|
+
* @param opt.errlvl - 错误的日志等级
|
|
88
|
+
* @param opt.nodeModules - nodeModules文件夹路径
|
|
85
89
|
*/
|
|
86
90
|
static exec(command: string, opt?: ExecOpt): Promise<{
|
|
87
91
|
stdout: string;
|
|
@@ -162,6 +166,16 @@ export declare class UtilFunc {
|
|
|
162
166
|
}>>(t: T, procObj: P): P extends Record<any, AnyFunc> ? {
|
|
163
167
|
[K in keyof P]: ReturnType<P[K]>;
|
|
164
168
|
}[keyof P] : never;
|
|
169
|
+
/**处理部分联合值
|
|
170
|
+
* @param t - 目标值
|
|
171
|
+
* @param procObj - 所有可能的id组成的处理函数映射 (status, result)=>any
|
|
172
|
+
* @returns 任意处理函数的返回值
|
|
173
|
+
*/
|
|
174
|
+
static matchPartialProc<T extends Matchable<Keyable>, P extends Partial<{
|
|
175
|
+
[K in MatchableFlag<T>]: (k: K, v: ExtractOutcome<T, MatchableFlag<K>>['result']) => unknown;
|
|
176
|
+
}>>(t: T, procObj: P): (P extends PRecord<Keyable, AnyFunc> ? {
|
|
177
|
+
[K in keyof P]: P[K] extends AnyFunc ? ReturnType<P[K]> : undefined;
|
|
178
|
+
}[keyof P] : never) | undefined;
|
|
165
179
|
/**根据典型的成功或失败状态运行函数
|
|
166
180
|
* @param t - 目标值
|
|
167
181
|
* @param sucessFunc - 成功则运行的函数 (result)=>any
|
package/dist/UtilFunctions.js
CHANGED
|
@@ -116,16 +116,19 @@ class UtilFunc {
|
|
|
116
116
|
/**封装的 cp.spawn 执行一段指令,指令运行时实时返回输出
|
|
117
117
|
* @param command - 指令文本
|
|
118
118
|
* @param opt - 可选参数
|
|
119
|
-
* @param opt.outlvl
|
|
120
|
-
* @param opt.errlvl
|
|
119
|
+
* @param opt.outlvl - 普通输出的日志等级
|
|
120
|
+
* @param opt.errlvl - 错误的日志等级
|
|
121
|
+
* @param opt.nodeModules - nodeModules文件夹路径
|
|
121
122
|
*/
|
|
122
123
|
static exec(command, opt) {
|
|
123
124
|
return new Promise(async (resolve, reject) => {
|
|
124
125
|
// 创建一个新的环境变量对象,并将项目的 node_modules/.bin 目录添加到 PATH 环境变量中
|
|
125
126
|
const env = Object.create(process.env);
|
|
126
|
-
const penv = path_1.default.join(
|
|
127
|
+
const penv = path_1.default.join(opt?.nodeModules
|
|
128
|
+
? UtilFileTools_1.UtilFT.currosizePath(opt.nodeModules)
|
|
129
|
+
: UtilFileTools_1.UtilFT.currosizePath(await UtilFunc.memoize(UtilFileTools_1.UtilFT.findNodeModulesDir)(process.cwd()) ?? ''), '.bin');
|
|
127
130
|
if (UtilFunc.checkEnv && !await UtilFileTools_1.UtilFT.pathExists(penv)) {
|
|
128
|
-
UtilLogger_1.SLogger.warn(
|
|
131
|
+
UtilLogger_1.SLogger.warn(`UtilFunc.exec 错误, 没有检测到 node_modules/.bin 环境 penv:${penv}`);
|
|
129
132
|
UtilFunc.checkEnv = false;
|
|
130
133
|
}
|
|
131
134
|
env.PATH = penv + path_1.default.delimiter + env.PATH;
|
|
@@ -389,19 +392,20 @@ class UtilFunc {
|
|
|
389
392
|
space = space === null ? undefined : space;
|
|
390
393
|
if (!compress)
|
|
391
394
|
return JSON.stringify(token, null, space);
|
|
395
|
+
const ec = '\uF121\uF122\uF123';
|
|
392
396
|
const compressReplacer = (key, value) => {
|
|
393
397
|
if (Array.isArray(value) && value.every(item => typeof item === 'number' || typeof item === 'string' ||
|
|
394
398
|
typeof item === 'boolean' || item == null))
|
|
395
|
-
return
|
|
399
|
+
return `${ec}${JSON.stringify(value)}${ec}`;
|
|
396
400
|
const str = JSON.stringify(value);
|
|
397
401
|
if (typeof str != 'string')
|
|
398
402
|
return value;
|
|
399
403
|
if (str.length <= 100)
|
|
400
|
-
return
|
|
404
|
+
return `${ec}${str}${ec}`;
|
|
401
405
|
return value;
|
|
402
406
|
};
|
|
403
407
|
return JSON.stringify(token, compressReplacer, space)
|
|
404
|
-
.replace(
|
|
408
|
+
.replace(new RegExp(`"${ec}(.*?)${ec}"`, 'g'), (match, p1) => p1.replace(/\\([\\"])/g, '$1'));
|
|
405
409
|
}
|
|
406
410
|
/**代办表 用于队列处理等待 */
|
|
407
411
|
static pendingMap = {};
|
|
@@ -483,6 +487,23 @@ class UtilFunc {
|
|
|
483
487
|
return procObj[t.status](t.status, t.result);
|
|
484
488
|
UtilFunc.throwError(`matchProc 传入了一个预料之外的值: ${String(t)}`, 'fatal');
|
|
485
489
|
}
|
|
490
|
+
/**处理部分联合值
|
|
491
|
+
* @param t - 目标值
|
|
492
|
+
* @param procObj - 所有可能的id组成的处理函数映射 (status, result)=>any
|
|
493
|
+
* @returns 任意处理函数的返回值
|
|
494
|
+
*/
|
|
495
|
+
static matchPartialProc(t, procObj) {
|
|
496
|
+
if (typeof t === 'string' || typeof t === 'number' || typeof t === 'symbol') {
|
|
497
|
+
if (procObj[t])
|
|
498
|
+
return procObj[t](t);
|
|
499
|
+
else
|
|
500
|
+
return undefined;
|
|
501
|
+
}
|
|
502
|
+
else if (procObj[t.status])
|
|
503
|
+
return procObj[t.status](t.status, t.result);
|
|
504
|
+
UtilLogger_1.SLogger.warn(`matchPartialProc 传入了一个预料之外的值, 已返回undefined: ${String(t)}`);
|
|
505
|
+
return undefined;
|
|
506
|
+
}
|
|
486
507
|
/**根据典型的成功或失败状态运行函数
|
|
487
508
|
* @param t - 目标值
|
|
488
509
|
* @param sucessFunc - 成功则运行的函数 (result)=>any
|
package/dist/UtilI18n.d.ts
CHANGED
|
@@ -69,7 +69,7 @@ export declare class SI18n {
|
|
|
69
69
|
* 若添加在文本末尾则不会作为原文文本显示, 仅供索引
|
|
70
70
|
* @param mk - 特殊标记文本
|
|
71
71
|
*/
|
|
72
|
-
static mark(mk: string):
|
|
72
|
+
static mark(mk: string): `%${string}%`;
|
|
73
73
|
/**初始化函数,加载国际化数据,设置语言,并在进程退出前保存数据。
|
|
74
74
|
* @param i18nDataDir - 国际化数据的路径。
|
|
75
75
|
* @param lang - 要设置的语言。
|
package/dist/UtilI18n.js
CHANGED
|
@@ -21,7 +21,7 @@ const parseLangFlag = (lf) => {
|
|
|
21
21
|
const match = lf.match(/^([^-]+)(.+)/);
|
|
22
22
|
return { 1: match[1], 2: match[2] != '' ? match[2] : undefined };
|
|
23
23
|
};
|
|
24
|
-
const MarkRegex =
|
|
24
|
+
const MarkRegex = /%([^%]+)%$/;
|
|
25
25
|
const BaseFile = 'base_lang.json';
|
|
26
26
|
const TemplateFile = 'template.json';
|
|
27
27
|
const LangDir = 'lang';
|
|
@@ -35,11 +35,11 @@ class SI18n {
|
|
|
35
35
|
static _vaildLang;
|
|
36
36
|
/**解析i18n索引 */
|
|
37
37
|
static parseI18nKey(i18nKey) {
|
|
38
|
-
const match = i18nKey.match(/([\s\S]+?)(
|
|
38
|
+
const match = i18nKey.match(/([\s\S]+?)((?<!\\)%[^%]+(?<!\\)%$)?$/);
|
|
39
39
|
const base = match[1];
|
|
40
40
|
const mark = match[2] == '' || match[2] == undefined
|
|
41
41
|
? undefined
|
|
42
|
-
: match[2].match(
|
|
42
|
+
: match[2].match(/^%([^%]+)%$/)[1];
|
|
43
43
|
return { base, mark };
|
|
44
44
|
}
|
|
45
45
|
/**格式化文本与mark为i18n索引key */
|
|
@@ -51,7 +51,7 @@ class SI18n {
|
|
|
51
51
|
* @param mk - 特殊标记文本
|
|
52
52
|
*/
|
|
53
53
|
static mark(mk) {
|
|
54
|
-
return
|
|
54
|
+
return `%${mk}%`;
|
|
55
55
|
}
|
|
56
56
|
/**初始化函数,加载国际化数据,设置语言,并在进程退出前保存数据。
|
|
57
57
|
* @param i18nDataDir - 国际化数据的路径。
|
package/dist/backup.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.composeClassFull = composeClassFull;
|
|
4
|
+
exports.assertThisAs = assertThisAs;
|
|
4
5
|
/**类组合
|
|
5
6
|
* 将mixinList每个成员的字段混入base
|
|
6
7
|
* @param base - 基础类
|
|
@@ -36,7 +37,6 @@ function composeClassFull(base, ...mixinList) {
|
|
|
36
37
|
}
|
|
37
38
|
return obj;
|
|
38
39
|
}
|
|
39
|
-
exports.composeClassFull = composeClassFull;
|
|
40
40
|
/**尝试断言一个类的原型
|
|
41
41
|
* 其所有函数的 this 都必须为某个类型
|
|
42
42
|
* assignThisAs<Self,T>(Self.prototype);
|
|
@@ -45,4 +45,3 @@ exports.composeClassFull = composeClassFull;
|
|
|
45
45
|
* @param prototype - 类的原型
|
|
46
46
|
*/
|
|
47
47
|
function assertThisAs(prototype) { }
|
|
48
|
-
exports.assertThisAs = assertThisAs;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zwa73/utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.147",
|
|
4
4
|
"description": "my utils",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
"author": "zwa73",
|
|
18
18
|
"license": "ISC",
|
|
19
19
|
"dependencies": {
|
|
20
|
+
"@deepkit/type": "^1.0.1-alpha.153",
|
|
20
21
|
"fluent-ffmpeg": "2.1.2",
|
|
21
22
|
"glob": "^10.4.1",
|
|
22
23
|
"html-entities": "^2.3.3",
|
|
@@ -32,11 +33,10 @@
|
|
|
32
33
|
"devDependencies": {
|
|
33
34
|
"@types/fluent-ffmpeg": "^2.1.21",
|
|
34
35
|
"@types/jest": "^29.5.12",
|
|
35
|
-
"@types/node": "^
|
|
36
|
+
"@types/node": "^20.14.11",
|
|
36
37
|
"@zwa73/dev-utils": "^1.0.58",
|
|
37
38
|
"jest": "^29.7.0",
|
|
38
39
|
"ts-jest": "^29.1.2",
|
|
39
|
-
"ts-morph": "^23.0.0",
|
|
40
40
|
"tsc-alias": "^1.8.8",
|
|
41
41
|
"typescript": "^5.3.3"
|
|
42
42
|
}
|
package/scripts/postinstall.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const path = require('path');
|
|
2
|
-
const { UtilFT
|
|
2
|
+
const { UtilFT } = require("../index");
|
|
3
3
|
const readline = require('readline');
|
|
4
4
|
|
|
5
5
|
const PACKAGE_PATH = path.join(__dirname,'..','package.json');
|
|
@@ -17,6 +17,7 @@ const INFO_TABLE = {
|
|
|
17
17
|
"1.0.128":"fileSearch 添加Sync版本, 默认改为异步函数",
|
|
18
18
|
"1.0.131":"repeatPromise 与 repeatPost 改为传入可选参数表模式",
|
|
19
19
|
}
|
|
20
|
+
const PKG_NAME = "utils";
|
|
20
21
|
|
|
21
22
|
// 将版本号转换为可以比较的数字
|
|
22
23
|
function versionToNumber(version) {
|
|
@@ -44,19 +45,18 @@ function showUpgradeMessages(prevVersion, currentVersion) {
|
|
|
44
45
|
}
|
|
45
46
|
|
|
46
47
|
|
|
47
|
-
|
|
48
48
|
async function main(){
|
|
49
49
|
const packageTable = await UtilFT.loadJSONFile(PACKAGE_PATH);
|
|
50
50
|
const currentVersion = packageTable.version;
|
|
51
51
|
await UtilFT.ensurePathExists(VERSION_PATH);
|
|
52
|
-
const versionTable = await UtilFT.loadJSONFile(VERSION_PATH,{})
|
|
53
|
-
const prevVersion = versionTable.
|
|
52
|
+
const versionTable = await UtilFT.loadJSONFile(VERSION_PATH,{default:{}});
|
|
53
|
+
const prevVersion = versionTable[PKG_NAME]?.version ?? "0.0.0";
|
|
54
54
|
|
|
55
55
|
console.log(`${currentVersion} 版本已安装`);
|
|
56
56
|
// 使用这个函数来显示升级信息
|
|
57
57
|
const hasOut = showUpgradeMessages(prevVersion, currentVersion);
|
|
58
58
|
|
|
59
|
-
const ntable = Object.assign({},versionTable,{
|
|
59
|
+
const ntable = Object.assign({},versionTable,{[PKG_NAME]:{version:currentVersion}});
|
|
60
60
|
await UtilFT.writeJSONFile(VERSION_PATH, ntable);
|
|
61
61
|
|
|
62
62
|
if(!hasOut) return;
|
|
File without changes
|
package/src/UtilFfmpegTools.ts
CHANGED
|
@@ -15,7 +15,7 @@ export type IOMap = { [key: string]: string };
|
|
|
15
15
|
class SFfmpegTool {
|
|
16
16
|
/**静态构造函数 */
|
|
17
17
|
static init() {
|
|
18
|
-
const ffmpegPath = process
|
|
18
|
+
const ffmpegPath = process?.env?.FFMPEG_PATH;
|
|
19
19
|
if(ffmpegPath!=null){
|
|
20
20
|
const exepath = path.join(ffmpegPath,"ffmpeg.exe");
|
|
21
21
|
SFfmpegTool.setFfmpegPath(exepath);
|
package/src/UtilFileTools.ts
CHANGED
|
@@ -296,6 +296,21 @@ export function currosizePath(filePath: string): string {
|
|
|
296
296
|
return win32izePath(filePath);
|
|
297
297
|
return posixizePath(filePath);
|
|
298
298
|
}
|
|
299
|
+
/**逐级寻找node_modules文件夹
|
|
300
|
+
* @param basePath - 基础位置
|
|
301
|
+
*/
|
|
302
|
+
export async function findNodeModulesDir(basePath:string){
|
|
303
|
+
let currentDir = basePath;
|
|
304
|
+
while (currentDir) {
|
|
305
|
+
const nodeModulesPath = path.join(currentDir, 'node_modules');
|
|
306
|
+
if (await UtilFT.pathExists(nodeModulesPath))
|
|
307
|
+
return nodeModulesPath;
|
|
308
|
+
const parentDir = path.dirname(currentDir);
|
|
309
|
+
if (parentDir === currentDir) break;
|
|
310
|
+
currentDir = parentDir;
|
|
311
|
+
}
|
|
312
|
+
return undefined;
|
|
313
|
+
}
|
|
299
314
|
|
|
300
315
|
|
|
301
316
|
|
package/src/UtilFunctions.ts
CHANGED
|
@@ -8,6 +8,7 @@ import opath from 'path';
|
|
|
8
8
|
import { UtilFT } from "./UtilFileTools";
|
|
9
9
|
import {inspect} from 'util';
|
|
10
10
|
import type * as publicIp from 'public-ip';
|
|
11
|
+
import { PRecord } from "..";
|
|
11
12
|
|
|
12
13
|
const HashMethodList = ["md5","sha1","sha256","sha512","sha3","blake2","blake3"] as const;
|
|
13
14
|
type HashMethod = typeof HashMethodList[number];
|
|
@@ -16,6 +17,8 @@ type HashMethod = typeof HashMethodList[number];
|
|
|
16
17
|
type ExecOpt = Partial<{
|
|
17
18
|
outlvl:LogLevel,
|
|
18
19
|
errlvl:LogLevel,
|
|
20
|
+
/**node_modules文件夹目录, 用于设定环境变量 */
|
|
21
|
+
nodeModules:string,
|
|
19
22
|
}>
|
|
20
23
|
|
|
21
24
|
/**序列化选项 */
|
|
@@ -142,17 +145,22 @@ private static originalExecTitle:None|string = None;
|
|
|
142
145
|
/**封装的 cp.spawn 执行一段指令,指令运行时实时返回输出
|
|
143
146
|
* @param command - 指令文本
|
|
144
147
|
* @param opt - 可选参数
|
|
145
|
-
* @param opt.outlvl
|
|
146
|
-
* @param opt.errlvl
|
|
148
|
+
* @param opt.outlvl - 普通输出的日志等级
|
|
149
|
+
* @param opt.errlvl - 错误的日志等级
|
|
150
|
+
* @param opt.nodeModules - nodeModules文件夹路径
|
|
147
151
|
*/
|
|
148
152
|
static exec(command: string, opt?:ExecOpt) {
|
|
149
153
|
return new Promise<{ stdout: string, stderr: string }>(async (resolve, reject) => {
|
|
150
154
|
// 创建一个新的环境变量对象,并将项目的 node_modules/.bin 目录添加到 PATH 环境变量中
|
|
151
155
|
const env = Object.create(process.env);
|
|
152
|
-
const penv = opath.join(
|
|
156
|
+
const penv = opath.join(
|
|
157
|
+
opt?.nodeModules
|
|
158
|
+
? UtilFT.currosizePath(opt.nodeModules)
|
|
159
|
+
: UtilFT.currosizePath(await UtilFunc.memoize(UtilFT.findNodeModulesDir)(process.cwd())??''),
|
|
160
|
+
'.bin');
|
|
153
161
|
|
|
154
162
|
if(UtilFunc.checkEnv && ! await UtilFT.pathExists(penv)){
|
|
155
|
-
SLogger.warn(
|
|
163
|
+
SLogger.warn(`UtilFunc.exec 错误, 没有检测到 node_modules/.bin 环境 penv:${penv}`);
|
|
156
164
|
UtilFunc.checkEnv = false;
|
|
157
165
|
}
|
|
158
166
|
|
|
@@ -452,19 +460,21 @@ static stringifyJToken(token:JToken|IJData,opt?:StringifyOpt){
|
|
|
452
460
|
|
|
453
461
|
if(!compress) return JSON.stringify(token,null,space);
|
|
454
462
|
|
|
463
|
+
const ec = '\uF121\uF122\uF123';
|
|
464
|
+
|
|
455
465
|
const compressReplacer = (key: string, value: any)=> {
|
|
456
466
|
if (Array.isArray(value) && value.every(item =>
|
|
457
467
|
typeof item === 'number' || typeof item === 'string' ||
|
|
458
468
|
typeof item === 'boolean' || item == null))
|
|
459
|
-
return
|
|
469
|
+
return `${ec}${JSON.stringify(value)}${ec}`;
|
|
460
470
|
const str = JSON.stringify(value);
|
|
461
471
|
if(typeof str!='string') return value;
|
|
462
|
-
if(str.length<=100) return
|
|
472
|
+
if(str.length<=100) return `${ec}${str}${ec}`;
|
|
463
473
|
return value;
|
|
464
474
|
}
|
|
465
475
|
|
|
466
476
|
return JSON.stringify(token,compressReplacer,space)
|
|
467
|
-
.replace(
|
|
477
|
+
.replace(new RegExp(`"${ec}(.*?)${ec}"`,'g'), (match, p1) =>
|
|
468
478
|
p1.replace(/\\([\\"])/g, '$1'));
|
|
469
479
|
}
|
|
470
480
|
|
|
@@ -546,9 +556,10 @@ static outcome<K extends Keyable,V> (key:K,value:V):Outcome<K,V>{
|
|
|
546
556
|
*/
|
|
547
557
|
static matchProc<
|
|
548
558
|
T extends Matchable<Keyable>,
|
|
549
|
-
P extends UnionToIntersection<
|
|
550
|
-
|
|
551
|
-
(k:K,v:ExtractOutcome<T,MatchableFlag<K>>['result'])=>unknown
|
|
559
|
+
P extends UnionToIntersection<{//旧版本可能会将此映射处理为联合类型 所以需要UnionToIntersection
|
|
560
|
+
[K in MatchableFlag<T>] :
|
|
561
|
+
(k:K,v:ExtractOutcome<T,MatchableFlag<K>>['result'])=>unknown
|
|
562
|
+
}>>
|
|
552
563
|
(t:T, procObj:P):
|
|
553
564
|
P extends Record<any,AnyFunc>
|
|
554
565
|
? {[K in keyof P]: ReturnType<P[K]>}[keyof P]
|
|
@@ -562,6 +573,35 @@ static matchProc<
|
|
|
562
573
|
UtilFunc.throwError(`matchProc 传入了一个预料之外的值: ${String(t)}`,'fatal');
|
|
563
574
|
}
|
|
564
575
|
|
|
576
|
+
/**处理部分联合值
|
|
577
|
+
* @param t - 目标值
|
|
578
|
+
* @param procObj - 所有可能的id组成的处理函数映射 (status, result)=>any
|
|
579
|
+
* @returns 任意处理函数的返回值
|
|
580
|
+
*/
|
|
581
|
+
static matchPartialProc<
|
|
582
|
+
T extends Matchable<Keyable>,
|
|
583
|
+
P extends Partial<{
|
|
584
|
+
[K in MatchableFlag<T>] :
|
|
585
|
+
(k:K,v:ExtractOutcome<T,MatchableFlag<K>>['result'])=>unknown
|
|
586
|
+
}>>
|
|
587
|
+
(t:T, procObj:P):
|
|
588
|
+
(P extends PRecord<Keyable,AnyFunc>
|
|
589
|
+
? {[K in keyof P]: P[K] extends AnyFunc
|
|
590
|
+
? ReturnType<P[K]> : undefined
|
|
591
|
+
}[keyof P] : never ) | undefined {
|
|
592
|
+
if (typeof t === 'string' || typeof t === 'number' || typeof t === 'symbol'){
|
|
593
|
+
if ((procObj as any)[t])
|
|
594
|
+
return (procObj as any)[t](t);
|
|
595
|
+
else return undefined;
|
|
596
|
+
}
|
|
597
|
+
else if ((procObj as any)[t.status])
|
|
598
|
+
return (procObj as any)[t.status](t.status,(t as any).result);
|
|
599
|
+
SLogger.warn(`matchPartialProc 传入了一个预料之外的值, 已返回undefined: ${String(t)}`);
|
|
600
|
+
return undefined;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+
|
|
565
605
|
/**根据典型的成功或失败状态运行函数
|
|
566
606
|
* @param t - 目标值
|
|
567
607
|
* @param sucessFunc - 成功则运行的函数 (result)=>any
|
package/src/UtilI18n.ts
CHANGED
|
@@ -69,7 +69,7 @@ const parseLangFlag = (lf:LangFlag|'*')=>{
|
|
|
69
69
|
return {1:match[1],2:match[2]!='' ? match[2]:undefined};
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
-
const MarkRegex =
|
|
72
|
+
const MarkRegex = /%([^%]+)%$/;
|
|
73
73
|
const BaseFile = 'base_lang.json';
|
|
74
74
|
const TemplateFile = 'template.json';
|
|
75
75
|
const LangDir = 'lang';
|
|
@@ -83,11 +83,11 @@ export class SI18n {
|
|
|
83
83
|
private static _vaildLang?:(LangFlag|'template')[];
|
|
84
84
|
/**解析i18n索引 */
|
|
85
85
|
static parseI18nKey(i18nKey:string){
|
|
86
|
-
const match = i18nKey.match(/([\s\S]+?)(
|
|
86
|
+
const match = i18nKey.match(/([\s\S]+?)((?<!\\)%[^%]+(?<!\\)%$)?$/)!;
|
|
87
87
|
const base = match[1];
|
|
88
88
|
const mark = match[2]==''||match[2]==undefined
|
|
89
89
|
? undefined
|
|
90
|
-
: match[2].match(
|
|
90
|
+
: match[2].match(/^%([^%]+)%$/)![1];
|
|
91
91
|
return {base,mark};
|
|
92
92
|
}
|
|
93
93
|
/**格式化文本与mark为i18n索引key */
|
|
@@ -98,8 +98,8 @@ export class SI18n {
|
|
|
98
98
|
* 若添加在文本末尾则不会作为原文文本显示, 仅供索引
|
|
99
99
|
* @param mk - 特殊标记文本
|
|
100
100
|
*/
|
|
101
|
-
static mark(mk:string)
|
|
102
|
-
return
|
|
101
|
+
static mark(mk:string):`%${string}%`{
|
|
102
|
+
return `%${mk}%`
|
|
103
103
|
}
|
|
104
104
|
/**初始化函数,加载国际化数据,设置语言,并在进程退出前保存数据。
|
|
105
105
|
* @param i18nDataDir - 国际化数据的路径。
|
package/dist/ServiceAdapter.d.ts
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { Await, JToken, NeedInit } from "./UtilInterfaces";
|
|
2
|
-
export type ServiceInstance = {
|
|
3
|
-
/**停止实例释放内存
|
|
4
|
-
* @async
|
|
5
|
-
*/
|
|
6
|
-
kill: () => Promise<void> | void;
|
|
7
|
-
};
|
|
8
|
-
export type ServiceConfig = {
|
|
9
|
-
name: string;
|
|
10
|
-
opt: JToken;
|
|
11
|
-
};
|
|
12
|
-
export type CtorTable = {
|
|
13
|
-
[Type in string]: (opt: any) => Promise<ServiceInstance> | ServiceInstance;
|
|
14
|
-
};
|
|
15
|
-
export type CtorTable2FullCfgTable<CT extends CtorTable, Cfg> = {
|
|
16
|
-
[P in keyof CT]: P extends string ? CT[P] extends (opt: infer Opt) => any ? Cfg & {
|
|
17
|
-
type: P;
|
|
18
|
-
opt: Opt;
|
|
19
|
-
} : never : never;
|
|
20
|
-
};
|
|
21
|
-
export declare class ServiceAdapter<CTT extends CtorTable, CFG extends ServiceConfig, FCT extends CtorTable2FullCfgTable<CTT, CFG> = CtorTable2FullCfgTable<CTT, CFG>> implements NeedInit {
|
|
22
|
-
private _cfgPath;
|
|
23
|
-
private _ctorTable;
|
|
24
|
-
inited: Promise<void>;
|
|
25
|
-
protected _cfgTable: Record<string, FCT[keyof FCT]>;
|
|
26
|
-
protected _insPool: Record<string, Await<ReturnType<CTT[keyof CTT]>>>;
|
|
27
|
-
protected constructor(_cfgPath: string, _ctorTable: CTT);
|
|
28
|
-
start<T extends keyof FCT>(type: T, opt: FCT[T]['opt'], name?: string): Promise<void>;
|
|
29
|
-
}
|
|
30
|
-
type ITest1 = {
|
|
31
|
-
test1(): void;
|
|
32
|
-
} & ServiceInstance;
|
|
33
|
-
type ITest2 = {
|
|
34
|
-
test2(): void;
|
|
35
|
-
} & ServiceInstance;
|
|
36
|
-
type TCT = {
|
|
37
|
-
'Test1': (opt: {
|
|
38
|
-
t1: number;
|
|
39
|
-
}) => ITest1;
|
|
40
|
-
'Test2': (opt: {
|
|
41
|
-
t2: string;
|
|
42
|
-
}) => ITest2;
|
|
43
|
-
};
|
|
44
|
-
export type CfgSchema = CtorTable2FullCfgTable<TCT, {
|
|
45
|
-
ssa: string;
|
|
46
|
-
}>;
|
|
47
|
-
export {};
|
package/dist/ServiceAdapter.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ServiceAdapter = void 0;
|
|
4
|
-
const UtilFileTools_1 = require("./UtilFileTools");
|
|
5
|
-
const UtilFunctions_1 = require("./UtilFunctions");
|
|
6
|
-
class ServiceAdapter {
|
|
7
|
-
_cfgPath;
|
|
8
|
-
_ctorTable;
|
|
9
|
-
inited;
|
|
10
|
-
_cfgTable = {};
|
|
11
|
-
_insPool = {};
|
|
12
|
-
constructor(_cfgPath, _ctorTable) {
|
|
13
|
-
this._cfgPath = _cfgPath;
|
|
14
|
-
this._ctorTable = _ctorTable;
|
|
15
|
-
this.inited = UtilFunctions_1.UtilFunc.ivk(async () => {
|
|
16
|
-
this._cfgTable = await UtilFileTools_1.UtilFT.loadJSONFile(_cfgPath);
|
|
17
|
-
for (const name in this._cfgTable) {
|
|
18
|
-
const cfg = this._cfgTable[name];
|
|
19
|
-
//@ts-ignore
|
|
20
|
-
this._insPool[name] = await this._ctorTable[cfg.type](cfg.opt);
|
|
21
|
-
}
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
async start(type, opt, name = 'default') {
|
|
25
|
-
if (this._insPool[name] != undefined)
|
|
26
|
-
await this._insPool[name].kill();
|
|
27
|
-
//@ts-ignore
|
|
28
|
-
this._insPool[name] = await this._ctorTable[type](opt);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
exports.ServiceAdapter = ServiceAdapter;
|
|
32
|
-
class TessClass extends ServiceAdapter {
|
|
33
|
-
}
|
|
34
|
-
let a = null;
|
|
35
|
-
a.start('Test1', { t1: 100 });
|
package/src/ServiceAdapter.ts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { UtilFT } from "./UtilFileTools";
|
|
2
|
-
import { UtilFunc } from "./UtilFunctions";
|
|
3
|
-
import { Await, JToken, NeedInit } from "./UtilInterfaces";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
export type ServiceInstance = {
|
|
11
|
-
/**停止实例释放内存
|
|
12
|
-
* @async
|
|
13
|
-
*/
|
|
14
|
-
kill:()=>Promise<void>|void;
|
|
15
|
-
}
|
|
16
|
-
export type ServiceConfig = {
|
|
17
|
-
name:string;
|
|
18
|
-
opt :JToken;
|
|
19
|
-
};
|
|
20
|
-
export type CtorTable = {
|
|
21
|
-
[Type in string]:(opt:any)=>Promise<ServiceInstance>|ServiceInstance
|
|
22
|
-
}
|
|
23
|
-
export type CtorTable2FullCfgTable<CT extends CtorTable,Cfg> = {
|
|
24
|
-
[P in keyof CT]: P extends string ?
|
|
25
|
-
CT[P] extends (opt:infer Opt)=>any
|
|
26
|
-
? Cfg&{type:P,opt:Opt}
|
|
27
|
-
: never
|
|
28
|
-
: never
|
|
29
|
-
}
|
|
30
|
-
export class ServiceAdapter
|
|
31
|
-
<CTT extends CtorTable,
|
|
32
|
-
CFG extends ServiceConfig,
|
|
33
|
-
FCT extends CtorTable2FullCfgTable<CTT,CFG> = CtorTable2FullCfgTable<CTT,CFG>>
|
|
34
|
-
implements NeedInit{
|
|
35
|
-
inited:Promise<void>;
|
|
36
|
-
protected _cfgTable :Record<string,FCT[keyof FCT]>={};
|
|
37
|
-
protected _insPool :Record<string,Await<ReturnType<CTT[keyof CTT]>>>={};
|
|
38
|
-
protected constructor(private _cfgPath:string, private _ctorTable:CTT){
|
|
39
|
-
this.inited=UtilFunc.ivk(async ()=>{
|
|
40
|
-
this._cfgTable = await UtilFT.loadJSONFile(_cfgPath) as Record<string,FCT[keyof FCT]>;
|
|
41
|
-
for(const name in this._cfgTable){
|
|
42
|
-
const cfg = this._cfgTable[name];
|
|
43
|
-
//@ts-ignore
|
|
44
|
-
this._insPool[name] = await this._ctorTable[cfg.type](cfg.opt);
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
async start<T extends keyof FCT>(type:T,opt:FCT[T]['opt'],name:string='default'){
|
|
49
|
-
if(this._insPool[name]!=undefined)
|
|
50
|
-
await this._insPool[name].kill();
|
|
51
|
-
//@ts-ignore
|
|
52
|
-
this._insPool[name] = await this._ctorTable[type as keyof CTT](opt)
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
type ITest1 = {
|
|
58
|
-
test1():void;
|
|
59
|
-
}&ServiceInstance;
|
|
60
|
-
type ITest2 = {
|
|
61
|
-
test2():void;
|
|
62
|
-
}&ServiceInstance;
|
|
63
|
-
|
|
64
|
-
type TCT = {
|
|
65
|
-
'Test1':(opt:{t1:number})=>ITest1;
|
|
66
|
-
'Test2':(opt:{t2:string})=>ITest2;
|
|
67
|
-
}
|
|
68
|
-
class TessClass extends ServiceAdapter<TCT,{}&ServiceConfig>{
|
|
69
|
-
}
|
|
70
|
-
let a:TessClass = null as any;
|
|
71
|
-
a.start('Test1',{t1:100});
|
|
72
|
-
|
|
73
|
-
export type CfgSchema = CtorTable2FullCfgTable<TCT,{ssa:string}>;
|