@zwa73/dev-utils 1.0.80 → 1.0.82
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/bin/cli +1 -1
- package/data/template/base/gitignore +3 -0
- package/data/template/base/package.json +25 -0
- package/data/template/base/scripts/compile.ps1 +8 -0
- package/data/template/base/src/macro.macro.ts +6 -0
- package/data/template/base/tsconfig.cjs.json +9 -0
- package/{tsconfig.json → data/template/base/tsconfig.json} +2 -2
- package/data/template/base/tsconfig.mjs.json +10 -0
- package/dist/cjs/Command/GenTemplate.d.ts +3 -0
- package/dist/cjs/Command/GenTemplate.js +25 -0
- package/dist/{Command → cjs/Command}/Route.js +2 -4
- package/dist/{Command → cjs/Command}/RouteInterface.d.ts +4 -0
- package/dist/{Command → cjs/Command}/RouteInterface.js +6 -2
- package/dist/cjs/Generator/Common.d.ts +2 -0
- package/dist/cjs/Generator/Common.js +65 -0
- package/dist/cjs/Generator/base/index.d.ts +3 -0
- package/dist/cjs/Generator/base/index.js +4 -0
- package/dist/cjs/Generator/electron/index.d.ts +3 -0
- package/dist/cjs/Generator/electron/index.js +4 -0
- package/dist/cjs/test/testFileMacro.d.ts +1 -0
- package/dist/cjs/test/testFileMacro.js +1 -0
- package/dist/cjs/test/testGlobMacro.d.ts +1 -0
- package/dist/cjs/test/testGlobMacro.js +2 -0
- package/dist/cjs/test/testRegex.d.ts +1 -0
- package/dist/cjs/test/testRegex.js +3 -0
- package/dist/cjs/test/testRegex.macro.d.ts +1 -0
- package/dist/cjs/test/testRegex.macro.js +11 -0
- package/dist/cjs/test/testRegexa.d.ts +1 -0
- package/dist/cjs/test/testRegexa.js +1 -0
- package/dist/cjs/test/testRegexa.macro.d.ts +1 -0
- package/dist/cjs/test/testRegexa.macro.js +4 -0
- package/dist/mjs/Command/ExpandMacro.d.ts +3 -0
- package/dist/mjs/Command/ExpandMacro.js +16 -0
- package/dist/mjs/Command/GenI18n.d.ts +3 -0
- package/dist/mjs/Command/GenI18n.js +75 -0
- package/dist/mjs/Command/GenSchema.d.ts +3 -0
- package/dist/mjs/Command/GenSchema.js +22 -0
- package/dist/mjs/Command/GenTemplate.d.ts +3 -0
- package/dist/mjs/Command/GenTemplate.js +18 -0
- package/dist/mjs/Command/MapPath.d.ts +3 -0
- package/dist/mjs/Command/MapPath.js +64 -0
- package/dist/mjs/Command/Node.d.ts +3 -0
- package/dist/mjs/Command/Node.js +14 -0
- package/dist/mjs/Command/Release.d.ts +3 -0
- package/dist/mjs/Command/Release.js +88 -0
- package/dist/mjs/Command/Route.d.ts +1 -0
- package/dist/mjs/Command/Route.js +20 -0
- package/dist/mjs/Command/RouteInterface.d.ts +24 -0
- package/{src/Command/RouteInterface.ts → dist/mjs/Command/RouteInterface.js} +58 -63
- package/dist/mjs/Command/ScanDups.d.ts +3 -0
- package/dist/mjs/Command/ScanDups.js +43 -0
- package/dist/mjs/Command/index.d.ts +1 -0
- package/dist/mjs/Command/index.js +1 -0
- package/dist/mjs/Generator/Common.d.ts +2 -0
- package/dist/mjs/Generator/Common.js +58 -0
- package/dist/mjs/Generator/base/index.d.ts +3 -0
- package/dist/mjs/Generator/base/index.js +2 -0
- package/dist/mjs/Generator/electron/index.d.ts +3 -0
- package/dist/mjs/Generator/electron/index.js +2 -0
- package/dist/mjs/Macro.macro.d.ts +1 -0
- package/dist/mjs/Macro.macro.js +2 -0
- package/dist/mjs/QuickFunc.d.ts +2 -0
- package/dist/mjs/QuickFunc.js +2 -0
- package/dist/mjs/UtilAst.d.ts +13 -0
- package/dist/mjs/UtilAst.js +15 -0
- package/dist/mjs/UtilDevTool.d.ts +99 -0
- package/dist/mjs/UtilDevTool.js +296 -0
- package/dist/mjs/UtilInterface.d.ts +4 -0
- package/dist/mjs/UtilInterface.js +1 -0
- package/dist/mjs/UtilMacro.d.ts +7 -0
- package/dist/mjs/UtilMacro.js +23 -0
- package/dist/mjs/index.d.ts +5 -0
- package/dist/mjs/index.js +5 -0
- package/dist/mjs/test/testFileMacro.d.ts +1 -0
- package/dist/mjs/test/testFileMacro.js +1 -0
- package/dist/mjs/test/testGlobMacro.d.ts +1 -0
- package/dist/mjs/test/testGlobMacro.js +2 -0
- package/dist/mjs/test/testRegex.d.ts +1 -0
- package/dist/mjs/test/testRegex.js +2 -0
- package/dist/mjs/test/testRegex.macro.d.ts +1 -0
- package/dist/mjs/test/testRegex.macro.js +9 -0
- package/dist/mjs/test/testRegexa.d.ts +1 -0
- package/dist/mjs/test/testRegexa.js +1 -0
- package/dist/mjs/test/testRegexa.macro.d.ts +1 -0
- package/dist/mjs/test/testRegexa.macro.js +2 -0
- package/package.json +18 -5
- package/UnableTypes.txt +0 -1
- package/data/init/index.d.ts +0 -1
- package/data/init/index.js +0 -1
- package/data/init/scripts/compile.ps1 +0 -5
- package/data/init/tsconfig.compile.json +0 -4
- package/data/init/tsconfig.json +0 -22
- package/dist/Command/Init.d.ts +0 -3
- package/dist/Command/Init.js +0 -82
- package/dist/Command/InitElectron.d.ts +0 -3
- package/dist/Command/InitElectron.js +0 -64
- package/index.d.ts +0 -1
- package/index.js +0 -1
- package/jest.config.js +0 -16
- package/scripts/compile.ps1 +0 -5
- package/scripts/postinstall.js +0 -54
- package/scripts/watch.ps1 +0 -10
- package/src/Command/ExpandMacro.ts +0 -18
- package/src/Command/GenI18n.ts +0 -85
- package/src/Command/GenSchema.ts +0 -24
- package/src/Command/Init.ts +0 -65
- package/src/Command/InitElectron.ts +0 -42
- package/src/Command/MapPath.ts +0 -64
- package/src/Command/Node.ts +0 -16
- package/src/Command/Release.ts +0 -96
- package/src/Command/Route.ts +0 -24
- package/src/Command/ScanDups.ts +0 -47
- package/src/Command/index.ts +0 -1
- package/src/Macro.macro.ts +0 -4
- package/src/QuickFunc.ts +0 -7
- package/src/UtilAst.ts +0 -25
- package/src/UtilDevTool.ts +0 -345
- package/src/UtilInterface.ts +0 -5
- package/src/UtilMacro.ts +0 -23
- package/src/index.ts +0 -5
- package/test.js +0 -29
- package/tsconfig.compile.json +0 -4
- /package/{scripts → data/template/base/scripts}/release.ps1 +0 -0
- /package/data/{init → template/base}/scripts/watch.ps1 +0 -0
- /package/data/{init → template/base}/src/index.ts +0 -0
- /package/data/{InitElectron → template/electron}/.eslintrc.js +0 -0
- /package/data/{InitElectron → template/electron}/forge.config.ts +0 -0
- /package/data/{InitElectron → template/electron}/gitignore +0 -0
- /package/data/{InitElectron → template/electron}/package-lock.json +0 -0
- /package/data/{InitElectron → template/electron}/package.json +0 -0
- /package/data/{InitElectron → template/electron}/src/Backend/BridgeBackend.ts +0 -0
- /package/data/{InitElectron → template/electron}/src/Backend/index.ts +0 -0
- /package/data/{InitElectron → template/electron}/src/Frontend/Base.tsx +0 -0
- /package/data/{InitElectron → template/electron}/src/Frontend/ContextProxy.ts +0 -0
- /package/data/{InitElectron → template/electron}/src/Frontend/index.ts +0 -0
- /package/data/{InitElectron → template/electron}/src/app.tsx +0 -0
- /package/data/{InitElectron → template/electron}/src/index.html +0 -0
- /package/data/{InitElectron → template/electron}/src/index.ts +0 -0
- /package/data/{InitElectron → template/electron}/src/preload.ts +0 -0
- /package/data/{InitElectron → template/electron}/src/renderer.ts +0 -0
- /package/data/{InitElectron → template/electron}/tsconfig.json +0 -0
- /package/data/{InitElectron → template/electron}/webpack.main.config.ts +0 -0
- /package/data/{InitElectron → template/electron}/webpack.plugins.ts +0 -0
- /package/data/{InitElectron → template/electron}/webpack.renderer.config.ts +0 -0
- /package/data/{InitElectron → template/electron}/webpack.rules.ts +0 -0
- /package/dist/{Command → cjs/Command}/ExpandMacro.d.ts +0 -0
- /package/dist/{Command → cjs/Command}/ExpandMacro.js +0 -0
- /package/dist/{Command → cjs/Command}/GenI18n.d.ts +0 -0
- /package/dist/{Command → cjs/Command}/GenI18n.js +0 -0
- /package/dist/{Command → cjs/Command}/GenSchema.d.ts +0 -0
- /package/dist/{Command → cjs/Command}/GenSchema.js +0 -0
- /package/dist/{Command → cjs/Command}/MapPath.d.ts +0 -0
- /package/dist/{Command → cjs/Command}/MapPath.js +0 -0
- /package/dist/{Command → cjs/Command}/Node.d.ts +0 -0
- /package/dist/{Command → cjs/Command}/Node.js +0 -0
- /package/dist/{Command → cjs/Command}/Release.d.ts +0 -0
- /package/dist/{Command → cjs/Command}/Release.js +0 -0
- /package/dist/{Command → cjs/Command}/Route.d.ts +0 -0
- /package/dist/{Command → cjs/Command}/ScanDups.d.ts +0 -0
- /package/dist/{Command → cjs/Command}/ScanDups.js +0 -0
- /package/dist/{Command → cjs/Command}/index.d.ts +0 -0
- /package/dist/{Command → cjs/Command}/index.js +0 -0
- /package/dist/{Macro.macro.d.ts → cjs/Macro.macro.d.ts} +0 -0
- /package/dist/{Macro.macro.js → cjs/Macro.macro.js} +0 -0
- /package/dist/{QuickFunc.d.ts → cjs/QuickFunc.d.ts} +0 -0
- /package/dist/{QuickFunc.js → cjs/QuickFunc.js} +0 -0
- /package/dist/{UtilAst.d.ts → cjs/UtilAst.d.ts} +0 -0
- /package/dist/{UtilAst.js → cjs/UtilAst.js} +0 -0
- /package/dist/{UtilDevTool.d.ts → cjs/UtilDevTool.d.ts} +0 -0
- /package/dist/{UtilDevTool.js → cjs/UtilDevTool.js} +0 -0
- /package/dist/{UtilInterface.d.ts → cjs/UtilInterface.d.ts} +0 -0
- /package/dist/{UtilInterface.js → cjs/UtilInterface.js} +0 -0
- /package/dist/{UtilMacro.d.ts → cjs/UtilMacro.d.ts} +0 -0
- /package/dist/{UtilMacro.js → cjs/UtilMacro.js} +0 -0
- /package/dist/{index.d.ts → cjs/index.d.ts} +0 -0
- /package/dist/{index.js → cjs/index.js} +0 -0
@@ -0,0 +1,296 @@
|
|
1
|
+
import path from 'pathe';
|
2
|
+
import * as TJS from 'typescript-json-schema';
|
3
|
+
import * as fs from 'fs';
|
4
|
+
import { SLogger, UtilFT, UtilFunc, dedent, throwError } from '@zwa73/utils';
|
5
|
+
import { Project, SyntaxKind } from 'ts-morph';
|
6
|
+
export var UtilDT;
|
7
|
+
(function (UtilDT) {
|
8
|
+
/**生成匹配的文件的所有type的schema
|
9
|
+
* @async
|
10
|
+
* @param dir - 基础目录
|
11
|
+
* @param opt - 可选参数
|
12
|
+
* @param opt.include - 包含的glob 默认 **\/\*.schema.ts
|
13
|
+
* @param opt.exclude - 忽略的glob
|
14
|
+
* @param opt.coverDefine - 将会覆盖 definitions 对应内容的表
|
15
|
+
* @param opt.project - tsconfig路径
|
16
|
+
* @param opt.outDir - schema输出路径目录 默认 ./schema/
|
17
|
+
* @param opt.includeTypes - 包含的types正则匹配数组 默认 [/.*\/]
|
18
|
+
* @param opt.excludeTypes - 排除的types正则匹配数组
|
19
|
+
*/
|
20
|
+
async function generateSchema(dir, opt) {
|
21
|
+
//生成
|
22
|
+
const settings = {
|
23
|
+
required: true,
|
24
|
+
aliasRef: true,
|
25
|
+
tsNodeRegister: true,
|
26
|
+
ignoreErrors: true,
|
27
|
+
};
|
28
|
+
const compilerOptions = {
|
29
|
+
strictNullChecks: true,
|
30
|
+
};
|
31
|
+
if (opt?.project)
|
32
|
+
Object.assign(compilerOptions, (await UtilFT.loadJSONFile(opt.project)).compilerOptions);
|
33
|
+
const files = await UtilFT.fileSearchGlob(dir, opt?.include ?? "**/*.schema.ts", { ingore: opt?.exclude });
|
34
|
+
const program = TJS.getProgramFromFiles(files, compilerOptions);
|
35
|
+
const gener = TJS.buildGenerator(program, settings);
|
36
|
+
//const schema = TJS.generateSchema(program, opt?.typeName??"*", settings,undefined,gener) as JObject;
|
37
|
+
const it = opt?.includeTypes?.map(t => new RegExp(t)) ?? [/.*/];
|
38
|
+
const et = opt?.excludeTypes?.map(t => new RegExp(t)) ?? [];
|
39
|
+
const p = new Project();
|
40
|
+
const types = files.map(fp => p
|
41
|
+
.addSourceFileAtPath(fp)
|
42
|
+
.getDescendantsOfKind(SyntaxKind.TypeAliasDeclaration)
|
43
|
+
.map(d => d.getName())).flat();
|
44
|
+
const list = gener.getUserSymbols()
|
45
|
+
.filter(t => types.some(i => i === t))
|
46
|
+
.filter(t => it.some(i => i.test(t)))
|
47
|
+
.filter(t => !et.some(i => i.test(t)));
|
48
|
+
const schema = gener.getSchemaForSymbols(list);
|
49
|
+
const outDir = opt?.outDir ?? path.join(process.cwd(), 'schema');
|
50
|
+
const schemasPath = path.join(outDir, 'schemas.json');
|
51
|
+
//预处理
|
52
|
+
const proced = procSchema(schema, opt?.coverDefine ?? {});
|
53
|
+
await Promise.all([
|
54
|
+
UtilFT.writeJSONFile(schemasPath, proced),
|
55
|
+
//展开
|
56
|
+
expandSchema(proced, schemasPath),
|
57
|
+
]);
|
58
|
+
}
|
59
|
+
UtilDT.generateSchema = generateSchema;
|
60
|
+
function procSchema(schema, coverDefine) {
|
61
|
+
//覆盖
|
62
|
+
coverObj(schema["definitions"], coverDefine);
|
63
|
+
//替换SchemaString标识符
|
64
|
+
schema = JSON.parse(JSON.stringify(schema).replace(/\^\.\*SchemaString\$/g, '^.*$'));
|
65
|
+
return schema;
|
66
|
+
}
|
67
|
+
/**展开schema以供使用 */
|
68
|
+
async function expandSchema(schema, schemasPath) {
|
69
|
+
const plist = [];
|
70
|
+
const definitions = schema["definitions"];
|
71
|
+
//展开定义
|
72
|
+
for (const typeName in definitions) {
|
73
|
+
const schema = definitions[typeName];
|
74
|
+
//展开所有object与忽略检测的类型
|
75
|
+
if (!(schema.type == "object" || schema.type == "array" || schema.type == undefined))
|
76
|
+
continue;
|
77
|
+
if ((/^.+_[0-9]/).test(typeName) || (/^{./).test(typeName))
|
78
|
+
continue;
|
79
|
+
const basename = path.basename(schemasPath);
|
80
|
+
const tpath = path.join(path.dirname(schemasPath), `${typeName}.schema.json`);
|
81
|
+
if (!UtilFT.isValidFilePath(tpath))
|
82
|
+
continue;
|
83
|
+
plist.push(UtilFT.writeJSONFile(tpath, {
|
84
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
85
|
+
"$ref": `${basename}#/definitions/${typeName}`
|
86
|
+
}));
|
87
|
+
}
|
88
|
+
await Promise.all(plist);
|
89
|
+
}
|
90
|
+
/**覆盖object */
|
91
|
+
function coverObj(base, cover) {
|
92
|
+
for (const k in cover) {
|
93
|
+
const v = cover[k];
|
94
|
+
if (typeof v === "object" && !Array.isArray(v) &&
|
95
|
+
typeof base[k] === "object" && !Array.isArray(base[k]))
|
96
|
+
coverObj(base[k], v);
|
97
|
+
else
|
98
|
+
base[k] = v;
|
99
|
+
}
|
100
|
+
}
|
101
|
+
/**运行所有匹配的的js/ts文件
|
102
|
+
* @async
|
103
|
+
* @param dir - 基础目录
|
104
|
+
* @param opt - 可选参数
|
105
|
+
* @param opt.include - 包含的glob 默认 index.ts
|
106
|
+
* @param opt.exclude - 忽略的glob
|
107
|
+
* @param opt.project - tsconfig路径
|
108
|
+
*/
|
109
|
+
async function matchNode(dir, opt) {
|
110
|
+
const files = await UtilFT.fileSearchGlob(dir, opt?.include ?? "index.ts", { ingore: opt?.exclude });
|
111
|
+
await batchNode(files, opt);
|
112
|
+
}
|
113
|
+
UtilDT.matchNode = matchNode;
|
114
|
+
/**运行所有js/ts文件
|
115
|
+
* @async
|
116
|
+
* @param filepath - 需要运行的文件
|
117
|
+
* @param opt - 可选参数
|
118
|
+
* @param opt.project - tsconfig路径
|
119
|
+
*/
|
120
|
+
async function batchNode(filepath, opt) {
|
121
|
+
// 确保 filepath 总是一个数组
|
122
|
+
if (!Array.isArray(filepath))
|
123
|
+
filepath = [filepath];
|
124
|
+
// 将所有的相对路径转换为绝对路径
|
125
|
+
const absolutePaths = filepath.map(fp => path.resolve(process.cwd(), fp));
|
126
|
+
// 创建一个字符串,其中包含所有文件的 require 语句
|
127
|
+
const requires = absolutePaths.map(fp => `require('${fp}')`).join(';');
|
128
|
+
// 创建并执行 ts-node 命令
|
129
|
+
const cmd = `ts-node -r tsconfig-paths/register -e "${requires}" ${opt?.project ? `-P "${opt.project}"` : ""}`;
|
130
|
+
await UtilFunc.exec(cmd, { outlvl: 'info', errlvl: 'warn' });
|
131
|
+
}
|
132
|
+
UtilDT.batchNode = batchNode;
|
133
|
+
//#region macro工具
|
134
|
+
const parseMacroPaths = (opt) => {
|
135
|
+
const loc = UtilFunc.getFuncLoc(3);
|
136
|
+
if (!loc && !opt?.filePath)
|
137
|
+
throwError(`parseMacroPaths 未能找到函数位置`);
|
138
|
+
const basePath = loc?.filePath;
|
139
|
+
return opt?.filePath
|
140
|
+
? opt.glob
|
141
|
+
? UtilFT.fileSearchGlob(process.cwd(), opt.filePath)
|
142
|
+
: typeof opt?.filePath === "string"
|
143
|
+
? [opt?.filePath]
|
144
|
+
: opt?.filePath.map((filepath) => filepath)
|
145
|
+
: [basePath.replace(/(.+)\.macro\.(js|ts|cjs|mjs)$/, "$1.$2")];
|
146
|
+
};
|
147
|
+
const readFile = async (basePath) => (await fs.promises.readFile(basePath, 'utf-8')).replaceAll("\r\n", "\n");
|
148
|
+
const parseCodeText = async (codeText, opt) => {
|
149
|
+
const strText = typeof codeText === "function" ? await codeText(opt) : codeText;
|
150
|
+
return strText.split('\n').map((line) => `${opt.inent}${line}`).join('\n');
|
151
|
+
};
|
152
|
+
const literalRegex = (str) => new RegExp(`^${str.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')}(?!\\S)`);
|
153
|
+
//#endregion
|
154
|
+
/**将codeText写入对应region
|
155
|
+
* @param regionId - 区域id \`//#region ${id}\`
|
156
|
+
* @param codeText - 文本
|
157
|
+
* @param opt - 可选参数
|
158
|
+
* @param opt.filePath - 目标文件 默认为去除".macro"的同名文件
|
159
|
+
* @param opt.glob - 使用glob匹配而非文件路径
|
160
|
+
*/
|
161
|
+
async function regionMacro(regionId, codeText, opt) {
|
162
|
+
const plist = [];
|
163
|
+
const filePaths = await parseMacroPaths(opt);
|
164
|
+
for (const filePath of filePaths) {
|
165
|
+
const queuefunc = async () => {
|
166
|
+
if (!(await UtilFT.pathExists(filePath))) {
|
167
|
+
SLogger.error(`UtilDT.regionMacro ${filePath} 不存在`);
|
168
|
+
return;
|
169
|
+
}
|
170
|
+
let fileText = await readFile(filePath);
|
171
|
+
const regex = new RegExp(/(^|\n)([^\S\n]*)(\/\/#region (.*)\n)/.source +
|
172
|
+
/([\s\S]*?)/.source +
|
173
|
+
/([^\S\n]*\/\/#endregion(?!\S).*)/.source, "g");
|
174
|
+
regex.lastIndex = 0;
|
175
|
+
let match;
|
176
|
+
let hasMatch = false;
|
177
|
+
while (match = regex.exec(fileText)) {
|
178
|
+
const id = match[4];
|
179
|
+
const prefix = match[1];
|
180
|
+
const content = match[5];
|
181
|
+
const inent = match[2];
|
182
|
+
const comment = match[3];
|
183
|
+
const endcomment = match[6];
|
184
|
+
const idregex = typeof regionId === "string"
|
185
|
+
? literalRegex(regionId) : regionId;
|
186
|
+
let idmatch = idregex.exec(id);
|
187
|
+
if (idmatch == null)
|
188
|
+
continue;
|
189
|
+
hasMatch = true;
|
190
|
+
const ol = fileText.length;
|
191
|
+
const parseCode = await parseCodeText(codeText, {
|
192
|
+
matchId: idmatch[0],
|
193
|
+
execArr: idmatch,
|
194
|
+
text: dedent(content),
|
195
|
+
inent,
|
196
|
+
filePath
|
197
|
+
});
|
198
|
+
fileText = fileText.replace(match[0], `${prefix}${inent}${comment}${parseCode}\n${endcomment}`);
|
199
|
+
regex.lastIndex += fileText.length - ol;
|
200
|
+
}
|
201
|
+
if (hasMatch)
|
202
|
+
await fs.promises.writeFile(filePath, fileText, 'utf-8');
|
203
|
+
else if (!opt?.glob)
|
204
|
+
SLogger.error(`UtilDT.regionMacro 无法找到区域 ${regionId}`);
|
205
|
+
};
|
206
|
+
plist.push(UtilFunc.queueProc(path.normalize(filePath), queuefunc));
|
207
|
+
}
|
208
|
+
await Promise.all(plist);
|
209
|
+
}
|
210
|
+
UtilDT.regionMacro = regionMacro;
|
211
|
+
/**将codeText写入对应注释下
|
212
|
+
* @param commentId - 注释id \`// ${id}\`
|
213
|
+
* @param codeText - 文本
|
214
|
+
* @param opt - 可选参数
|
215
|
+
* @param opt.filePath - 目标文件 默认为去除".macro"的同名文件
|
216
|
+
* @param opt.glob - 使用glob匹配而非文件路径
|
217
|
+
*/
|
218
|
+
async function commentMacro(commentId, codeText, opt) {
|
219
|
+
const plist = [];
|
220
|
+
const filePaths = await parseMacroPaths(opt);
|
221
|
+
for (const filePath of filePaths) {
|
222
|
+
const queuefunc = async () => {
|
223
|
+
if (!(await UtilFT.pathExists(filePath))) {
|
224
|
+
SLogger.error(`UtilDT.commentMacro ${filePath} 不存在`);
|
225
|
+
return;
|
226
|
+
}
|
227
|
+
let fileText = await readFile(filePath);
|
228
|
+
const regex = new RegExp(/(^|\n)([^\S\n]*)(\/\/ (.*))(\n|)/.source +
|
229
|
+
/([^\n]*)/.source, "g");
|
230
|
+
let match;
|
231
|
+
let hasMatch = false;
|
232
|
+
while (match = regex.exec(fileText)) {
|
233
|
+
const id = match[4];
|
234
|
+
const prefix = match[1];
|
235
|
+
const content = match[6];
|
236
|
+
const inent = match[2];
|
237
|
+
const comment = match[3];
|
238
|
+
const idregex = typeof commentId === "string"
|
239
|
+
? literalRegex(commentId) : commentId;
|
240
|
+
let idmatch = idregex.exec(id);
|
241
|
+
if (idmatch == null)
|
242
|
+
continue;
|
243
|
+
hasMatch = true;
|
244
|
+
const ol = fileText.length;
|
245
|
+
const parseCode = await parseCodeText(codeText, {
|
246
|
+
matchId: idmatch[0],
|
247
|
+
execArr: idmatch,
|
248
|
+
text: dedent(content),
|
249
|
+
inent,
|
250
|
+
filePath
|
251
|
+
});
|
252
|
+
if (parseCode.includes('\n')) {
|
253
|
+
SLogger.error(`UtilDT.commentMacro 无法使用多行文本, 考虑使用regionMacro ${codeText}`);
|
254
|
+
return;
|
255
|
+
}
|
256
|
+
fileText = fileText.replace(match[0], `${prefix}${inent}${comment}\n${parseCode}`);
|
257
|
+
regex.lastIndex += fileText.length - ol;
|
258
|
+
}
|
259
|
+
if (hasMatch)
|
260
|
+
await fs.promises.writeFile(filePath, fileText, 'utf-8');
|
261
|
+
else if (!opt?.glob)
|
262
|
+
SLogger.error(`UtilDT.commentMacro 无法找到注释 ${commentId}`);
|
263
|
+
};
|
264
|
+
plist.push(UtilFunc.queueProc(path.normalize(filePath), queuefunc));
|
265
|
+
}
|
266
|
+
await Promise.all(plist);
|
267
|
+
}
|
268
|
+
UtilDT.commentMacro = commentMacro;
|
269
|
+
/**将codeText写入对应文件 不存在则创建
|
270
|
+
* @param codeText - 文本
|
271
|
+
* @param opt - 可选参数
|
272
|
+
* @param opt.filePath - 目标文件 默认为去除".macro"的同名文件
|
273
|
+
* @param opt.glob - 使用glob匹配而非文件路径
|
274
|
+
*/
|
275
|
+
async function fileMacro(codeText, opt) {
|
276
|
+
const plist = [];
|
277
|
+
const filePaths = await parseMacroPaths(opt);
|
278
|
+
for (const filePath of filePaths) {
|
279
|
+
const queuefunc = async () => {
|
280
|
+
await UtilFT.ensurePathExists(filePath);
|
281
|
+
const text = await readFile(filePath);
|
282
|
+
const parseCode = await parseCodeText(codeText, {
|
283
|
+
matchId: '',
|
284
|
+
execArr: /''/.exec(''),
|
285
|
+
text,
|
286
|
+
inent: '',
|
287
|
+
filePath
|
288
|
+
});
|
289
|
+
await fs.promises.writeFile(filePath, parseCode, 'utf-8');
|
290
|
+
};
|
291
|
+
plist.push(UtilFunc.queueProc(path.normalize(filePath), queuefunc));
|
292
|
+
}
|
293
|
+
await Promise.all(plist);
|
294
|
+
}
|
295
|
+
UtilDT.fileMacro = fileMacro;
|
296
|
+
})(UtilDT || (UtilDT = {}));
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -0,0 +1,23 @@
|
|
1
|
+
import { UtilFT } from "@zwa73/utils";
|
2
|
+
import { commentMacro } from "./QuickFunc.js";
|
3
|
+
import path from "pathe";
|
4
|
+
export var UtilMacro;
|
5
|
+
(function (UtilMacro) {
|
6
|
+
/**根据注释批量生成导出
|
7
|
+
* @param glob - 应用的golb
|
8
|
+
* @example
|
9
|
+
*/
|
10
|
+
function exportComment(glob) {
|
11
|
+
commentMacro(/export (\S*)/, async ({ filePath, matchId, execArr }) => {
|
12
|
+
const basedir = path.dirname(filePath);
|
13
|
+
const result = (await UtilFT.fileSearchGlob(basedir, execArr[1]))
|
14
|
+
.map((file) => path.relative(basedir, file))
|
15
|
+
.map((file) => path.parse(file).name)
|
16
|
+
.filter((file) => file != path.parse(filePath).name)
|
17
|
+
.map((file) => `export * from './${file}'`)
|
18
|
+
.join(';');
|
19
|
+
return result.length > 0 ? `${result};` : result;
|
20
|
+
}, { glob: true, filePath: glob });
|
21
|
+
}
|
22
|
+
UtilMacro.exportComment = exportComment;
|
23
|
+
})(UtilMacro || (UtilMacro = {}));
|
@@ -0,0 +1 @@
|
|
1
|
+
type FileMacro = 5;
|
@@ -0,0 +1 @@
|
|
1
|
+
"use strict";
|
@@ -0,0 +1 @@
|
|
1
|
+
type Test3 = 4;
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -0,0 +1,9 @@
|
|
1
|
+
import { commentMacro, fileMacro, regionMacro } from "../QuickFunc.js";
|
2
|
+
regionMacro('macroTest1', "type Test1 = 1");
|
3
|
+
regionMacro('macroTest2', "type Test2 = 2");
|
4
|
+
regionMacro('macroTest3', "type Test3 = 3");
|
5
|
+
commentMacro('comment1', "type CM1 = 3");
|
6
|
+
commentMacro('comment2', "type CM2 = 3");
|
7
|
+
regionMacro('macroTest4', "type Test3 = 4", { filePath: 'src/test/**/*.ts', glob: true });
|
8
|
+
fileMacro("type FileMacro = 5", { filePath: 'src/test/testFileMacro.ts' });
|
9
|
+
commentMacro(/regexTest[\S]*/, ({ matchId: id }) => `type ${id} = "${id}"`);
|
@@ -0,0 +1 @@
|
|
1
|
+
type CM1 = 3;
|
@@ -0,0 +1 @@
|
|
1
|
+
"use strict";
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
package/package.json
CHANGED
@@ -1,8 +1,14 @@
|
|
1
1
|
{
|
2
2
|
"name": "@zwa73/dev-utils",
|
3
|
-
"version": "1.0.
|
3
|
+
"version": "1.0.82",
|
4
4
|
"description": "编译与调试工具",
|
5
|
-
"
|
5
|
+
"type": "module",
|
6
|
+
"exports": {
|
7
|
+
".": {
|
8
|
+
"require": "./dist/cjs/index.js",
|
9
|
+
"import": "./dist/mjs/index.js"
|
10
|
+
}
|
11
|
+
},
|
6
12
|
"scripts": {
|
7
13
|
"test": "jest",
|
8
14
|
"release": "powershell scripts/release",
|
@@ -22,10 +28,11 @@
|
|
22
28
|
"ts-morph": "^23.0.0",
|
23
29
|
"ts-node": "^10.9.2",
|
24
30
|
"tsconfig-paths": "^4.2.0",
|
25
|
-
"typescript-json-schema": "^0.64.0"
|
31
|
+
"typescript-json-schema": "^0.64.0",
|
32
|
+
"yeoman-environment": "^4.4.3",
|
33
|
+
"yeoman-generator": "^7.4.0"
|
26
34
|
},
|
27
35
|
"devDependencies": {
|
28
|
-
"@types/fluent-ffmpeg": "^2.1.24",
|
29
36
|
"@types/jest": "^29.5.12",
|
30
37
|
"@types/node": "^20.14.11",
|
31
38
|
"jest": "^29.7.0",
|
@@ -35,5 +42,11 @@
|
|
35
42
|
},
|
36
43
|
"bin": {
|
37
44
|
"zcli": "bin/cli"
|
38
|
-
}
|
45
|
+
},
|
46
|
+
"files": [
|
47
|
+
"data",
|
48
|
+
"dist",
|
49
|
+
"index.js",
|
50
|
+
"index.d.ts"
|
51
|
+
]
|
39
52
|
}
|
package/UnableTypes.txt
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
PartialOption,ProperSubset,ProperSubsetCheck,Literal,LiteralCheck,AssignObject,FixedLengthTuple,UnionToIntersection,ExclusiveSub,ExclusiveRecursive,ExclusiveRecord,ExclusiveJObject,Await,FuncPropNames,ComposedClass,Mixinable,IsCtor,RefMixinable,ComposedMixinable,ComposedRefMixinable,ExtendThen,MatchableFlag,ExtractOutcome,AnnotableTuple,bigint,UnionInclude
|
package/data/init/index.d.ts
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
export * from './dist';
|
package/data/init/index.js
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
module.exports = require("./dist");
|
package/data/init/tsconfig.json
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
{
|
2
|
-
"compilerOptions": {
|
3
|
-
"allowJs": true,
|
4
|
-
"strict": true,
|
5
|
-
"target": "ES2022",
|
6
|
-
"module": "CommonJS",
|
7
|
-
"moduleResolution": "node",
|
8
|
-
"esModuleInterop": true,
|
9
|
-
"outDir": "./dist",
|
10
|
-
"declaration": true,
|
11
|
-
"baseUrl": ".",
|
12
|
-
"emitDecoratorMetadata": true,
|
13
|
-
"experimentalDecorators": true,
|
14
|
-
"paths": {
|
15
|
-
"@src/*": ["./src/*"],
|
16
|
-
"@/*": ["./*"],
|
17
|
-
"@": ["./src/index"]
|
18
|
-
}
|
19
|
-
},
|
20
|
-
"include": ["./src/**/*.ts", "./src/**/*.js", "./jest/**/*.ts"],
|
21
|
-
"exclude": ["./node_modules/**/*"]
|
22
|
-
}
|
package/dist/Command/Init.d.ts
DELETED
package/dist/Command/Init.js
DELETED
@@ -1,82 +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
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
26
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
27
|
-
};
|
28
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
29
|
-
exports.CmdInit = void 0;
|
30
|
-
const fs = __importStar(require("fs"));
|
31
|
-
const pathe_1 = __importDefault(require("pathe"));
|
32
|
-
const utils_1 = require("@zwa73/utils");
|
33
|
-
const RouteInterface_1 = require("./RouteInterface");
|
34
|
-
const InitDataPath = pathe_1.default.join(RouteInterface_1.DATA_PATH, 'init');
|
35
|
-
/**复制基础文件 */
|
36
|
-
async function copyData() {
|
37
|
-
const filelist = await fs.promises.readdir(InitDataPath);
|
38
|
-
const plist = filelist.map(async (fileName) => {
|
39
|
-
utils_1.SLogger.info(`正在复制 ${fileName}`);
|
40
|
-
const filePath = pathe_1.default.join(InitDataPath, fileName);
|
41
|
-
const targetPath = pathe_1.default.join(RouteInterface_1.PROJECT_PATH, fileName);
|
42
|
-
if (!await utils_1.UtilFT.pathExists(targetPath))
|
43
|
-
await fs.promises.cp(filePath, targetPath, { recursive: true });
|
44
|
-
else
|
45
|
-
utils_1.SLogger.info(`${fileName} 已存在 跳过`);
|
46
|
-
return null;
|
47
|
-
});
|
48
|
-
await Promise.all(plist);
|
49
|
-
}
|
50
|
-
/**安装npm包 */
|
51
|
-
async function installPackage() {
|
52
|
-
const install = async (name) => await utils_1.UtilFunc.exec(`npm i --registry ${RouteInterface_1.MIRROR_SOURCE} ${name}`);
|
53
|
-
const installdev = async (name) => await utils_1.UtilFunc.exec(`npm i --registry ${RouteInterface_1.MIRROR_SOURCE} --save-dev ${name}`);
|
54
|
-
const packageList = ['@zwa73/utils'];
|
55
|
-
const devPackList = [
|
56
|
-
'@zwa73/dev-utils',
|
57
|
-
'@types/fluent-ffmpeg',
|
58
|
-
'@types/node',
|
59
|
-
];
|
60
|
-
for (const name of packageList) {
|
61
|
-
utils_1.SLogger.info(`正在安装 ${name}`);
|
62
|
-
await install(name);
|
63
|
-
}
|
64
|
-
for (const name of devPackList) {
|
65
|
-
utils_1.SLogger.info(`正在安装 ${name}`);
|
66
|
-
await installdev(name);
|
67
|
-
}
|
68
|
-
}
|
69
|
-
/**对项目进行初始化 */
|
70
|
-
const CmdInit = (program) => program
|
71
|
-
.command('Init')
|
72
|
-
.alias('init')
|
73
|
-
.description('对当前目录进行项目初始化')
|
74
|
-
.action(async (opt) => {
|
75
|
-
(0, RouteInterface_1.checkProject)();
|
76
|
-
utils_1.SLogger.info(`开始初始化设置当前目录 ${RouteInterface_1.PROJECT_PATH}`);
|
77
|
-
await Promise.all([
|
78
|
-
copyData(),
|
79
|
-
installPackage(),
|
80
|
-
]);
|
81
|
-
});
|
82
|
-
exports.CmdInit = CmdInit;
|
@@ -1,64 +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
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
26
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
27
|
-
};
|
28
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
29
|
-
exports.CmdInitElectron = void 0;
|
30
|
-
const fs = __importStar(require("fs"));
|
31
|
-
const pathe_1 = __importDefault(require("pathe"));
|
32
|
-
const utils_1 = require("@zwa73/utils");
|
33
|
-
const RouteInterface_1 = require("./RouteInterface");
|
34
|
-
const InitDataPath = pathe_1.default.join(RouteInterface_1.DATA_PATH, 'InitElectron');
|
35
|
-
/**复制基础文件 */
|
36
|
-
async function copyData() {
|
37
|
-
const filelist = await fs.promises.readdir(InitDataPath);
|
38
|
-
const plist = filelist.map(async (fileName) => {
|
39
|
-
utils_1.SLogger.info(`正在复制 ${fileName}`);
|
40
|
-
const filePath = pathe_1.default.join(InitDataPath, fileName);
|
41
|
-
const targetPath = pathe_1.default.join(RouteInterface_1.PROJECT_PATH, fileName);
|
42
|
-
if (!await utils_1.UtilFT.pathExists(targetPath))
|
43
|
-
await fs.promises.cp(filePath, targetPath, { recursive: true });
|
44
|
-
else
|
45
|
-
utils_1.SLogger.info(`${fileName} 已存在 跳过`);
|
46
|
-
return null;
|
47
|
-
});
|
48
|
-
await Promise.all(plist);
|
49
|
-
await fs.promises.rename('./gitignore', './.gitignore');
|
50
|
-
}
|
51
|
-
/**对项目进行初始化 */
|
52
|
-
const CmdInitElectron = (program) => program
|
53
|
-
.command('Init-Electron')
|
54
|
-
.alias('initelectron')
|
55
|
-
.description('对当前目录进行项目初始化,创建Electron基础环境')
|
56
|
-
.action(async (opt) => {
|
57
|
-
//checkProject();
|
58
|
-
utils_1.SLogger.info(`开始初始化设置当前目录 ${RouteInterface_1.PROJECT_PATH}`);
|
59
|
-
await Promise.all([
|
60
|
-
copyData(),
|
61
|
-
utils_1.UtilFunc.exec(`npm ci`, { outlvl: 'info', errlvl: 'warn' })
|
62
|
-
]);
|
63
|
-
});
|
64
|
-
exports.CmdInitElectron = CmdInitElectron;
|
package/index.d.ts
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
export * from './dist';
|
package/index.js
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
module.exports = require("./dist");
|