@zwa73/dev-utils 1.0.54 → 1.0.55
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/Command/GenSchema.js +3 -1
- package/dist/UtilDevTool.d.ts +3 -0
- package/dist/UtilDevTool.js +18 -7
- package/package.json +1 -1
- package/src/Command/GenSchema.ts +7 -5
- package/src/UtilDevTool.ts +20 -8
@@ -10,13 +10,15 @@ const CmdGenSchema = (program) => program
|
|
10
10
|
.option("-i, --include <glob>", "包含的glob 默认 src/**/*.schema.ts", "src/**/*.schema.ts")
|
11
11
|
.option("-g, --exclude <glob>", "忽略的glob")
|
12
12
|
.option("-p, --project <path>", "tsconfig路径 默认tsconfig.json", "tsconfig.json")
|
13
|
-
.option("-o, --out
|
13
|
+
.option("-o, --out <dir>", "schema输出路径目录 默认 ./schema/")
|
14
|
+
.option("-t, --type <string>", "若传入则仅输出 type 对应的类型 此时将直接输出至outDir/type.json, 且不会展开")
|
14
15
|
.action(async (opt) => {
|
15
16
|
await UtilDevTool_1.UtilDT.generateSchema(process.cwd(), {
|
16
17
|
include: opt.include,
|
17
18
|
exclude: opt.exclude,
|
18
19
|
project: opt.project,
|
19
20
|
outDir: opt.outDir,
|
21
|
+
typeName: opt.type
|
20
22
|
});
|
21
23
|
});
|
22
24
|
exports.CmdGenSchema = CmdGenSchema;
|
package/dist/UtilDevTool.d.ts
CHANGED
@@ -18,6 +18,8 @@ export declare namespace UtilDT {
|
|
18
18
|
coverDefine: JObject;
|
19
19
|
/**schema输出路径目录 如 ./schema/ */
|
20
20
|
outDir: string;
|
21
|
+
/**若传入则仅输出 typeName 对应的类型 此时将直接输出至outDir/typeName.json, 且不会展开 */
|
22
|
+
typeName?: string;
|
21
23
|
}> & BuildMatchOpt;
|
22
24
|
/**生成匹配的文件的所有type的schema
|
23
25
|
* @async
|
@@ -28,6 +30,7 @@ export declare namespace UtilDT {
|
|
28
30
|
* @param opt.coverDefine - 将会覆盖 definitions 对应内容的表
|
29
31
|
* @param opt.project - tsconfig路径
|
30
32
|
* @param opt.outDir - schema输出路径目录 默认 ./schema/
|
33
|
+
* @param opt.typeName - 若传入则仅输出 typeName 对应的类型 此时将直接输出至outDir/typeName.json, 且不会展开
|
31
34
|
*/
|
32
35
|
export function generateSchema(dir: string, opt?: BuildSchemaOpt): Promise<void>;
|
33
36
|
/**运行所有匹配的的js/ts文件
|
package/dist/UtilDevTool.js
CHANGED
@@ -42,6 +42,7 @@ var UtilDT;
|
|
42
42
|
* @param opt.coverDefine - 将会覆盖 definitions 对应内容的表
|
43
43
|
* @param opt.project - tsconfig路径
|
44
44
|
* @param opt.outDir - schema输出路径目录 默认 ./schema/
|
45
|
+
* @param opt.typeName - 若传入则仅输出 typeName 对应的类型 此时将直接输出至outDir/typeName.json, 且不会展开
|
45
46
|
*/
|
46
47
|
async function generateSchema(dir, opt) {
|
47
48
|
//生成
|
@@ -49,6 +50,7 @@ var UtilDT;
|
|
49
50
|
required: true,
|
50
51
|
aliasRef: true,
|
51
52
|
tsNodeRegister: true,
|
53
|
+
ignoreErrors: true,
|
52
54
|
};
|
53
55
|
const compilerOptions = {
|
54
56
|
strictNullChecks: true,
|
@@ -57,21 +59,30 @@ var UtilDT;
|
|
57
59
|
Object.assign(compilerOptions, (await utils_1.UtilFT.loadJSONFile(opt.project)).compilerOptions);
|
58
60
|
const files = await utils_1.UtilFT.fileSearchGlob(dir, opt?.include ?? "**/*.schema.ts", { ingore: opt?.exclude });
|
59
61
|
const program = TJS.getProgramFromFiles(files, compilerOptions);
|
60
|
-
const schema = TJS.generateSchema(program, "*", settings);
|
62
|
+
const schema = TJS.generateSchema(program, opt?.typeName ?? "*", settings);
|
61
63
|
const outDir = opt?.outDir ?? pathe_1.default.join(process.cwd(), 'schema');
|
62
|
-
const schemasPath =
|
63
|
-
|
64
|
-
|
64
|
+
const schemasPath = opt?.typeName != undefined
|
65
|
+
? pathe_1.default.join(outDir, `${opt.typeName}.json`)
|
66
|
+
: pathe_1.default.join(outDir, 'schemas.json');
|
67
|
+
//预处理
|
68
|
+
const proced = procSchema(schema, opt?.coverDefine ?? {});
|
69
|
+
await Promise.all([
|
70
|
+
utils_1.UtilFT.writeJSONFile(schemasPath, schema),
|
71
|
+
//全范围类型则展开
|
72
|
+
opt?.typeName != undefined ? undefined : expandSchema(proced, schemasPath),
|
73
|
+
]);
|
65
74
|
}
|
66
75
|
UtilDT.generateSchema = generateSchema;
|
67
|
-
|
68
|
-
async function expandSchema(schemasPath, schema, coverDefine) {
|
76
|
+
function procSchema(schema, coverDefine) {
|
69
77
|
//覆盖
|
70
78
|
coverObj(schema["definitions"], coverDefine);
|
71
79
|
//替换SchemaString标识符
|
72
80
|
schema = JSON.parse(JSON.stringify(schema).replace(/\^\.\*SchemaString\$/g, '^.*$'));
|
81
|
+
return schema;
|
82
|
+
}
|
83
|
+
/**展开schema以供使用 */
|
84
|
+
async function expandSchema(schema, schemasPath) {
|
73
85
|
const plist = [];
|
74
|
-
plist.push(utils_1.UtilFT.writeJSONFile(schemasPath, schema));
|
75
86
|
const definitions = schema["definitions"];
|
76
87
|
//展开定义
|
77
88
|
for (const typeName in definitions) {
|
package/package.json
CHANGED
package/src/Command/GenSchema.ts
CHANGED
@@ -9,12 +9,14 @@ export const CmdGenSchema = (program:Command) => program
|
|
9
9
|
.option("-i, --include <glob>", "包含的glob 默认 src/**/*.schema.ts","src/**/*.schema.ts")
|
10
10
|
.option("-g, --exclude <glob>", "忽略的glob")
|
11
11
|
.option("-p, --project <path>", "tsconfig路径 默认tsconfig.json","tsconfig.json")
|
12
|
-
.option("-o, --out
|
12
|
+
.option("-o, --out <dir>", "schema输出路径目录 默认 ./schema/")
|
13
|
+
.option("-t, --type <string>", "若传入则仅输出 type 对应的类型 此时将直接输出至outDir/type.json, 且不会展开")
|
13
14
|
.action(async (opt) => {
|
14
15
|
await UtilDT.generateSchema(process.cwd(), {
|
15
|
-
include: opt.include,
|
16
|
-
exclude: opt.exclude,
|
17
|
-
project: opt.project,
|
18
|
-
outDir: opt.outDir,
|
16
|
+
include : opt.include,
|
17
|
+
exclude : opt.exclude,
|
18
|
+
project : opt.project,
|
19
|
+
outDir : opt.outDir,
|
20
|
+
typeName: opt.type
|
19
21
|
});
|
20
22
|
});
|
package/src/UtilDevTool.ts
CHANGED
@@ -25,6 +25,8 @@ type BuildSchemaOpt = Partial<{
|
|
25
25
|
coverDefine:JObject;
|
26
26
|
/**schema输出路径目录 如 ./schema/ */
|
27
27
|
outDir:string;
|
28
|
+
/**若传入则仅输出 typeName 对应的类型 此时将直接输出至outDir/typeName.json, 且不会展开 */
|
29
|
+
typeName?:string;
|
28
30
|
}>&BuildMatchOpt;
|
29
31
|
|
30
32
|
|
@@ -38,6 +40,7 @@ type BuildSchemaOpt = Partial<{
|
|
38
40
|
* @param opt.coverDefine - 将会覆盖 definitions 对应内容的表
|
39
41
|
* @param opt.project - tsconfig路径
|
40
42
|
* @param opt.outDir - schema输出路径目录 默认 ./schema/
|
43
|
+
* @param opt.typeName - 若传入则仅输出 typeName 对应的类型 此时将直接输出至outDir/typeName.json, 且不会展开
|
41
44
|
*/
|
42
45
|
export async function generateSchema(dir:string,opt?:BuildSchemaOpt){
|
43
46
|
//生成
|
@@ -45,6 +48,7 @@ export async function generateSchema(dir:string,opt?:BuildSchemaOpt){
|
|
45
48
|
required: true,
|
46
49
|
aliasRef:true,
|
47
50
|
tsNodeRegister:true,
|
51
|
+
ignoreErrors:true,
|
48
52
|
};
|
49
53
|
const compilerOptions: TJS.CompilerOptions = {
|
50
54
|
strictNullChecks: true,
|
@@ -56,21 +60,29 @@ export async function generateSchema(dir:string,opt?:BuildSchemaOpt){
|
|
56
60
|
files,
|
57
61
|
compilerOptions,
|
58
62
|
);
|
59
|
-
const schema = TJS.generateSchema(program, "*", settings) as JObject;
|
63
|
+
const schema = TJS.generateSchema(program, opt?.typeName??"*", settings) as JObject;
|
60
64
|
const outDir = opt?.outDir ?? path.join(process.cwd(),'schema');
|
61
|
-
const schemasPath =
|
62
|
-
|
63
|
-
|
65
|
+
const schemasPath = opt?.typeName !=undefined
|
66
|
+
? path.join(outDir,`${opt.typeName}.json`)
|
67
|
+
: path.join(outDir,'schemas.json');
|
68
|
+
//预处理
|
69
|
+
const proced = procSchema(schema,opt?.coverDefine??{});
|
70
|
+
await Promise.all([
|
71
|
+
UtilFT.writeJSONFile(schemasPath,schema),
|
72
|
+
//全范围类型则展开
|
73
|
+
opt?.typeName !=undefined ? undefined : expandSchema(proced,schemasPath),
|
74
|
+
]);
|
64
75
|
}
|
65
|
-
|
66
|
-
async function expandSchema(schemasPath:string,schema:JObject,coverDefine:JObject){
|
76
|
+
function procSchema(schema:JObject,coverDefine:JObject){
|
67
77
|
//覆盖
|
68
78
|
coverObj(schema["definitions"] as JObject,coverDefine);
|
69
79
|
//替换SchemaString标识符
|
70
80
|
schema = JSON.parse(JSON.stringify(schema).replace(/\^\.\*SchemaString\$/g,'^.*$'));
|
81
|
+
return schema;
|
82
|
+
}
|
83
|
+
/**展开schema以供使用 */
|
84
|
+
async function expandSchema(schema:JObject,schemasPath:string){
|
71
85
|
const plist:Promise<void>[] = [];
|
72
|
-
plist.push(UtilFT.writeJSONFile(schemasPath,schema));
|
73
|
-
|
74
86
|
const definitions = schema["definitions"] as Record<string,JObject>;
|
75
87
|
//展开定义
|
76
88
|
for(const typeName in definitions){
|