@zwa73/dev-utils 1.0.26 → 1.0.28
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/BuildMacro.d.ts +3 -0
- package/dist/Command/BuildMacro.js +20 -0
- package/dist/Command/BuildSchema.d.ts +3 -0
- package/dist/Command/BuildSchema.js +22 -0
- package/dist/Command/Init.js +1 -0
- package/dist/Command/Node.js +3 -2
- package/dist/Command/Release.js +2 -1
- package/dist/Command/Route.js +4 -0
- package/dist/QuickFunc.d.ts +2 -0
- package/dist/QuickFunc.js +5 -0
- package/dist/UtilDevTool.d.ts +56 -0
- package/dist/UtilDevTool.js +170 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/package.json +2 -2
- package/schema/schemas.json +4 -0
- package/src/Command/BuildMacro.ts +18 -0
- package/src/Command/BuildSchema.ts +20 -0
- package/src/Command/Init.ts +1 -0
- package/src/Command/Node.ts +3 -2
- package/src/Command/Release.ts +2 -1
- package/src/Command/Route.ts +4 -0
- package/src/QuickFunc.ts +5 -0
- package/src/UtilDevTool.ts +167 -0
- package/src/index.ts +3 -2
- package/dist/UtilSchemaTool.d.ts +0 -28
- package/dist/UtilSchemaTool.js +0 -112
- package/src/UtilSchemaTool.ts +0 -95
@@ -0,0 +1,20 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.CmdBuildMacro = void 0;
|
4
|
+
const UtilDevTool_1 = require("../UtilDevTool");
|
5
|
+
/**构造macro */
|
6
|
+
const CmdBuildMacro = (program) => program
|
7
|
+
.command("Build-Macro")
|
8
|
+
.alias("buildmacro")
|
9
|
+
.description("生成根据macro生成代码")
|
10
|
+
.option("-i, --include <glob>", "包含的glob 默认 src/**/*.macro.ts", "src/**/*.macro.ts")
|
11
|
+
.option("-g, --exclude <glob>", "忽略的glob")
|
12
|
+
.option("-p, --project <path>", "tsconfig路径 默认tsconfig.json", "tsconfig.json")
|
13
|
+
.action(async (opt) => {
|
14
|
+
await UtilDevTool_1.UtilDT.matchNode(process.cwd(), {
|
15
|
+
include: opt.include,
|
16
|
+
exclude: opt.ignore,
|
17
|
+
project: opt.project,
|
18
|
+
});
|
19
|
+
});
|
20
|
+
exports.CmdBuildMacro = CmdBuildMacro;
|
@@ -0,0 +1,22 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.CmdBuildSchema = void 0;
|
4
|
+
const UtilDevTool_1 = require("../UtilDevTool");
|
5
|
+
/**生成schema */
|
6
|
+
const CmdBuildSchema = (program) => program
|
7
|
+
.command("Build-Schema")
|
8
|
+
.alias("buildschema")
|
9
|
+
.description("生成匹配的文件的所有type的schema")
|
10
|
+
.option("-i, --include <glob>", "包含的glob 默认 src/**/*.schema.ts", "src/**/*.schema.ts")
|
11
|
+
.option("-g, --exclude <glob>", "忽略的glob")
|
12
|
+
.option("-p, --project <path>", "tsconfig路径 默认tsconfig.json", "tsconfig.json")
|
13
|
+
.option("-o, --outDir <dir>", "schema输出路径目录 默认 ./schema/")
|
14
|
+
.action(async (opt) => {
|
15
|
+
await UtilDevTool_1.UtilDT.buildSchema(process.cwd(), {
|
16
|
+
include: opt.include,
|
17
|
+
exclude: opt.ignore,
|
18
|
+
project: opt.project,
|
19
|
+
outDir: opt.outDir,
|
20
|
+
});
|
21
|
+
});
|
22
|
+
exports.CmdBuildSchema = CmdBuildSchema;
|
package/dist/Command/Init.js
CHANGED
package/dist/Command/Node.js
CHANGED
@@ -4,14 +4,15 @@ exports.CmdNode = void 0;
|
|
4
4
|
const utils_1 = require("@zwa73/utils");
|
5
5
|
/**编译并运行某个文件 */
|
6
6
|
const CmdNode = (program) => program
|
7
|
-
.command('
|
7
|
+
.command('Node <path|path[]>')
|
8
|
+
.alias('node')
|
8
9
|
.option('-p, --project <path>', 'tsconfig的目录')
|
9
10
|
.description('编译并运行某个文件')
|
10
11
|
.action(async (arg, opt) => {
|
11
12
|
utils_1.SLogger.info(`编译并运行: ${arg}`);
|
12
13
|
let cmd = `ts-node -r tsconfig-paths/register ${arg}`;
|
13
14
|
if (opt.project)
|
14
|
-
cmd += `-
|
15
|
+
cmd += `-P ${opt.project}`;
|
15
16
|
await utils_1.UtilFunc.exec(cmd, { outlvl: 'info', errlvl: 'error' });
|
16
17
|
});
|
17
18
|
exports.CmdNode = CmdNode;
|
package/dist/Command/Release.js
CHANGED
@@ -47,7 +47,8 @@ async function updateVersion(newVersion) {
|
|
47
47
|
}
|
48
48
|
/**更新版本号并发布npm包 */
|
49
49
|
const CmdRelease = (program) => program
|
50
|
-
.command("
|
50
|
+
.command("Release")
|
51
|
+
.alias("release")
|
51
52
|
.description("更新版本号并发布包")
|
52
53
|
.option("-v, --version <version>", "指定发布的版本号 `${number}.${number}.${number}`")
|
53
54
|
.option("-a, --access <access>", "npm publish 的 access 参数", "public")
|
package/dist/Command/Route.js
CHANGED
@@ -7,12 +7,16 @@ const Node_1 = require("./Node");
|
|
7
7
|
const Release_1 = require("./Release");
|
8
8
|
const MapPath_1 = require("./MapPath");
|
9
9
|
const ScanDups_1 = require("./ScanDups");
|
10
|
+
const BuildSchema_1 = require("./BuildSchema");
|
11
|
+
const BuildMacro_1 = require("./BuildMacro");
|
10
12
|
async function cliRoute() {
|
11
13
|
(0, Init_1.CmdInit)(commander_1.program);
|
12
14
|
(0, Node_1.CmdNode)(commander_1.program);
|
13
15
|
(0, Release_1.CmdRelease)(commander_1.program);
|
14
16
|
(0, MapPath_1.CmdMapPath)(commander_1.program);
|
15
17
|
(0, ScanDups_1.CmdScanDups)(commander_1.program);
|
18
|
+
(0, BuildSchema_1.CmdBuildSchema)(commander_1.program);
|
19
|
+
(0, BuildMacro_1.CmdBuildMacro)(commander_1.program);
|
16
20
|
commander_1.program.parse(process.argv);
|
17
21
|
}
|
18
22
|
exports.cliRoute = cliRoute;
|
@@ -0,0 +1,56 @@
|
|
1
|
+
import { JObject } from '@zwa73/utils';
|
2
|
+
export declare namespace UtilDT {
|
3
|
+
/**执行js/ts的可选项 */
|
4
|
+
type BatchNodeOpt = Partial<{
|
5
|
+
/**tsconfig路径 */
|
6
|
+
project: string;
|
7
|
+
}>;
|
8
|
+
/**build任意ts文件的可选项 */
|
9
|
+
type BuildMatchOpt = Partial<{
|
10
|
+
/**包含的glob */
|
11
|
+
include: string | string[];
|
12
|
+
/**忽略的glob */
|
13
|
+
exclude: string | string[];
|
14
|
+
}> & BatchNodeOpt;
|
15
|
+
/**build schema的可选项 */
|
16
|
+
type BuildSchemaOpt = Partial<{
|
17
|
+
/**将会覆盖 definitions 对应内容的表 */
|
18
|
+
coverDefine: JObject;
|
19
|
+
/**schema输出路径目录 如 ./schema/ */
|
20
|
+
outDir: string;
|
21
|
+
}> & BuildMatchOpt;
|
22
|
+
/**生成匹配的文件的所有type的schema
|
23
|
+
* @async
|
24
|
+
* @param dir - 基础目录
|
25
|
+
* @param opt - 可选参数
|
26
|
+
* @param opt.include - 包含的glob 默认 **\/\*.schema.ts
|
27
|
+
* @param opt.exclude - 忽略的glob
|
28
|
+
* @param opt.coverDefine - 将会覆盖 definitions 对应内容的表
|
29
|
+
* @param opt.project - tsconfig路径
|
30
|
+
* @param opt.outDir - schema输出路径目录 默认 ./schema/
|
31
|
+
*/
|
32
|
+
export function buildSchema(dir: string, opt?: BuildSchemaOpt): Promise<void>;
|
33
|
+
/**运行所有匹配的的js/ts文件
|
34
|
+
* @async
|
35
|
+
* @param dir - 基础目录
|
36
|
+
* @param opt - 可选参数
|
37
|
+
* @param opt.include - 包含的glob 默认 index.ts
|
38
|
+
* @param opt.exclude - 忽略的glob
|
39
|
+
* @param opt.project - tsconfig路径
|
40
|
+
*/
|
41
|
+
export function matchNode(dir: string, opt?: BuildMatchOpt): Promise<void>;
|
42
|
+
/**运行所有js/ts文件
|
43
|
+
* @async
|
44
|
+
* @param filepath - 需要运行的文件
|
45
|
+
* @param opt - 可选参数
|
46
|
+
* @param opt.project - tsconfig路径
|
47
|
+
*/
|
48
|
+
export function batchNode(filepath: string | string[], opt?: BatchNodeOpt): Promise<void>;
|
49
|
+
/**将codeText写入对应region
|
50
|
+
* @param regionId - 区域id
|
51
|
+
* @param codeText - 文本
|
52
|
+
* @param targetPath - 目标文件 默认为去除".macro"的同名文件
|
53
|
+
*/
|
54
|
+
export function $macro(regionId: string, codeText: string, targetPath?: string): Promise<void>;
|
55
|
+
export {};
|
56
|
+
}
|
@@ -0,0 +1,170 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
15
|
+
}) : function(o, v) {
|
16
|
+
o["default"] = v;
|
17
|
+
});
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
19
|
+
if (mod && mod.__esModule) return mod;
|
20
|
+
var result = {};
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
22
|
+
__setModuleDefault(result, mod);
|
23
|
+
return result;
|
24
|
+
};
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
26
|
+
exports.UtilDT = void 0;
|
27
|
+
const path = __importStar(require("path"));
|
28
|
+
const TJS = __importStar(require("typescript-json-schema"));
|
29
|
+
const fs = __importStar(require("fs"));
|
30
|
+
const utils_1 = require("@zwa73/utils");
|
31
|
+
var UtilDT;
|
32
|
+
(function (UtilDT) {
|
33
|
+
/**生成匹配的文件的所有type的schema
|
34
|
+
* @async
|
35
|
+
* @param dir - 基础目录
|
36
|
+
* @param opt - 可选参数
|
37
|
+
* @param opt.include - 包含的glob 默认 **\/\*.schema.ts
|
38
|
+
* @param opt.exclude - 忽略的glob
|
39
|
+
* @param opt.coverDefine - 将会覆盖 definitions 对应内容的表
|
40
|
+
* @param opt.project - tsconfig路径
|
41
|
+
* @param opt.outDir - schema输出路径目录 默认 ./schema/
|
42
|
+
*/
|
43
|
+
async function buildSchema(dir, opt) {
|
44
|
+
//生成
|
45
|
+
const settings = {
|
46
|
+
required: true,
|
47
|
+
aliasRef: true,
|
48
|
+
tsNodeRegister: true,
|
49
|
+
};
|
50
|
+
const compilerOptions = {
|
51
|
+
strictNullChecks: true,
|
52
|
+
};
|
53
|
+
if (opt?.project)
|
54
|
+
Object.assign(compilerOptions, (await utils_1.UtilFT.loadJSONFile(opt.project)).compilerOptions);
|
55
|
+
const files = utils_1.UtilFT.fileSearchGlob(dir, opt?.include ?? "**/*.schema.ts", { ingore: opt?.exclude });
|
56
|
+
const program = TJS.getProgramFromFiles(files, compilerOptions);
|
57
|
+
const schema = TJS.generateSchema(program, "*", settings);
|
58
|
+
const outDir = opt?.outDir ?? path.join(process.cwd(), 'schema');
|
59
|
+
const schemasPath = path.join(outDir, 'schemas.json');
|
60
|
+
//进行预处理并展开
|
61
|
+
await expandSchema(schemasPath, schema, opt?.coverDefine ?? {});
|
62
|
+
}
|
63
|
+
UtilDT.buildSchema = buildSchema;
|
64
|
+
/**展开schema以供使用 */
|
65
|
+
async function expandSchema(schemasPath, schema, coverDefine) {
|
66
|
+
//覆盖
|
67
|
+
coverObj(schema["definitions"], coverDefine);
|
68
|
+
//替换SchemaString标识符
|
69
|
+
schema = JSON.parse(JSON.stringify(schema).replace(/\^\.\*SchemaString\$/g, '^.*$'));
|
70
|
+
const plist = [];
|
71
|
+
plist.push(utils_1.UtilFT.writeJSONFile(schemasPath, schema));
|
72
|
+
const definitions = schema["definitions"];
|
73
|
+
//展开定义
|
74
|
+
for (const typeName in definitions) {
|
75
|
+
const schema = definitions[typeName];
|
76
|
+
//展开所有object与忽略检测的类型
|
77
|
+
if (!(schema.type == "object" || schema.type == "array" || schema.type == undefined))
|
78
|
+
continue;
|
79
|
+
if ((/^.+_[0-9]/).test(typeName) || (/^{./).test(typeName))
|
80
|
+
continue;
|
81
|
+
const basename = path.basename(schemasPath);
|
82
|
+
const tpath = path.join(path.dirname(schemasPath), `${typeName}.schema.json`);
|
83
|
+
if (!utils_1.UtilFT.isValidFilePath(tpath))
|
84
|
+
continue;
|
85
|
+
plist.push(utils_1.UtilFT.writeJSONFile(tpath, {
|
86
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
87
|
+
"$ref": `${basename}#/definitions/${typeName}`
|
88
|
+
}));
|
89
|
+
}
|
90
|
+
await Promise.all(plist);
|
91
|
+
}
|
92
|
+
/**覆盖object */
|
93
|
+
function coverObj(base, cover) {
|
94
|
+
for (const k in cover) {
|
95
|
+
const v = cover[k];
|
96
|
+
if (typeof v === "object" && !Array.isArray(v) &&
|
97
|
+
typeof base[k] === "object" && !Array.isArray(base[k]))
|
98
|
+
coverObj(base[k], v);
|
99
|
+
else
|
100
|
+
base[k] = v;
|
101
|
+
}
|
102
|
+
}
|
103
|
+
/**运行所有匹配的的js/ts文件
|
104
|
+
* @async
|
105
|
+
* @param dir - 基础目录
|
106
|
+
* @param opt - 可选参数
|
107
|
+
* @param opt.include - 包含的glob 默认 index.ts
|
108
|
+
* @param opt.exclude - 忽略的glob
|
109
|
+
* @param opt.project - tsconfig路径
|
110
|
+
*/
|
111
|
+
async function matchNode(dir, opt) {
|
112
|
+
const files = utils_1.UtilFT.fileSearchGlob(dir, opt?.include ?? "index.ts", { ingore: opt?.exclude });
|
113
|
+
await batchNode(files, opt);
|
114
|
+
}
|
115
|
+
UtilDT.matchNode = matchNode;
|
116
|
+
/**运行所有js/ts文件
|
117
|
+
* @async
|
118
|
+
* @param filepath - 需要运行的文件
|
119
|
+
* @param opt - 可选参数
|
120
|
+
* @param opt.project - tsconfig路径
|
121
|
+
*/
|
122
|
+
async function batchNode(filepath, opt) {
|
123
|
+
// 确保 filepath 总是一个数组
|
124
|
+
if (!Array.isArray(filepath))
|
125
|
+
filepath = [filepath];
|
126
|
+
// 将所有的相对路径转换为绝对路径
|
127
|
+
const absolutePaths = filepath.map(fp => path.resolve(process.cwd(), fp).replaceAll("\\", "/"));
|
128
|
+
// 创建一个字符串,其中包含所有文件的 require 语句
|
129
|
+
const requires = absolutePaths.map(fp => `require('${fp}');`).join('\n');
|
130
|
+
// 创建并执行 ts-node 命令
|
131
|
+
const cmd = `ts-node -r tsconfig-paths/register -e "${requires}" ${opt?.project ? `-P "${opt.project}"` : ""}`;
|
132
|
+
await utils_1.UtilFunc.exec(cmd, { outlvl: 'info', errlvl: 'warn' });
|
133
|
+
}
|
134
|
+
UtilDT.batchNode = batchNode;
|
135
|
+
/**将codeText写入对应region
|
136
|
+
* @param regionId - 区域id
|
137
|
+
* @param codeText - 文本
|
138
|
+
* @param targetPath - 目标文件 默认为去除".macro"的同名文件
|
139
|
+
*/
|
140
|
+
async function $macro(regionId, codeText, targetPath) {
|
141
|
+
const loc = (0, utils_1.getFuncLoc)(2);
|
142
|
+
if (!loc) {
|
143
|
+
utils_1.SLogger.error(`UtilDT.$macro 未能找到函数位置`);
|
144
|
+
return;
|
145
|
+
}
|
146
|
+
;
|
147
|
+
const baseFilePath = targetPath
|
148
|
+
? path.resolve(process.cwd(), targetPath)
|
149
|
+
: loc.filePath.replace(/(.+)\.macro\.(js|ts|cjs|mjs)$/, "$1.$2");
|
150
|
+
const queuefunc = async () => {
|
151
|
+
if (!(await utils_1.UtilFT.pathExists(baseFilePath))) {
|
152
|
+
utils_1.SLogger.error(`UtilDT.$macro ${baseFilePath} 不存在`);
|
153
|
+
return;
|
154
|
+
}
|
155
|
+
;
|
156
|
+
const text = await fs.promises.readFile(baseFilePath, 'utf-8');
|
157
|
+
const regex = new RegExp(`(//#region ${regionId}\\s*?\\n)([\\s\\S]+?)(//#endregion)`);
|
158
|
+
//console.log(text)
|
159
|
+
//console.log(regex)
|
160
|
+
if (!regex.test(text)) {
|
161
|
+
utils_1.SLogger.error(`UtilDT.$macro 无法找到区域 ${regionId}`);
|
162
|
+
return;
|
163
|
+
}
|
164
|
+
const ntext = text.replace(regex, `$1${codeText}\n$3`);
|
165
|
+
await fs.promises.writeFile(baseFilePath, ntext, 'utf-8');
|
166
|
+
};
|
167
|
+
await utils_1.UtilFunc.queueProc(path.posix.normalize(baseFilePath.replaceAll("\\", "/")), queuefunc);
|
168
|
+
}
|
169
|
+
UtilDT.$macro = $macro;
|
170
|
+
})(UtilDT || (exports.UtilDT = UtilDT = {}));
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
@@ -14,5 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
15
|
};
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
-
__exportStar(require("./
|
17
|
+
__exportStar(require("./UtilDevTool"), exports);
|
18
18
|
__exportStar(require("./Command"), exports);
|
19
|
+
__exportStar(require("./QuickFunc"), exports);
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@zwa73/dev-utils",
|
3
|
-
"version": "1.0.
|
3
|
+
"version": "1.0.28",
|
4
4
|
"description": "编译与调试工具",
|
5
5
|
"main": "index.js",
|
6
6
|
"scripts": {
|
@@ -15,7 +15,7 @@
|
|
15
15
|
"author": "zwa73",
|
16
16
|
"license": "ISC",
|
17
17
|
"dependencies": {
|
18
|
-
"@zwa73/utils": "^1.0.
|
18
|
+
"@zwa73/utils": "^1.0.114",
|
19
19
|
"commander": "^11.1.0",
|
20
20
|
"ts-node": "^10.9.2",
|
21
21
|
"tsconfig-paths": "^4.2.0",
|
@@ -0,0 +1,18 @@
|
|
1
|
+
import { Command } from "commander";
|
2
|
+
import { UtilDT } from "@src/UtilDevTool";
|
3
|
+
|
4
|
+
/**构造macro */
|
5
|
+
export const CmdBuildMacro = (program:Command) => program
|
6
|
+
.command("Build-Macro")
|
7
|
+
.alias("buildmacro")
|
8
|
+
.description("生成根据macro生成代码")
|
9
|
+
.option("-i, --include <glob>", "包含的glob 默认 src/**/*.macro.ts","src/**/*.macro.ts")
|
10
|
+
.option("-g, --exclude <glob>", "忽略的glob")
|
11
|
+
.option("-p, --project <path>", "tsconfig路径 默认tsconfig.json","tsconfig.json")
|
12
|
+
.action(async (opt) => {
|
13
|
+
await UtilDT.matchNode(process.cwd(), {
|
14
|
+
include: opt.include,
|
15
|
+
exclude: opt.ignore,
|
16
|
+
project: opt.project,
|
17
|
+
});
|
18
|
+
});
|
@@ -0,0 +1,20 @@
|
|
1
|
+
import { Command } from "commander";
|
2
|
+
import { UtilDT } from "@src/UtilDevTool";
|
3
|
+
|
4
|
+
/**生成schema */
|
5
|
+
export const CmdBuildSchema = (program:Command) => program
|
6
|
+
.command("Build-Schema")
|
7
|
+
.alias("buildschema")
|
8
|
+
.description("生成匹配的文件的所有type的schema")
|
9
|
+
.option("-i, --include <glob>", "包含的glob 默认 src/**/*.schema.ts","src/**/*.schema.ts")
|
10
|
+
.option("-g, --exclude <glob>", "忽略的glob")
|
11
|
+
.option("-p, --project <path>", "tsconfig路径 默认tsconfig.json","tsconfig.json")
|
12
|
+
.option("-o, --outDir <dir>", "schema输出路径目录 默认 ./schema/")
|
13
|
+
.action(async (opt) => {
|
14
|
+
await UtilDT.buildSchema(process.cwd(), {
|
15
|
+
include: opt.include,
|
16
|
+
exclude: opt.ignore,
|
17
|
+
project: opt.project,
|
18
|
+
outDir: opt.outDir,
|
19
|
+
});
|
20
|
+
});
|
package/src/Command/Init.ts
CHANGED
package/src/Command/Node.ts
CHANGED
@@ -4,12 +4,13 @@ import { SLogger, UtilFunc } from '@zwa73/utils';
|
|
4
4
|
|
5
5
|
/**编译并运行某个文件 */
|
6
6
|
export const CmdNode = (program:Command)=>program
|
7
|
-
.command('
|
7
|
+
.command('Node <path|path[]>')
|
8
|
+
.alias('node')
|
8
9
|
.option('-p, --project <path>','tsconfig的目录')
|
9
10
|
.description('编译并运行某个文件')
|
10
11
|
.action(async (arg, opt) => {
|
11
12
|
SLogger.info(`编译并运行: ${arg}`);
|
12
13
|
let cmd = `ts-node -r tsconfig-paths/register ${arg}`;
|
13
|
-
if(opt.project) cmd+=`-
|
14
|
+
if(opt.project) cmd+=`-P ${opt.project}`;
|
14
15
|
await UtilFunc.exec(cmd,{outlvl:'info',errlvl:'error'});
|
15
16
|
});
|
package/src/Command/Release.ts
CHANGED
@@ -44,7 +44,8 @@ async function updateVersion(newVersion?: string): Promise<string> {
|
|
44
44
|
}
|
45
45
|
/**更新版本号并发布npm包 */
|
46
46
|
export const CmdRelease = (program: Command) =>program
|
47
|
-
.command("
|
47
|
+
.command("Release")
|
48
|
+
.alias("release")
|
48
49
|
.description("更新版本号并发布包")
|
49
50
|
.option("-v, --version <version>", "指定发布的版本号 `${number}.${number}.${number}`")
|
50
51
|
.option("-a, --access <access>", "npm publish 的 access 参数","public")
|
package/src/Command/Route.ts
CHANGED
@@ -5,6 +5,8 @@ import { CmdNode } from './Node';
|
|
5
5
|
import { CmdRelease } from './Release';
|
6
6
|
import { CmdMapPath } from './MapPath';
|
7
7
|
import { CmdScanDups } from './ScanDups';
|
8
|
+
import { CmdBuildSchema } from './BuildSchema';
|
9
|
+
import { CmdBuildMacro } from './BuildMacro';
|
8
10
|
|
9
11
|
export async function cliRoute(){
|
10
12
|
CmdInit(program);
|
@@ -12,5 +14,7 @@ export async function cliRoute(){
|
|
12
14
|
CmdRelease(program);
|
13
15
|
CmdMapPath(program);
|
14
16
|
CmdScanDups(program);
|
17
|
+
CmdBuildSchema(program);
|
18
|
+
CmdBuildMacro(program);
|
15
19
|
program.parse(process.argv);
|
16
20
|
}
|
package/src/QuickFunc.ts
ADDED
@@ -0,0 +1,167 @@
|
|
1
|
+
import * as path from 'path';
|
2
|
+
import * as TJS from 'typescript-json-schema';
|
3
|
+
import * as fs from 'fs';
|
4
|
+
import { JObject, SLogger, UtilFT, UtilFunc, getFuncLoc } from '@zwa73/utils';
|
5
|
+
|
6
|
+
export namespace UtilDT{
|
7
|
+
|
8
|
+
/**执行js/ts的可选项 */
|
9
|
+
type BatchNodeOpt = Partial<{
|
10
|
+
/**tsconfig路径 */
|
11
|
+
project:string;
|
12
|
+
}>
|
13
|
+
|
14
|
+
/**build任意ts文件的可选项 */
|
15
|
+
type BuildMatchOpt = Partial<{
|
16
|
+
/**包含的glob */
|
17
|
+
include:string|string[];
|
18
|
+
/**忽略的glob */
|
19
|
+
exclude:string|string[];
|
20
|
+
}>&BatchNodeOpt;
|
21
|
+
|
22
|
+
/**build schema的可选项 */
|
23
|
+
type BuildSchemaOpt = Partial<{
|
24
|
+
/**将会覆盖 definitions 对应内容的表 */
|
25
|
+
coverDefine:JObject;
|
26
|
+
/**schema输出路径目录 如 ./schema/ */
|
27
|
+
outDir:string;
|
28
|
+
}>&BuildMatchOpt;
|
29
|
+
|
30
|
+
|
31
|
+
|
32
|
+
/**生成匹配的文件的所有type的schema
|
33
|
+
* @async
|
34
|
+
* @param dir - 基础目录
|
35
|
+
* @param opt - 可选参数
|
36
|
+
* @param opt.include - 包含的glob 默认 **\/\*.schema.ts
|
37
|
+
* @param opt.exclude - 忽略的glob
|
38
|
+
* @param opt.coverDefine - 将会覆盖 definitions 对应内容的表
|
39
|
+
* @param opt.project - tsconfig路径
|
40
|
+
* @param opt.outDir - schema输出路径目录 默认 ./schema/
|
41
|
+
*/
|
42
|
+
export async function buildSchema(dir:string,opt?:BuildSchemaOpt){
|
43
|
+
//生成
|
44
|
+
const settings: TJS.PartialArgs = {
|
45
|
+
required: true,
|
46
|
+
aliasRef:true,
|
47
|
+
tsNodeRegister:true,
|
48
|
+
};
|
49
|
+
const compilerOptions: TJS.CompilerOptions = {
|
50
|
+
strictNullChecks: true,
|
51
|
+
};
|
52
|
+
if(opt?.project) Object.assign(compilerOptions,(await UtilFT.loadJSONFile(opt.project) as any).compilerOptions);
|
53
|
+
const files = UtilFT.fileSearchGlob(dir,opt?.include ?? "**/*.schema.ts",{ingore:opt?.exclude});
|
54
|
+
|
55
|
+
const program = TJS.getProgramFromFiles(
|
56
|
+
files,
|
57
|
+
compilerOptions,
|
58
|
+
);
|
59
|
+
const schema = TJS.generateSchema(program, "*", settings) as JObject;
|
60
|
+
const outDir = opt?.outDir ?? path.join(process.cwd(),'schema');
|
61
|
+
const schemasPath = path.join(outDir,'schemas.json');
|
62
|
+
//进行预处理并展开
|
63
|
+
await expandSchema(schemasPath,schema,opt?.coverDefine??{});
|
64
|
+
}
|
65
|
+
/**展开schema以供使用 */
|
66
|
+
async function expandSchema(schemasPath:string,schema:JObject,coverDefine:JObject){
|
67
|
+
//覆盖
|
68
|
+
coverObj(schema["definitions"] as JObject,coverDefine);
|
69
|
+
//替换SchemaString标识符
|
70
|
+
schema = JSON.parse(JSON.stringify(schema).replace(/\^\.\*SchemaString\$/g,'^.*$'));
|
71
|
+
const plist:Promise<void>[] = [];
|
72
|
+
plist.push(UtilFT.writeJSONFile(schemasPath,schema));
|
73
|
+
|
74
|
+
const definitions = schema["definitions"] as Record<string,JObject>;
|
75
|
+
//展开定义
|
76
|
+
for(const typeName in definitions){
|
77
|
+
const schema = definitions[typeName];
|
78
|
+
//展开所有object与忽略检测的类型
|
79
|
+
if(!(schema.type == "object" || schema.type == "array" || schema.type == undefined)) continue;
|
80
|
+
if((/^.+_[0-9]/).test(typeName) || (/^{./).test(typeName)) continue;
|
81
|
+
|
82
|
+
const basename = path.basename(schemasPath);
|
83
|
+
const tpath = path.join(path.dirname(schemasPath),`${typeName}.schema.json`);
|
84
|
+
if(!UtilFT.isValidFilePath(tpath)) continue;
|
85
|
+
plist.push(UtilFT.writeJSONFile(tpath,{
|
86
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
87
|
+
"$ref": `${basename}#/definitions/${typeName}`
|
88
|
+
}));
|
89
|
+
}
|
90
|
+
await Promise.all(plist);
|
91
|
+
}
|
92
|
+
/**覆盖object */
|
93
|
+
function coverObj(base:JObject,cover:JObject){
|
94
|
+
for(const k in cover){
|
95
|
+
const v = cover[k];
|
96
|
+
if( typeof v === "object" && !Array.isArray(v) &&
|
97
|
+
typeof base[k] === "object" && !Array.isArray(base[k]) )
|
98
|
+
coverObj(base[k] as JObject,v as JObject);
|
99
|
+
else
|
100
|
+
base[k] = v;
|
101
|
+
}
|
102
|
+
}
|
103
|
+
|
104
|
+
/**运行所有匹配的的js/ts文件
|
105
|
+
* @async
|
106
|
+
* @param dir - 基础目录
|
107
|
+
* @param opt - 可选参数
|
108
|
+
* @param opt.include - 包含的glob 默认 index.ts
|
109
|
+
* @param opt.exclude - 忽略的glob
|
110
|
+
* @param opt.project - tsconfig路径
|
111
|
+
*/
|
112
|
+
export async function matchNode(dir:string,opt?:BuildMatchOpt) {
|
113
|
+
const files = UtilFT.fileSearchGlob(dir,opt?.include ?? "index.ts",{ingore:opt?.exclude});
|
114
|
+
await batchNode(files,opt);
|
115
|
+
}
|
116
|
+
/**运行所有js/ts文件
|
117
|
+
* @async
|
118
|
+
* @param filepath - 需要运行的文件
|
119
|
+
* @param opt - 可选参数
|
120
|
+
* @param opt.project - tsconfig路径
|
121
|
+
*/
|
122
|
+
export async function batchNode(filepath:string|string[],opt?:BatchNodeOpt) {
|
123
|
+
// 确保 filepath 总是一个数组
|
124
|
+
if (!Array.isArray(filepath)) filepath = [filepath];
|
125
|
+
// 将所有的相对路径转换为绝对路径
|
126
|
+
const absolutePaths = filepath.map(fp => path.resolve(process.cwd(), fp).replaceAll("\\","/"));
|
127
|
+
// 创建一个字符串,其中包含所有文件的 require 语句
|
128
|
+
const requires = absolutePaths.map(fp => `require('${fp}');`).join('\n');
|
129
|
+
// 创建并执行 ts-node 命令
|
130
|
+
const cmd = `ts-node -r tsconfig-paths/register -e "${requires}" ${opt?.project?`-P "${opt.project}"` : ""}`;
|
131
|
+
await UtilFunc.exec(cmd, { outlvl: 'info', errlvl: 'warn' });
|
132
|
+
}
|
133
|
+
|
134
|
+
|
135
|
+
/**将codeText写入对应region
|
136
|
+
* @param regionId - 区域id
|
137
|
+
* @param codeText - 文本
|
138
|
+
* @param targetPath - 目标文件 默认为去除".macro"的同名文件
|
139
|
+
*/
|
140
|
+
export async function $macro(regionId:string,codeText:string,targetPath?:string){
|
141
|
+
const loc = getFuncLoc(2);
|
142
|
+
if(!loc){
|
143
|
+
SLogger.error(`UtilDT.$macro 未能找到函数位置`);
|
144
|
+
return
|
145
|
+
};
|
146
|
+
const baseFilePath = targetPath
|
147
|
+
? path.resolve(process.cwd(),targetPath)
|
148
|
+
: loc.filePath.replace(/(.+)\.macro\.(js|ts|cjs|mjs)$/,"$1.$2");
|
149
|
+
const queuefunc = async ()=>{
|
150
|
+
if(!(await UtilFT.pathExists(baseFilePath))) {
|
151
|
+
SLogger.error(`UtilDT.$macro ${baseFilePath} 不存在`);
|
152
|
+
return
|
153
|
+
};
|
154
|
+
const text = await fs.promises.readFile(baseFilePath,'utf-8');
|
155
|
+
const regex = new RegExp(`(//#region ${regionId}\\s*?\\n)([\\s\\S]+?)(//#endregion)`);
|
156
|
+
//console.log(text)
|
157
|
+
//console.log(regex)
|
158
|
+
if (!regex.test(text)) {
|
159
|
+
SLogger.error(`UtilDT.$macro 无法找到区域 ${regionId}`);
|
160
|
+
return;
|
161
|
+
}
|
162
|
+
const ntext = text.replace(regex, `$1${codeText}\n$3`);
|
163
|
+
await fs.promises.writeFile(baseFilePath, ntext, 'utf-8');
|
164
|
+
}
|
165
|
+
await UtilFunc.queueProc(path.posix.normalize(baseFilePath.replaceAll("\\","/")),queuefunc);
|
166
|
+
}
|
167
|
+
}
|
package/src/index.ts
CHANGED
@@ -1,2 +1,3 @@
|
|
1
|
-
export * from './
|
2
|
-
export * from './Command';
|
1
|
+
export * from './UtilDevTool';
|
2
|
+
export * from './Command';
|
3
|
+
export * from "./QuickFunc";
|
package/dist/UtilSchemaTool.d.ts
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
import { JObject } from '@zwa73/utils';
|
2
|
-
export declare namespace UtilST {
|
3
|
-
/**构造Schema
|
4
|
-
* @async
|
5
|
-
* @param configPath - tsconfig路径
|
6
|
-
* @param outDir - schema文件夹路径 如 ./schema/
|
7
|
-
* @param coverDefine - 将会覆盖 definitions 对应内容的表
|
8
|
-
*/
|
9
|
-
export function builAllSchema(configPath: string, outDir: string, coverDefine?: JObject): Promise<void>;
|
10
|
-
/**buildMatchSchema可选项 */
|
11
|
-
type BuildMatchSchemaOpt = Partial<{
|
12
|
-
/**忽略某些glob */
|
13
|
-
ignore?: string | string[];
|
14
|
-
/** */
|
15
|
-
coverDefine: JObject;
|
16
|
-
}>;
|
17
|
-
/**生成匹配的文件的所有type的schema
|
18
|
-
* @async
|
19
|
-
* @param dir - 目录
|
20
|
-
* @param globPattern - glob匹配符
|
21
|
-
* @param outDir - schema输出路径目录 如 ./schema/
|
22
|
-
* @param opt - 可选参数
|
23
|
-
* @param opt.ignore - glob忽略匹配符
|
24
|
-
* @param opt.coverDefine - 将会覆盖 definitions 对应内容的表
|
25
|
-
*/
|
26
|
-
export function buildMatchSchema(dir: string, globPattern: string | string[], outDir: string, opt?: BuildMatchSchemaOpt): Promise<void>;
|
27
|
-
export {};
|
28
|
-
}
|
package/dist/UtilSchemaTool.js
DELETED
@@ -1,112 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
-
if (k2 === undefined) k2 = k;
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
-
}
|
8
|
-
Object.defineProperty(o, k2, desc);
|
9
|
-
}) : (function(o, m, k, k2) {
|
10
|
-
if (k2 === undefined) k2 = k;
|
11
|
-
o[k2] = m[k];
|
12
|
-
}));
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
15
|
-
}) : function(o, v) {
|
16
|
-
o["default"] = v;
|
17
|
-
});
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
19
|
-
if (mod && mod.__esModule) return mod;
|
20
|
-
var result = {};
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
22
|
-
__setModuleDefault(result, mod);
|
23
|
-
return result;
|
24
|
-
};
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
26
|
-
exports.UtilST = void 0;
|
27
|
-
const path = __importStar(require("path"));
|
28
|
-
const TJS = __importStar(require("typescript-json-schema"));
|
29
|
-
const utils_1 = require("@zwa73/utils");
|
30
|
-
var UtilST;
|
31
|
-
(function (UtilST) {
|
32
|
-
/**构造Schema
|
33
|
-
* @async
|
34
|
-
* @param configPath - tsconfig路径
|
35
|
-
* @param outDir - schema文件夹路径 如 ./schema/
|
36
|
-
* @param coverDefine - 将会覆盖 definitions 对应内容的表
|
37
|
-
*/
|
38
|
-
async function builAllSchema(configPath, outDir, coverDefine = {}) {
|
39
|
-
outDir = path.join(outDir, "schemas.json");
|
40
|
-
await utils_1.UtilFunc.exec(`typescript-json-schema ${configPath} * --out ${outDir} --required --strictNullChecks --aliasRefs`, { outlvl: 'info', errlvl: 'error' });
|
41
|
-
const schema = (await utils_1.UtilFT.loadJSONFile(outDir));
|
42
|
-
//进行预处理并展开
|
43
|
-
await expandSchema(outDir, schema, coverDefine);
|
44
|
-
}
|
45
|
-
UtilST.builAllSchema = builAllSchema;
|
46
|
-
/**生成匹配的文件的所有type的schema
|
47
|
-
* @async
|
48
|
-
* @param dir - 目录
|
49
|
-
* @param globPattern - glob匹配符
|
50
|
-
* @param outDir - schema输出路径目录 如 ./schema/
|
51
|
-
* @param opt - 可选参数
|
52
|
-
* @param opt.ignore - glob忽略匹配符
|
53
|
-
* @param opt.coverDefine - 将会覆盖 definitions 对应内容的表
|
54
|
-
*/
|
55
|
-
async function buildMatchSchema(dir, globPattern, outDir, opt) {
|
56
|
-
//生成
|
57
|
-
const settings = {
|
58
|
-
required: true,
|
59
|
-
aliasRef: true,
|
60
|
-
};
|
61
|
-
const compilerOptions = {
|
62
|
-
strictNullChecks: true,
|
63
|
-
};
|
64
|
-
const files = utils_1.UtilFT.fileSearchGlob(dir, globPattern, { ingore: opt?.ignore });
|
65
|
-
const program = TJS.getProgramFromFiles(files, compilerOptions);
|
66
|
-
const schema = TJS.generateSchema(program, "*", settings);
|
67
|
-
outDir = outDir ?? path.join(process.cwd(), 'schema');
|
68
|
-
const schemasPath = path.join(outDir, 'schemas.json');
|
69
|
-
//进行预处理并展开
|
70
|
-
await expandSchema(schemasPath, schema, opt?.coverDefine ?? {});
|
71
|
-
}
|
72
|
-
UtilST.buildMatchSchema = buildMatchSchema;
|
73
|
-
/**展开schema以供使用 */
|
74
|
-
async function expandSchema(schemasPath, schema, coverDefine) {
|
75
|
-
//覆盖
|
76
|
-
coverObj(schema["definitions"], coverDefine);
|
77
|
-
//替换SchemaString标识符
|
78
|
-
schema = JSON.parse(JSON.stringify(schema).replace(/\^\.\*SchemaString\$/g, '^.*$'));
|
79
|
-
const plist = [];
|
80
|
-
plist.push(utils_1.UtilFT.writeJSONFile(schemasPath, schema));
|
81
|
-
const definitions = schema["definitions"];
|
82
|
-
//展开定义
|
83
|
-
for (const typeName in definitions) {
|
84
|
-
const schema = definitions[typeName];
|
85
|
-
//展开所有object与忽略检测的类型
|
86
|
-
if (!(schema.type == "object" || schema.type == "array" || schema.type == undefined))
|
87
|
-
continue;
|
88
|
-
if ((/^.+_[0-9]/).test(typeName) || (/^{./).test(typeName))
|
89
|
-
continue;
|
90
|
-
const basename = path.basename(schemasPath);
|
91
|
-
const tpath = path.join(path.dirname(schemasPath), `${typeName}.schema.json`);
|
92
|
-
if (!utils_1.UtilFT.isValidFilePath(tpath))
|
93
|
-
continue;
|
94
|
-
plist.push(utils_1.UtilFT.writeJSONFile(tpath, {
|
95
|
-
"$schema": "http://json-schema.org/draft-07/schema#",
|
96
|
-
"$ref": `${basename}#/definitions/${typeName}`
|
97
|
-
}));
|
98
|
-
}
|
99
|
-
await Promise.all(plist);
|
100
|
-
}
|
101
|
-
/**覆盖object */
|
102
|
-
function coverObj(base, cover) {
|
103
|
-
for (const k in cover) {
|
104
|
-
const v = cover[k];
|
105
|
-
if (typeof v === "object" && !Array.isArray(v) &&
|
106
|
-
typeof base[k] === "object" && !Array.isArray(base[k]))
|
107
|
-
coverObj(base[k], v);
|
108
|
-
else
|
109
|
-
base[k] = v;
|
110
|
-
}
|
111
|
-
}
|
112
|
-
})(UtilST || (exports.UtilST = UtilST = {}));
|
package/src/UtilSchemaTool.ts
DELETED
@@ -1,95 +0,0 @@
|
|
1
|
-
import * as path from 'path';
|
2
|
-
import * as TJS from 'typescript-json-schema';
|
3
|
-
|
4
|
-
import { JObject, SLogger, UtilFT, UtilFunc } from '@zwa73/utils';
|
5
|
-
|
6
|
-
export namespace UtilST{
|
7
|
-
/**构造Schema
|
8
|
-
* @async
|
9
|
-
* @param configPath - tsconfig路径
|
10
|
-
* @param outDir - schema文件夹路径 如 ./schema/
|
11
|
-
* @param coverDefine - 将会覆盖 definitions 对应内容的表
|
12
|
-
*/
|
13
|
-
export async function builAllSchema(configPath:string,outDir:string,coverDefine:JObject={}){
|
14
|
-
outDir = path.join(outDir,"schemas.json");
|
15
|
-
await UtilFunc.exec(`typescript-json-schema ${configPath} * --out ${outDir} --required --strictNullChecks --aliasRefs`,{outlvl:'info',errlvl:'error'});
|
16
|
-
const schema = (await UtilFT.loadJSONFile(outDir)) as any;
|
17
|
-
//进行预处理并展开
|
18
|
-
await expandSchema(outDir,schema,coverDefine);
|
19
|
-
}
|
20
|
-
|
21
|
-
/**buildMatchSchema可选项 */
|
22
|
-
type BuildMatchSchemaOpt = Partial<{
|
23
|
-
/**忽略某些glob */
|
24
|
-
ignore?:string|string[],
|
25
|
-
/** */
|
26
|
-
coverDefine:JObject,
|
27
|
-
}>
|
28
|
-
/**生成匹配的文件的所有type的schema
|
29
|
-
* @async
|
30
|
-
* @param dir - 目录
|
31
|
-
* @param globPattern - glob匹配符
|
32
|
-
* @param outDir - schema输出路径目录 如 ./schema/
|
33
|
-
* @param opt - 可选参数
|
34
|
-
* @param opt.ignore - glob忽略匹配符
|
35
|
-
* @param opt.coverDefine - 将会覆盖 definitions 对应内容的表
|
36
|
-
*/
|
37
|
-
export async function buildMatchSchema(dir:string,globPattern:string|string[],outDir:string,opt?:BuildMatchSchemaOpt){
|
38
|
-
//生成
|
39
|
-
const settings: TJS.PartialArgs = {
|
40
|
-
required: true,
|
41
|
-
aliasRef:true,
|
42
|
-
};
|
43
|
-
const compilerOptions: TJS.CompilerOptions = {
|
44
|
-
strictNullChecks: true,
|
45
|
-
};
|
46
|
-
const files = UtilFT.fileSearchGlob(dir,globPattern,{ingore:opt?.ignore});
|
47
|
-
const program = TJS.getProgramFromFiles(
|
48
|
-
files,
|
49
|
-
compilerOptions,
|
50
|
-
);
|
51
|
-
const schema = TJS.generateSchema(program, "*", settings) as any;
|
52
|
-
outDir = outDir??path.join(process.cwd(),'schema');
|
53
|
-
const schemasPath = path.join(outDir,'schemas.json');
|
54
|
-
//进行预处理并展开
|
55
|
-
await expandSchema(schemasPath,schema,opt?.coverDefine??{});
|
56
|
-
}
|
57
|
-
/**展开schema以供使用 */
|
58
|
-
async function expandSchema(schemasPath:string,schema:any,coverDefine:JObject){
|
59
|
-
//覆盖
|
60
|
-
coverObj(schema["definitions"],coverDefine);
|
61
|
-
//替换SchemaString标识符
|
62
|
-
schema = JSON.parse(JSON.stringify(schema).replace(/\^\.\*SchemaString\$/g,'^.*$'));
|
63
|
-
const plist:Promise<void>[] = [];
|
64
|
-
plist.push(UtilFT.writeJSONFile(schemasPath,schema));
|
65
|
-
|
66
|
-
const definitions = schema["definitions"] as Record<string,JObject>;
|
67
|
-
//展开定义
|
68
|
-
for(const typeName in definitions){
|
69
|
-
const schema = definitions[typeName];
|
70
|
-
//展开所有object与忽略检测的类型
|
71
|
-
if(!(schema.type == "object" || schema.type == "array" || schema.type == undefined)) continue;
|
72
|
-
if((/^.+_[0-9]/).test(typeName) || (/^{./).test(typeName)) continue;
|
73
|
-
|
74
|
-
const basename = path.basename(schemasPath);
|
75
|
-
const tpath = path.join(path.dirname(schemasPath),`${typeName}.schema.json`);
|
76
|
-
if(!UtilFT.isValidFilePath(tpath)) continue;
|
77
|
-
plist.push(UtilFT.writeJSONFile(tpath,{
|
78
|
-
"$schema": "http://json-schema.org/draft-07/schema#",
|
79
|
-
"$ref": `${basename}#/definitions/${typeName}`
|
80
|
-
}));
|
81
|
-
}
|
82
|
-
await Promise.all(plist);
|
83
|
-
}
|
84
|
-
/**覆盖object */
|
85
|
-
function coverObj(base:JObject,cover:JObject){
|
86
|
-
for(const k in cover){
|
87
|
-
const v = cover[k];
|
88
|
-
if( typeof v === "object" && !Array.isArray(v) &&
|
89
|
-
typeof base[k] === "object" && !Array.isArray(base[k]) )
|
90
|
-
coverObj(base[k] as JObject,v as JObject);
|
91
|
-
else
|
92
|
-
base[k] = v;
|
93
|
-
}
|
94
|
-
}
|
95
|
-
}
|