@zwa73/utils 1.0.145 → 1.0.146
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/UtilDecorators.js +11 -12
- package/dist/UtilFileTools.d.ts +4 -0
- package/dist/UtilFileTools.js +17 -0
- package/dist/UtilFunctions.d.ts +5 -2
- package/dist/UtilFunctions.js +7 -4
- package/dist/UtilI18n.d.ts +1 -1
- package/dist/UtilI18n.js +4 -4
- package/dist/backup.js +2 -3
- package/package.json +1 -1
- package/src/UtilFileTools.ts +15 -0
- package/src/UtilFunctions.ts +11 -4
- package/src/UtilI18n.ts +5 -5
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/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
|
@@ -7,6 +7,8 @@ type HashMethod = typeof HashMethodList[number];
|
|
|
7
7
|
type ExecOpt = Partial<{
|
|
8
8
|
outlvl: LogLevel;
|
|
9
9
|
errlvl: LogLevel;
|
|
10
|
+
/**node_modules文件夹目录, 用于设定环境变量 */
|
|
11
|
+
nodeModules: string;
|
|
10
12
|
}>;
|
|
11
13
|
/**序列化选项 */
|
|
12
14
|
type StringifyOpt = Partial<{
|
|
@@ -80,8 +82,9 @@ export declare class UtilFunc {
|
|
|
80
82
|
/**封装的 cp.spawn 执行一段指令,指令运行时实时返回输出
|
|
81
83
|
* @param command - 指令文本
|
|
82
84
|
* @param opt - 可选参数
|
|
83
|
-
* @param opt.outlvl
|
|
84
|
-
* @param opt.errlvl
|
|
85
|
+
* @param opt.outlvl - 普通输出的日志等级
|
|
86
|
+
* @param opt.errlvl - 错误的日志等级
|
|
87
|
+
* @param opt.nodeModules - nodeModules文件夹路径
|
|
85
88
|
*/
|
|
86
89
|
static exec(command: string, opt?: ExecOpt): Promise<{
|
|
87
90
|
stdout: string;
|
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;
|
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
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
|
@@ -16,6 +16,8 @@ type HashMethod = typeof HashMethodList[number];
|
|
|
16
16
|
type ExecOpt = Partial<{
|
|
17
17
|
outlvl:LogLevel,
|
|
18
18
|
errlvl:LogLevel,
|
|
19
|
+
/**node_modules文件夹目录, 用于设定环境变量 */
|
|
20
|
+
nodeModules:string,
|
|
19
21
|
}>
|
|
20
22
|
|
|
21
23
|
/**序列化选项 */
|
|
@@ -142,17 +144,22 @@ private static originalExecTitle:None|string = None;
|
|
|
142
144
|
/**封装的 cp.spawn 执行一段指令,指令运行时实时返回输出
|
|
143
145
|
* @param command - 指令文本
|
|
144
146
|
* @param opt - 可选参数
|
|
145
|
-
* @param opt.outlvl
|
|
146
|
-
* @param opt.errlvl
|
|
147
|
+
* @param opt.outlvl - 普通输出的日志等级
|
|
148
|
+
* @param opt.errlvl - 错误的日志等级
|
|
149
|
+
* @param opt.nodeModules - nodeModules文件夹路径
|
|
147
150
|
*/
|
|
148
151
|
static exec(command: string, opt?:ExecOpt) {
|
|
149
152
|
return new Promise<{ stdout: string, stderr: string }>(async (resolve, reject) => {
|
|
150
153
|
// 创建一个新的环境变量对象,并将项目的 node_modules/.bin 目录添加到 PATH 环境变量中
|
|
151
154
|
const env = Object.create(process.env);
|
|
152
|
-
const penv = opath.join(
|
|
155
|
+
const penv = opath.join(
|
|
156
|
+
opt?.nodeModules
|
|
157
|
+
? UtilFT.currosizePath(opt.nodeModules)
|
|
158
|
+
: UtilFT.currosizePath(await UtilFunc.memoize(UtilFT.findNodeModulesDir)(process.cwd())??''),
|
|
159
|
+
'.bin');
|
|
153
160
|
|
|
154
161
|
if(UtilFunc.checkEnv && ! await UtilFT.pathExists(penv)){
|
|
155
|
-
SLogger.warn(
|
|
162
|
+
SLogger.warn(`UtilFunc.exec 错误, 没有检测到 node_modules/.bin 环境 penv:${penv}`);
|
|
156
163
|
UtilFunc.checkEnv = false;
|
|
157
164
|
}
|
|
158
165
|
|
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 - 国际化数据的路径。
|