@zwa73/utils 1.0.143 → 1.0.145
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/ServiceAdapter.d.ts +47 -0
- package/dist/ServiceAdapter.js +35 -0
- package/dist/UtilI18n.js +6 -2
- package/package.json +2 -2
- package/src/ServiceAdapter.ts +73 -0
- package/src/UtilI18n.ts +6 -3
|
@@ -0,0 +1,47 @@
|
|
|
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 {};
|
|
@@ -0,0 +1,35 @@
|
|
|
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/dist/UtilI18n.js
CHANGED
|
@@ -17,6 +17,10 @@ const I18nFlagTable = {
|
|
|
17
17
|
const LangFlagList = Object.entries(I18nFlagTable)
|
|
18
18
|
.map(([k, v]) => `${k}-${v}`)
|
|
19
19
|
.concat(Object.keys(I18nFlagTable));
|
|
20
|
+
const parseLangFlag = (lf) => {
|
|
21
|
+
const match = lf.match(/^([^-]+)(.+)/);
|
|
22
|
+
return { 1: match[1], 2: match[2] != '' ? match[2] : undefined };
|
|
23
|
+
};
|
|
20
24
|
const MarkRegex = /%%%([^%]+)%%%$/;
|
|
21
25
|
const BaseFile = 'base_lang.json';
|
|
22
26
|
const TemplateFile = 'template.json';
|
|
@@ -53,7 +57,7 @@ class SI18n {
|
|
|
53
57
|
* @param i18nDataDir - 国际化数据的路径。
|
|
54
58
|
* @param lang - 要设置的语言。
|
|
55
59
|
*/
|
|
56
|
-
static async init(i18nDataDir, lang) {
|
|
60
|
+
static async init(i18nDataDir, lang = '*') {
|
|
57
61
|
const date = new Date().toISOString();
|
|
58
62
|
const mergePath = pathe_1.default.join(i18nDataDir, BaseFile);
|
|
59
63
|
const table = await UtilFileTools_1.UtilFT.loadJSONFile(mergePath, { default: {
|
|
@@ -68,7 +72,7 @@ class SI18n {
|
|
|
68
72
|
SI18n._vaildLang = ['template'];
|
|
69
73
|
//覆盖入单语言文件
|
|
70
74
|
const singleDir = pathe_1.default.join(i18nDataDir, LangDir);
|
|
71
|
-
(await Promise.all((await UtilFileTools_1.UtilFT.fileSearchGlob(singleDir, `**/${lang}*.json`))
|
|
75
|
+
(await Promise.all((await UtilFileTools_1.UtilFT.fileSearchGlob(singleDir, `**/${parseLangFlag(lang)[1]}*.json`))
|
|
72
76
|
.map(async (p) => await UtilFileTools_1.UtilFT.loadJSONFile(p))))
|
|
73
77
|
.forEach(t => {
|
|
74
78
|
SI18n._vaildLang?.push(t.target_lang);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zwa73/utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.145",
|
|
4
4
|
"description": "my utils",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"@types/fluent-ffmpeg": "^2.1.21",
|
|
34
34
|
"@types/jest": "^29.5.12",
|
|
35
35
|
"@types/node": "^18.16.3",
|
|
36
|
-
"@zwa73/dev-utils": "^1.0.
|
|
36
|
+
"@zwa73/dev-utils": "^1.0.58",
|
|
37
37
|
"jest": "^29.7.0",
|
|
38
38
|
"ts-jest": "^29.1.2",
|
|
39
39
|
"ts-morph": "^23.0.0",
|
|
@@ -0,0 +1,73 @@
|
|
|
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}>;
|
package/src/UtilI18n.ts
CHANGED
|
@@ -64,7 +64,10 @@ export type I18nTextData = I18nOrigTextData&{lang_table:PRecord<LangFlag,string>
|
|
|
64
64
|
export type I18nTable = Omit<I18nOrigTable,'texts'>&{
|
|
65
65
|
text_table:Record<string,I18nTextData>;
|
|
66
66
|
}
|
|
67
|
-
|
|
67
|
+
const parseLangFlag = (lf:LangFlag|'*')=>{
|
|
68
|
+
const match = lf.match(/^([^-]+)(.+)/)!
|
|
69
|
+
return {1:match[1],2:match[2]!='' ? match[2]:undefined};
|
|
70
|
+
}
|
|
68
71
|
|
|
69
72
|
const MarkRegex = /%%%([^%]+)%%%$/;
|
|
70
73
|
const BaseFile = 'base_lang.json';
|
|
@@ -102,7 +105,7 @@ export class SI18n {
|
|
|
102
105
|
* @param i18nDataDir - 国际化数据的路径。
|
|
103
106
|
* @param lang - 要设置的语言。
|
|
104
107
|
*/
|
|
105
|
-
static async init(i18nDataDir:string,lang
|
|
108
|
+
static async init(i18nDataDir:string,lang:LangFlag|'*'='*'){
|
|
106
109
|
const date = new Date().toISOString();
|
|
107
110
|
const mergePath = path.join(i18nDataDir,BaseFile);
|
|
108
111
|
const table = await UtilFT.loadJSONFile<I18nOrigTable>(mergePath,{default:{
|
|
@@ -119,7 +122,7 @@ export class SI18n {
|
|
|
119
122
|
SI18n._vaildLang=['template'];
|
|
120
123
|
//覆盖入单语言文件
|
|
121
124
|
const singleDir = path.join(i18nDataDir,LangDir);
|
|
122
|
-
(await Promise.all((await UtilFT.fileSearchGlob(singleDir,`**/${lang}*.json`))
|
|
125
|
+
(await Promise.all((await UtilFT.fileSearchGlob(singleDir,`**/${parseLangFlag(lang)[1]}*.json`))
|
|
123
126
|
.map(async p=>await UtilFT.loadJSONFile(p) as I18nSingleTable)))
|
|
124
127
|
.forEach(t=>{
|
|
125
128
|
SI18n._vaildLang?.push(t.target_lang);
|