@zwa73/dev-utils 1.0.87 → 1.0.89

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.
Files changed (108) hide show
  1. package/bin/cli +2 -2
  2. package/data/template/base/package.json +6 -6
  3. package/data/template/base/scripts/compile.ps1 +23 -6
  4. package/data/template/base/tsconfig.cjs.json +2 -1
  5. package/data/template/base/tsconfig.json +3 -5
  6. package/data/template/base/tsconfig.mjs.json +2 -2
  7. package/data/template/base/tsconfig.types.json +8 -0
  8. package/data/template/cjs/gitignore +3 -0
  9. package/data/template/cjs/package.json +19 -0
  10. package/data/template/cjs/scripts/compile.ps1 +6 -0
  11. package/data/template/cjs/scripts/expand-macro.ps1 +2 -0
  12. package/data/template/cjs/scripts/release.ps1 +4 -0
  13. package/data/template/cjs/scripts/watch.ps1 +10 -0
  14. package/data/template/cjs/src/index.ts +0 -0
  15. package/data/template/cjs/src/macro.macro.ts +6 -0
  16. package/data/template/cjs/tsconfig.compile.json +12 -0
  17. package/data/template/cjs/tsconfig.json +21 -0
  18. package/data/template/electron/package.json +25 -25
  19. package/dist/{cjs/Command → Command}/GenI18n.js +14 -10
  20. package/dist/{cjs/Command → Command}/GenTemplate.js +7 -4
  21. package/dist/{cjs/Command → Command}/MapPath.js +34 -25
  22. package/dist/{cjs/Command → Command}/Release.js +3 -1
  23. package/dist/Command/ScanDups.js +122 -0
  24. package/dist/DynImport.d.ts +15 -0
  25. package/dist/DynImport.js +15 -0
  26. package/dist/{mjs/UtilAst.d.ts → UtilAst.d.ts} +1 -1
  27. package/dist/{cjs/UtilDevTool.js → UtilDevTool.js} +13 -34
  28. package/dist/UtilInterface.d.ts +8 -0
  29. package/dist/UtilMacro.d.ts +7 -0
  30. package/dist/{cjs/UtilMacro.js → UtilMacro.js} +2 -2
  31. package/package.json +2 -8
  32. package/dist/cjs/Command/ScanDups.js +0 -101
  33. package/dist/cjs/UtilAst.d.ts +0 -13
  34. package/dist/cjs/UtilInterface.d.ts +0 -4
  35. package/dist/cjs/UtilMacro.d.ts +0 -7
  36. package/dist/cjs/test/testFileMacro.d.ts +0 -1
  37. package/dist/cjs/test/testFileMacro.js +0 -1
  38. package/dist/cjs/test/testGlobMacro.d.ts +0 -1
  39. package/dist/cjs/test/testGlobMacro.js +0 -2
  40. package/dist/cjs/test/testRegex.d.ts +0 -1
  41. package/dist/cjs/test/testRegex.js +0 -3
  42. package/dist/cjs/test/testRegexa.d.ts +0 -1
  43. package/dist/cjs/test/testRegexa.js +0 -1
  44. package/dist/mjs/Command/ExpandMacro.d.ts +0 -3
  45. package/dist/mjs/Command/ExpandMacro.js +0 -16
  46. package/dist/mjs/Command/GenI18n.d.ts +0 -3
  47. package/dist/mjs/Command/GenI18n.js +0 -75
  48. package/dist/mjs/Command/GenSchema.d.ts +0 -3
  49. package/dist/mjs/Command/GenSchema.js +0 -22
  50. package/dist/mjs/Command/GenTemplate.d.ts +0 -3
  51. package/dist/mjs/Command/GenTemplate.js +0 -61
  52. package/dist/mjs/Command/MapPath.d.ts +0 -3
  53. package/dist/mjs/Command/MapPath.js +0 -64
  54. package/dist/mjs/Command/Node.d.ts +0 -3
  55. package/dist/mjs/Command/Node.js +0 -14
  56. package/dist/mjs/Command/Release.d.ts +0 -3
  57. package/dist/mjs/Command/Release.js +0 -88
  58. package/dist/mjs/Command/Route.d.ts +0 -1
  59. package/dist/mjs/Command/Route.js +0 -20
  60. package/dist/mjs/Command/RouteInterface.d.ts +0 -24
  61. package/dist/mjs/Command/RouteInterface.js +0 -58
  62. package/dist/mjs/Command/ScanDups.d.ts +0 -3
  63. package/dist/mjs/Command/ScanDups.js +0 -94
  64. package/dist/mjs/Command/index.d.ts +0 -1
  65. package/dist/mjs/Command/index.js +0 -1
  66. package/dist/mjs/QuickFunc.d.ts +0 -2
  67. package/dist/mjs/QuickFunc.js +0 -2
  68. package/dist/mjs/UtilAst.js +0 -15
  69. package/dist/mjs/UtilDevTool.d.ts +0 -99
  70. package/dist/mjs/UtilDevTool.js +0 -297
  71. package/dist/mjs/UtilInterface.d.ts +0 -4
  72. package/dist/mjs/UtilInterface.js +0 -1
  73. package/dist/mjs/UtilMacro.d.ts +0 -7
  74. package/dist/mjs/UtilMacro.js +0 -23
  75. package/dist/mjs/index.d.ts +0 -5
  76. package/dist/mjs/index.js +0 -5
  77. package/dist/mjs/test/testFileMacro.d.ts +0 -1
  78. package/dist/mjs/test/testFileMacro.js +0 -1
  79. package/dist/mjs/test/testGlobMacro.d.ts +0 -1
  80. package/dist/mjs/test/testGlobMacro.js +0 -2
  81. package/dist/mjs/test/testRegex.d.ts +0 -1
  82. package/dist/mjs/test/testRegex.js +0 -2
  83. package/dist/mjs/test/testRegexa.d.ts +0 -1
  84. package/dist/mjs/test/testRegexa.js +0 -1
  85. /package/dist/{cjs/Command → Command}/ExpandMacro.d.ts +0 -0
  86. /package/dist/{cjs/Command → Command}/ExpandMacro.js +0 -0
  87. /package/dist/{cjs/Command → Command}/GenI18n.d.ts +0 -0
  88. /package/dist/{cjs/Command → Command}/GenSchema.d.ts +0 -0
  89. /package/dist/{cjs/Command → Command}/GenSchema.js +0 -0
  90. /package/dist/{cjs/Command → Command}/GenTemplate.d.ts +0 -0
  91. /package/dist/{cjs/Command → Command}/MapPath.d.ts +0 -0
  92. /package/dist/{cjs/Command → Command}/Node.d.ts +0 -0
  93. /package/dist/{cjs/Command → Command}/Node.js +0 -0
  94. /package/dist/{cjs/Command → Command}/Release.d.ts +0 -0
  95. /package/dist/{cjs/Command → Command}/Route.d.ts +0 -0
  96. /package/dist/{cjs/Command → Command}/Route.js +0 -0
  97. /package/dist/{cjs/Command → Command}/RouteInterface.d.ts +0 -0
  98. /package/dist/{cjs/Command → Command}/RouteInterface.js +0 -0
  99. /package/dist/{cjs/Command → Command}/ScanDups.d.ts +0 -0
  100. /package/dist/{cjs/Command → Command}/index.d.ts +0 -0
  101. /package/dist/{cjs/Command → Command}/index.js +0 -0
  102. /package/dist/{cjs/QuickFunc.d.ts → QuickFunc.d.ts} +0 -0
  103. /package/dist/{cjs/QuickFunc.js → QuickFunc.js} +0 -0
  104. /package/dist/{cjs/UtilAst.js → UtilAst.js} +0 -0
  105. /package/dist/{cjs/UtilDevTool.d.ts → UtilDevTool.d.ts} +0 -0
  106. /package/dist/{cjs/UtilInterface.js → UtilInterface.js} +0 -0
  107. /package/dist/{cjs/index.d.ts → index.d.ts} +0 -0
  108. /package/dist/{cjs/index.js → index.js} +0 -0
@@ -1,297 +0,0 @@
1
- import path from 'pathe';
2
- import * as TJS from 'typescript-json-schema';
3
- import * as fs from 'fs';
4
- import { PromiseQueue, 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
- // JsFunc -> ComposeFunc -> xxxmacro -> parseMacroPaths -> sourceTS
136
- const loc = UtilFunc.getFuncLoc(4);
137
- if (!loc && !opt?.filePath)
138
- throwError(`parseMacroPaths 未能找到函数位置`);
139
- const basePath = loc?.filePath;
140
- return opt?.filePath
141
- ? opt.glob
142
- ? UtilFT.fileSearchGlob(process.cwd(), opt.filePath)
143
- : typeof opt?.filePath === "string"
144
- ? [opt?.filePath]
145
- : opt?.filePath.map((filepath) => filepath)
146
- : [basePath.replace(/(.+)\.macro\.(js|ts|cjs|mjs)$/, "$1.$2")];
147
- };
148
- const readFile = async (basePath) => (await fs.promises.readFile(basePath, 'utf-8')).replaceAll("\r\n", "\n");
149
- const parseCodeText = async (codeText, opt) => {
150
- const strText = typeof codeText === "function" ? await codeText(opt) : codeText;
151
- return strText.split('\n').map((line) => `${opt.inent}${line}`).join('\n');
152
- };
153
- const literalRegex = (str) => new RegExp(`^${str.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')}(?!\\S)`);
154
- //#endregion
155
- /**将codeText写入对应region
156
- * @param regionId - 区域id \`//#region ${id}\`
157
- * @param codeText - 文本
158
- * @param opt - 可选参数
159
- * @param opt.filePath - 目标文件 默认为去除".macro"的同名文件
160
- * @param opt.glob - 使用glob匹配而非文件路径
161
- */
162
- async function regionMacro(regionId, codeText, opt) {
163
- const plist = [];
164
- const filePaths = await parseMacroPaths(opt);
165
- for (const filePath of filePaths) {
166
- const queuefunc = async () => {
167
- if (!(await UtilFT.pathExists(filePath))) {
168
- SLogger.error(`UtilDT.regionMacro ${filePath} 不存在`);
169
- return;
170
- }
171
- let fileText = await readFile(filePath);
172
- const regex = new RegExp(/(^|\n)([^\S\n]*)(\/\/#region (.*)\n)/.source +
173
- /([\s\S]*?)/.source +
174
- /([^\S\n]*\/\/#endregion(?!\S).*)/.source, "g");
175
- regex.lastIndex = 0;
176
- let match;
177
- let hasMatch = false;
178
- while (match = regex.exec(fileText)) {
179
- const id = match[4];
180
- const prefix = match[1];
181
- const content = match[5];
182
- const inent = match[2];
183
- const comment = match[3];
184
- const endcomment = match[6];
185
- const idregex = typeof regionId === "string"
186
- ? literalRegex(regionId) : regionId;
187
- let idmatch = idregex.exec(id);
188
- if (idmatch == null)
189
- continue;
190
- hasMatch = true;
191
- const ol = fileText.length;
192
- const parseCode = await parseCodeText(codeText, {
193
- matchId: idmatch[0],
194
- execArr: idmatch,
195
- text: dedent(content),
196
- inent,
197
- filePath
198
- });
199
- fileText = fileText.replace(match[0], `${prefix}${inent}${comment}${parseCode}\n${endcomment}`);
200
- regex.lastIndex += fileText.length - ol;
201
- }
202
- if (hasMatch)
203
- await fs.promises.writeFile(filePath, fileText, 'utf-8');
204
- else if (!opt?.glob)
205
- SLogger.error(`UtilDT.regionMacro 无法找到区域 ${regionId}`);
206
- };
207
- plist.push(PromiseQueue.enqueue(path.normalize(filePath), queuefunc));
208
- }
209
- await Promise.all(plist);
210
- }
211
- UtilDT.regionMacro = regionMacro;
212
- /**将codeText写入对应注释下
213
- * @param commentId - 注释id \`// ${id}\`
214
- * @param codeText - 文本
215
- * @param opt - 可选参数
216
- * @param opt.filePath - 目标文件 默认为去除".macro"的同名文件
217
- * @param opt.glob - 使用glob匹配而非文件路径
218
- */
219
- async function commentMacro(commentId, codeText, opt) {
220
- const plist = [];
221
- const filePaths = await parseMacroPaths(opt);
222
- for (const filePath of filePaths) {
223
- const queuefunc = async () => {
224
- if (!(await UtilFT.pathExists(filePath))) {
225
- SLogger.error(`UtilDT.commentMacro ${filePath} 不存在`);
226
- return;
227
- }
228
- let fileText = await readFile(filePath);
229
- const regex = new RegExp(/(^|\n)([^\S\n]*)(\/\/ (.*))(\n|)/.source +
230
- /([^\n]*)/.source, "g");
231
- let match;
232
- let hasMatch = false;
233
- while (match = regex.exec(fileText)) {
234
- const id = match[4];
235
- const prefix = match[1];
236
- const content = match[6];
237
- const inent = match[2];
238
- const comment = match[3];
239
- const idregex = typeof commentId === "string"
240
- ? literalRegex(commentId) : commentId;
241
- let idmatch = idregex.exec(id);
242
- if (idmatch == null)
243
- continue;
244
- hasMatch = true;
245
- const ol = fileText.length;
246
- const parseCode = await parseCodeText(codeText, {
247
- matchId: idmatch[0],
248
- execArr: idmatch,
249
- text: dedent(content),
250
- inent,
251
- filePath
252
- });
253
- if (parseCode.includes('\n')) {
254
- SLogger.error(`UtilDT.commentMacro 无法使用多行文本, 考虑使用regionMacro ${codeText}`);
255
- return;
256
- }
257
- fileText = fileText.replace(match[0], `${prefix}${inent}${comment}\n${parseCode}`);
258
- regex.lastIndex += fileText.length - ol;
259
- }
260
- if (hasMatch)
261
- await fs.promises.writeFile(filePath, fileText, 'utf-8');
262
- else if (!opt?.glob)
263
- SLogger.error(`UtilDT.commentMacro 无法找到注释 ${commentId}`);
264
- };
265
- plist.push(PromiseQueue.enqueue(path.normalize(filePath), queuefunc));
266
- }
267
- await Promise.all(plist);
268
- }
269
- UtilDT.commentMacro = commentMacro;
270
- /**将codeText写入对应文件 不存在则创建
271
- * @param codeText - 文本
272
- * @param opt - 可选参数
273
- * @param opt.filePath - 目标文件 默认为去除".macro"的同名文件
274
- * @param opt.glob - 使用glob匹配而非文件路径
275
- */
276
- async function fileMacro(codeText, opt) {
277
- const plist = [];
278
- const filePaths = await parseMacroPaths(opt);
279
- for (const filePath of filePaths) {
280
- const queuefunc = async () => {
281
- await UtilFT.ensurePathExists(filePath);
282
- const text = await readFile(filePath);
283
- const parseCode = await parseCodeText(codeText, {
284
- matchId: '',
285
- execArr: /''/.exec(''),
286
- text,
287
- inent: '',
288
- filePath
289
- });
290
- await fs.promises.writeFile(filePath, parseCode, 'utf-8');
291
- };
292
- plist.push(PromiseQueue.enqueue(path.normalize(filePath), queuefunc));
293
- }
294
- await Promise.all(plist);
295
- }
296
- UtilDT.fileMacro = fileMacro;
297
- })(UtilDT || (UtilDT = {}));
@@ -1,4 +0,0 @@
1
- /**用于辅助解析只能补全的类型
2
- * 输出后替换为 ^.*$ 的 string 匹配
3
- */
4
- export type SchemaString = `${string}SchemaString`;
@@ -1 +0,0 @@
1
- export {};
@@ -1,7 +0,0 @@
1
- export declare namespace UtilMacro {
2
- /**根据注释批量生成导出
3
- * @param glob - 应用的golb
4
- * @example
5
- */
6
- function exportComment(glob: string): void;
7
- }
@@ -1,23 +0,0 @@
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 = {}));
@@ -1,5 +0,0 @@
1
- export * from './UtilDevTool.js';
2
- export * from './Command/index.js';
3
- export * from "./QuickFunc.js";
4
- export * from './UtilMacro.js';
5
- export * from './UtilInterface.js';
package/dist/mjs/index.js DELETED
@@ -1,5 +0,0 @@
1
- export * from './UtilDevTool.js';
2
- export * from './Command/index.js';
3
- export * from "./QuickFunc.js";
4
- export * from './UtilMacro.js';
5
- export * from './UtilInterface.js';
@@ -1 +0,0 @@
1
- type FileMacro = 5;
@@ -1 +0,0 @@
1
- "use strict";
@@ -1 +0,0 @@
1
- type Test3 = 4;
@@ -1,2 +0,0 @@
1
- "use strict";
2
- //#endregion 1 ssa
@@ -1 +0,0 @@
1
- export {};
@@ -1,2 +0,0 @@
1
- 2;
2
- export {};
@@ -1 +0,0 @@
1
- type CM1 = 3;
@@ -1 +0,0 @@
1
- "use strict";
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes