@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.
Files changed (176) hide show
  1. package/bin/cli +1 -1
  2. package/data/template/base/gitignore +3 -0
  3. package/data/template/base/package.json +25 -0
  4. package/data/template/base/scripts/compile.ps1 +8 -0
  5. package/data/template/base/src/macro.macro.ts +6 -0
  6. package/data/template/base/tsconfig.cjs.json +9 -0
  7. package/{tsconfig.json → data/template/base/tsconfig.json} +2 -2
  8. package/data/template/base/tsconfig.mjs.json +10 -0
  9. package/dist/cjs/Command/GenTemplate.d.ts +3 -0
  10. package/dist/cjs/Command/GenTemplate.js +25 -0
  11. package/dist/{Command → cjs/Command}/Route.js +2 -4
  12. package/dist/{Command → cjs/Command}/RouteInterface.d.ts +4 -0
  13. package/dist/{Command → cjs/Command}/RouteInterface.js +6 -2
  14. package/dist/cjs/Generator/Common.d.ts +2 -0
  15. package/dist/cjs/Generator/Common.js +65 -0
  16. package/dist/cjs/Generator/base/index.d.ts +3 -0
  17. package/dist/cjs/Generator/base/index.js +4 -0
  18. package/dist/cjs/Generator/electron/index.d.ts +3 -0
  19. package/dist/cjs/Generator/electron/index.js +4 -0
  20. package/dist/cjs/test/testFileMacro.d.ts +1 -0
  21. package/dist/cjs/test/testFileMacro.js +1 -0
  22. package/dist/cjs/test/testGlobMacro.d.ts +1 -0
  23. package/dist/cjs/test/testGlobMacro.js +2 -0
  24. package/dist/cjs/test/testRegex.d.ts +1 -0
  25. package/dist/cjs/test/testRegex.js +3 -0
  26. package/dist/cjs/test/testRegex.macro.d.ts +1 -0
  27. package/dist/cjs/test/testRegex.macro.js +11 -0
  28. package/dist/cjs/test/testRegexa.d.ts +1 -0
  29. package/dist/cjs/test/testRegexa.js +1 -0
  30. package/dist/cjs/test/testRegexa.macro.d.ts +1 -0
  31. package/dist/cjs/test/testRegexa.macro.js +4 -0
  32. package/dist/mjs/Command/ExpandMacro.d.ts +3 -0
  33. package/dist/mjs/Command/ExpandMacro.js +16 -0
  34. package/dist/mjs/Command/GenI18n.d.ts +3 -0
  35. package/dist/mjs/Command/GenI18n.js +75 -0
  36. package/dist/mjs/Command/GenSchema.d.ts +3 -0
  37. package/dist/mjs/Command/GenSchema.js +22 -0
  38. package/dist/mjs/Command/GenTemplate.d.ts +3 -0
  39. package/dist/mjs/Command/GenTemplate.js +18 -0
  40. package/dist/mjs/Command/MapPath.d.ts +3 -0
  41. package/dist/mjs/Command/MapPath.js +64 -0
  42. package/dist/mjs/Command/Node.d.ts +3 -0
  43. package/dist/mjs/Command/Node.js +14 -0
  44. package/dist/mjs/Command/Release.d.ts +3 -0
  45. package/dist/mjs/Command/Release.js +88 -0
  46. package/dist/mjs/Command/Route.d.ts +1 -0
  47. package/dist/mjs/Command/Route.js +20 -0
  48. package/dist/mjs/Command/RouteInterface.d.ts +24 -0
  49. package/{src/Command/RouteInterface.ts → dist/mjs/Command/RouteInterface.js} +58 -63
  50. package/dist/mjs/Command/ScanDups.d.ts +3 -0
  51. package/dist/mjs/Command/ScanDups.js +43 -0
  52. package/dist/mjs/Command/index.d.ts +1 -0
  53. package/dist/mjs/Command/index.js +1 -0
  54. package/dist/mjs/Generator/Common.d.ts +2 -0
  55. package/dist/mjs/Generator/Common.js +58 -0
  56. package/dist/mjs/Generator/base/index.d.ts +3 -0
  57. package/dist/mjs/Generator/base/index.js +2 -0
  58. package/dist/mjs/Generator/electron/index.d.ts +3 -0
  59. package/dist/mjs/Generator/electron/index.js +2 -0
  60. package/dist/mjs/Macro.macro.d.ts +1 -0
  61. package/dist/mjs/Macro.macro.js +2 -0
  62. package/dist/mjs/QuickFunc.d.ts +2 -0
  63. package/dist/mjs/QuickFunc.js +2 -0
  64. package/dist/mjs/UtilAst.d.ts +13 -0
  65. package/dist/mjs/UtilAst.js +15 -0
  66. package/dist/mjs/UtilDevTool.d.ts +99 -0
  67. package/dist/mjs/UtilDevTool.js +296 -0
  68. package/dist/mjs/UtilInterface.d.ts +4 -0
  69. package/dist/mjs/UtilInterface.js +1 -0
  70. package/dist/mjs/UtilMacro.d.ts +7 -0
  71. package/dist/mjs/UtilMacro.js +23 -0
  72. package/dist/mjs/index.d.ts +5 -0
  73. package/dist/mjs/index.js +5 -0
  74. package/dist/mjs/test/testFileMacro.d.ts +1 -0
  75. package/dist/mjs/test/testFileMacro.js +1 -0
  76. package/dist/mjs/test/testGlobMacro.d.ts +1 -0
  77. package/dist/mjs/test/testGlobMacro.js +2 -0
  78. package/dist/mjs/test/testRegex.d.ts +1 -0
  79. package/dist/mjs/test/testRegex.js +2 -0
  80. package/dist/mjs/test/testRegex.macro.d.ts +1 -0
  81. package/dist/mjs/test/testRegex.macro.js +9 -0
  82. package/dist/mjs/test/testRegexa.d.ts +1 -0
  83. package/dist/mjs/test/testRegexa.js +1 -0
  84. package/dist/mjs/test/testRegexa.macro.d.ts +1 -0
  85. package/dist/mjs/test/testRegexa.macro.js +2 -0
  86. package/package.json +18 -5
  87. package/UnableTypes.txt +0 -1
  88. package/data/init/index.d.ts +0 -1
  89. package/data/init/index.js +0 -1
  90. package/data/init/scripts/compile.ps1 +0 -5
  91. package/data/init/tsconfig.compile.json +0 -4
  92. package/data/init/tsconfig.json +0 -22
  93. package/dist/Command/Init.d.ts +0 -3
  94. package/dist/Command/Init.js +0 -82
  95. package/dist/Command/InitElectron.d.ts +0 -3
  96. package/dist/Command/InitElectron.js +0 -64
  97. package/index.d.ts +0 -1
  98. package/index.js +0 -1
  99. package/jest.config.js +0 -16
  100. package/scripts/compile.ps1 +0 -5
  101. package/scripts/postinstall.js +0 -54
  102. package/scripts/watch.ps1 +0 -10
  103. package/src/Command/ExpandMacro.ts +0 -18
  104. package/src/Command/GenI18n.ts +0 -85
  105. package/src/Command/GenSchema.ts +0 -24
  106. package/src/Command/Init.ts +0 -65
  107. package/src/Command/InitElectron.ts +0 -42
  108. package/src/Command/MapPath.ts +0 -64
  109. package/src/Command/Node.ts +0 -16
  110. package/src/Command/Release.ts +0 -96
  111. package/src/Command/Route.ts +0 -24
  112. package/src/Command/ScanDups.ts +0 -47
  113. package/src/Command/index.ts +0 -1
  114. package/src/Macro.macro.ts +0 -4
  115. package/src/QuickFunc.ts +0 -7
  116. package/src/UtilAst.ts +0 -25
  117. package/src/UtilDevTool.ts +0 -345
  118. package/src/UtilInterface.ts +0 -5
  119. package/src/UtilMacro.ts +0 -23
  120. package/src/index.ts +0 -5
  121. package/test.js +0 -29
  122. package/tsconfig.compile.json +0 -4
  123. /package/{scripts → data/template/base/scripts}/release.ps1 +0 -0
  124. /package/data/{init → template/base}/scripts/watch.ps1 +0 -0
  125. /package/data/{init → template/base}/src/index.ts +0 -0
  126. /package/data/{InitElectron → template/electron}/.eslintrc.js +0 -0
  127. /package/data/{InitElectron → template/electron}/forge.config.ts +0 -0
  128. /package/data/{InitElectron → template/electron}/gitignore +0 -0
  129. /package/data/{InitElectron → template/electron}/package-lock.json +0 -0
  130. /package/data/{InitElectron → template/electron}/package.json +0 -0
  131. /package/data/{InitElectron → template/electron}/src/Backend/BridgeBackend.ts +0 -0
  132. /package/data/{InitElectron → template/electron}/src/Backend/index.ts +0 -0
  133. /package/data/{InitElectron → template/electron}/src/Frontend/Base.tsx +0 -0
  134. /package/data/{InitElectron → template/electron}/src/Frontend/ContextProxy.ts +0 -0
  135. /package/data/{InitElectron → template/electron}/src/Frontend/index.ts +0 -0
  136. /package/data/{InitElectron → template/electron}/src/app.tsx +0 -0
  137. /package/data/{InitElectron → template/electron}/src/index.html +0 -0
  138. /package/data/{InitElectron → template/electron}/src/index.ts +0 -0
  139. /package/data/{InitElectron → template/electron}/src/preload.ts +0 -0
  140. /package/data/{InitElectron → template/electron}/src/renderer.ts +0 -0
  141. /package/data/{InitElectron → template/electron}/tsconfig.json +0 -0
  142. /package/data/{InitElectron → template/electron}/webpack.main.config.ts +0 -0
  143. /package/data/{InitElectron → template/electron}/webpack.plugins.ts +0 -0
  144. /package/data/{InitElectron → template/electron}/webpack.renderer.config.ts +0 -0
  145. /package/data/{InitElectron → template/electron}/webpack.rules.ts +0 -0
  146. /package/dist/{Command → cjs/Command}/ExpandMacro.d.ts +0 -0
  147. /package/dist/{Command → cjs/Command}/ExpandMacro.js +0 -0
  148. /package/dist/{Command → cjs/Command}/GenI18n.d.ts +0 -0
  149. /package/dist/{Command → cjs/Command}/GenI18n.js +0 -0
  150. /package/dist/{Command → cjs/Command}/GenSchema.d.ts +0 -0
  151. /package/dist/{Command → cjs/Command}/GenSchema.js +0 -0
  152. /package/dist/{Command → cjs/Command}/MapPath.d.ts +0 -0
  153. /package/dist/{Command → cjs/Command}/MapPath.js +0 -0
  154. /package/dist/{Command → cjs/Command}/Node.d.ts +0 -0
  155. /package/dist/{Command → cjs/Command}/Node.js +0 -0
  156. /package/dist/{Command → cjs/Command}/Release.d.ts +0 -0
  157. /package/dist/{Command → cjs/Command}/Release.js +0 -0
  158. /package/dist/{Command → cjs/Command}/Route.d.ts +0 -0
  159. /package/dist/{Command → cjs/Command}/ScanDups.d.ts +0 -0
  160. /package/dist/{Command → cjs/Command}/ScanDups.js +0 -0
  161. /package/dist/{Command → cjs/Command}/index.d.ts +0 -0
  162. /package/dist/{Command → cjs/Command}/index.js +0 -0
  163. /package/dist/{Macro.macro.d.ts → cjs/Macro.macro.d.ts} +0 -0
  164. /package/dist/{Macro.macro.js → cjs/Macro.macro.js} +0 -0
  165. /package/dist/{QuickFunc.d.ts → cjs/QuickFunc.d.ts} +0 -0
  166. /package/dist/{QuickFunc.js → cjs/QuickFunc.js} +0 -0
  167. /package/dist/{UtilAst.d.ts → cjs/UtilAst.d.ts} +0 -0
  168. /package/dist/{UtilAst.js → cjs/UtilAst.js} +0 -0
  169. /package/dist/{UtilDevTool.d.ts → cjs/UtilDevTool.d.ts} +0 -0
  170. /package/dist/{UtilDevTool.js → cjs/UtilDevTool.js} +0 -0
  171. /package/dist/{UtilInterface.d.ts → cjs/UtilInterface.d.ts} +0 -0
  172. /package/dist/{UtilInterface.js → cjs/UtilInterface.js} +0 -0
  173. /package/dist/{UtilMacro.d.ts → cjs/UtilMacro.d.ts} +0 -0
  174. /package/dist/{UtilMacro.js → cjs/UtilMacro.js} +0 -0
  175. /package/dist/{index.d.ts → cjs/index.d.ts} +0 -0
  176. /package/dist/{index.js → cjs/index.js} +0 -0
@@ -1,345 +0,0 @@
1
- import path from 'pathe';
2
- import * as TJS from 'typescript-json-schema';
3
- import * as fs from 'fs';
4
- import { JObject, SLogger, UtilFT, UtilFunc, dedent, throwError } from '@zwa73/utils';
5
- import { Project, SyntaxKind } from 'ts-morph';
6
-
7
- export namespace UtilDT{
8
-
9
- /**执行js/ts的可选项 */
10
- type BatchNodeOpt = Partial<{
11
- /**tsconfig路径 */
12
- project:string;
13
- }>
14
-
15
- /**build任意ts文件的可选项 */
16
- type BuildMatchOpt = Partial<{
17
- /**包含的glob */
18
- include:string|string[];
19
- /**忽略的glob */
20
- exclude:string|string[];
21
- }>&BatchNodeOpt;
22
-
23
- /**build schema的可选项 */
24
- type BuildSchemaOpt = Partial<{
25
- /**将会覆盖 definitions 对应内容的表 */
26
- coverDefine:JObject;
27
- /**schema输出路径目录 如 ./schema/ */
28
- outDir:string;
29
- /**包含的types正则匹配数组 默认 [/.*\/] */
30
- includeTypes?:string[];
31
- /**排除的types正则匹配数组 */
32
- excludeTypes?:string[];
33
- }>&BuildMatchOpt;
34
-
35
-
36
-
37
- /**生成匹配的文件的所有type的schema
38
- * @async
39
- * @param dir - 基础目录
40
- * @param opt - 可选参数
41
- * @param opt.include - 包含的glob 默认 **\/\*.schema.ts
42
- * @param opt.exclude - 忽略的glob
43
- * @param opt.coverDefine - 将会覆盖 definitions 对应内容的表
44
- * @param opt.project - tsconfig路径
45
- * @param opt.outDir - schema输出路径目录 默认 ./schema/
46
- * @param opt.includeTypes - 包含的types正则匹配数组 默认 [/.*\/]
47
- * @param opt.excludeTypes - 排除的types正则匹配数组
48
- */
49
- export async function generateSchema(dir:string,opt?:BuildSchemaOpt){
50
- //生成
51
- const settings: TJS.PartialArgs = {
52
- required: true,
53
- aliasRef:true,
54
- tsNodeRegister:true,
55
- ignoreErrors:true,
56
- };
57
- const compilerOptions: TJS.CompilerOptions = {
58
- strictNullChecks: true,
59
- };
60
- if(opt?.project) Object.assign(compilerOptions,(await UtilFT.loadJSONFile(opt.project) as any).compilerOptions);
61
- const files = await UtilFT.fileSearchGlob(dir,opt?.include ?? "**/*.schema.ts",{ingore:opt?.exclude});
62
-
63
- const program = TJS.getProgramFromFiles(
64
- files,
65
- compilerOptions,
66
- );
67
- const gener = TJS.buildGenerator(program,settings)!;
68
- //const schema = TJS.generateSchema(program, opt?.typeName??"*", settings,undefined,gener) as JObject;
69
- const it = opt?.includeTypes?.map(t=>new RegExp(t))??[/.*/];
70
- const et = opt?.excludeTypes?.map(t=>new RegExp(t))??[];
71
- const p = new Project();
72
- const types = files.map(fp => p
73
- .addSourceFileAtPath(fp)
74
- .getDescendantsOfKind(SyntaxKind.TypeAliasDeclaration)
75
- .map(d=>d.getName())
76
- ).flat();
77
- const list = gener.getUserSymbols()
78
- .filter(t=>types.some(i=>i===t))
79
- .filter(t=>it.some(i=>i.test(t)))
80
- .filter(t=>!et.some(i=>i.test(t)));
81
- const schema = gener.getSchemaForSymbols(list) as JObject;
82
- const outDir = opt?.outDir ?? path.join(process.cwd(),'schema');
83
- const schemasPath = path.join(outDir,'schemas.json');
84
- //预处理
85
- const proced = procSchema(schema,opt?.coverDefine??{});
86
- await Promise.all([
87
- UtilFT.writeJSONFile(schemasPath,proced),
88
- //展开
89
- expandSchema(proced,schemasPath),
90
- ]);
91
- }
92
- function procSchema(schema:JObject,coverDefine:JObject){
93
- //覆盖
94
- coverObj(schema["definitions"] as JObject,coverDefine);
95
- //替换SchemaString标识符
96
- schema = JSON.parse(JSON.stringify(schema).replace(/\^\.\*SchemaString\$/g,'^.*$'));
97
- return schema;
98
- }
99
- /**展开schema以供使用 */
100
- async function expandSchema(schema:JObject,schemasPath:string){
101
- const plist:Promise<void>[] = [];
102
- const definitions = schema["definitions"] as Record<string,JObject>;
103
- //展开定义
104
- for(const typeName in definitions){
105
- const schema = definitions[typeName];
106
- //展开所有object与忽略检测的类型
107
- if(!(schema.type == "object" || schema.type == "array" || schema.type == undefined)) continue;
108
- if((/^.+_[0-9]/).test(typeName) || (/^{./).test(typeName)) continue;
109
-
110
- const basename = path.basename(schemasPath);
111
- const tpath = path.join(path.dirname(schemasPath),`${typeName}.schema.json`);
112
- if(!UtilFT.isValidFilePath(tpath)) continue;
113
- plist.push(UtilFT.writeJSONFile(tpath,{
114
- "$schema": "http://json-schema.org/draft-07/schema#",
115
- "$ref": `${basename}#/definitions/${typeName}`
116
- }));
117
- }
118
- await Promise.all(plist);
119
- }
120
- /**覆盖object */
121
- function coverObj(base:JObject,cover:JObject){
122
- for(const k in cover){
123
- const v = cover[k];
124
- if( typeof v === "object" && !Array.isArray(v) &&
125
- typeof base[k] === "object" && !Array.isArray(base[k]) )
126
- coverObj(base[k] as JObject,v as JObject);
127
- else
128
- base[k] = v;
129
- }
130
- }
131
-
132
- /**运行所有匹配的的js/ts文件
133
- * @async
134
- * @param dir - 基础目录
135
- * @param opt - 可选参数
136
- * @param opt.include - 包含的glob 默认 index.ts
137
- * @param opt.exclude - 忽略的glob
138
- * @param opt.project - tsconfig路径
139
- */
140
- export async function matchNode(dir:string,opt?:BuildMatchOpt) {
141
- const files = await UtilFT.fileSearchGlob(dir,opt?.include ?? "index.ts",{ingore:opt?.exclude});
142
- await batchNode(files,opt);
143
- }
144
- /**运行所有js/ts文件
145
- * @async
146
- * @param filepath - 需要运行的文件
147
- * @param opt - 可选参数
148
- * @param opt.project - tsconfig路径
149
- */
150
- export async function batchNode(filepath:string|string[],opt?:BatchNodeOpt) {
151
- // 确保 filepath 总是一个数组
152
- if (!Array.isArray(filepath)) filepath = [filepath];
153
- // 将所有的相对路径转换为绝对路径
154
- const absolutePaths = filepath.map(fp => path.resolve(process.cwd(), fp));
155
- // 创建一个字符串,其中包含所有文件的 require 语句
156
- const requires = absolutePaths.map(fp => `require('${fp}')`).join(';');
157
- // 创建并执行 ts-node 命令
158
- const cmd = `ts-node -r tsconfig-paths/register -e "${requires}" ${opt?.project?`-P "${opt.project}"` : ""}`;
159
- await UtilFunc.exec(cmd, { outlvl: 'info', errlvl: 'warn' });
160
- }
161
-
162
- /**宏的可选参数 */
163
- type MacroOpt = Partial<{
164
- /**宏展开的目标文件 */
165
- filePath:string[]|string;
166
- /**使用glob匹配而非文件路径 */
167
- glob:boolean;
168
- }>
169
- /**codeText的参数 */
170
- type CodeTextOpt = {
171
- /**匹配的region/comment id */
172
- matchId:string;
173
- /**region/comment id正则的执行结果*/
174
- execArr:RegExpExecArray;
175
- /**展开宏的目标文件 */
176
- filePath:string;
177
- /**展开宏区域的原文本 */
178
- text:string;
179
- /**缩进 会自动应用 */
180
- inent:string;
181
- }
182
- //#region macro工具
183
- const parseMacroPaths = (opt?:MacroOpt)=>{
184
- const loc = UtilFunc.getFuncLoc(3);
185
- if(!loc && !opt?.filePath) throwError(`parseMacroPaths 未能找到函数位置`);
186
- const basePath = loc?.filePath!;
187
- return opt?.filePath
188
- ? opt.glob
189
- ? UtilFT.fileSearchGlob(process.cwd(),opt.filePath)
190
- : typeof opt?.filePath==="string"
191
- ? [opt?.filePath]
192
- : opt?.filePath.map((filepath)=>filepath)
193
- : [basePath.replace(/(.+)\.macro\.(js|ts|cjs|mjs)$/,"$1.$2")];
194
- }
195
- const readFile = async (basePath:string)=>
196
- (await fs.promises.readFile(basePath,'utf-8')).replaceAll("\r\n","\n");
197
- const parseCodeText = async (codeText:string|((opt:CodeTextOpt)=>string|Promise<string>),opt:CodeTextOpt)=>{
198
- const strText = typeof codeText === "function" ? await codeText(opt) : codeText;
199
- return strText.split('\n').map((line)=>`${opt.inent}${line}`).join('\n');
200
- }
201
- const literalRegex = (str:string)=>new RegExp(
202
- `^${str.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')}(?!\\S)`);
203
- //#endregion
204
- /**将codeText写入对应region
205
- * @param regionId - 区域id \`//#region ${id}\`
206
- * @param codeText - 文本
207
- * @param opt - 可选参数
208
- * @param opt.filePath - 目标文件 默认为去除".macro"的同名文件
209
- * @param opt.glob - 使用glob匹配而非文件路径
210
- */
211
- export async function regionMacro(regionId:string|RegExp,codeText:string|((opt:CodeTextOpt)=>string|Promise<string>),opt?:MacroOpt){
212
- const plist:Promise<void>[] = [];
213
- const filePaths = await parseMacroPaths(opt);
214
- for(const filePath of filePaths){
215
- const queuefunc = async ()=>{
216
- if(!(await UtilFT.pathExists(filePath))) {
217
- SLogger.error(`UtilDT.regionMacro ${filePath} 不存在`);
218
- return
219
- }
220
- let fileText = await readFile(filePath);
221
- const regex = new RegExp(
222
- /(^|\n)([^\S\n]*)(\/\/#region (.*)\n)/.source+
223
- /([\s\S]*?)/.source+
224
- /([^\S\n]*\/\/#endregion(?!\S).*)/.source
225
- ,"g")
226
- regex.lastIndex=0;
227
- let match:RegExpExecArray|null;
228
- let hasMatch = false;
229
- while(match = regex.exec(fileText)){
230
- const id = match[4];
231
- const prefix = match[1];
232
- const content = match[5];
233
- const inent = match[2];
234
- const comment = match[3];
235
- const endcomment = match[6];
236
-
237
- const idregex = typeof regionId === "string"
238
- ? literalRegex(regionId) : regionId;
239
- let idmatch = idregex.exec(id);
240
- if(idmatch==null) continue;
241
- hasMatch=true;
242
- const ol = fileText.length;
243
- const parseCode = await parseCodeText(codeText,{
244
- matchId :idmatch[0],
245
- execArr :idmatch,
246
- text :dedent(content),
247
- inent ,
248
- filePath
249
- });
250
- fileText = fileText.replace(match[0], `${prefix}${inent}${comment}${parseCode}\n${endcomment}`);
251
- regex.lastIndex += fileText.length - ol;
252
- }
253
- if(hasMatch)
254
- await fs.promises.writeFile(filePath, fileText, 'utf-8');
255
- else if(!opt?.glob) SLogger.error(`UtilDT.regionMacro 无法找到区域 ${regionId}`);
256
- }
257
- plist.push(UtilFunc.queueProc(path.normalize(filePath),queuefunc))
258
- }
259
- await Promise.all(plist);
260
- }
261
- /**将codeText写入对应注释下
262
- * @param commentId - 注释id \`// ${id}\`
263
- * @param codeText - 文本
264
- * @param opt - 可选参数
265
- * @param opt.filePath - 目标文件 默认为去除".macro"的同名文件
266
- * @param opt.glob - 使用glob匹配而非文件路径
267
- */
268
- export async function commentMacro(commentId:string|RegExp,codeText:string|((opt:CodeTextOpt)=>string|Promise<string>),opt?:MacroOpt){
269
- const plist:Promise<void>[] = [];
270
- const filePaths = await parseMacroPaths(opt);
271
- for(const filePath of filePaths){
272
- const queuefunc = async ()=>{
273
- if(!(await UtilFT.pathExists(filePath))) {
274
- SLogger.error(`UtilDT.commentMacro ${filePath} 不存在`);
275
- return
276
- }
277
- let fileText = await readFile(filePath);
278
- const regex = new RegExp(
279
- /(^|\n)([^\S\n]*)(\/\/ (.*))(\n|)/.source+
280
- /([^\n]*)/.source
281
- ,"g")
282
- let match:RegExpExecArray|null;
283
- let hasMatch = false;
284
- while(match = regex.exec(fileText)){
285
- const id = match[4];
286
- const prefix = match[1];
287
- const content = match[6];
288
- const inent = match[2];
289
- const comment = match[3];
290
-
291
- const idregex = typeof commentId === "string"
292
- ? literalRegex(commentId) : commentId;
293
- let idmatch = idregex.exec(id);
294
- if(idmatch==null) continue;
295
- hasMatch=true;
296
- const ol = fileText.length;
297
- const parseCode = await parseCodeText(codeText,{
298
- matchId :idmatch[0],
299
- execArr :idmatch,
300
- text :dedent(content),
301
- inent ,
302
- filePath
303
- });
304
- if(parseCode.includes('\n')){
305
- SLogger.error(`UtilDT.commentMacro 无法使用多行文本, 考虑使用regionMacro ${codeText}`);
306
- return;
307
- }
308
- fileText = fileText.replace(match[0], `${prefix}${inent}${comment}\n${parseCode}`);
309
- regex.lastIndex += fileText.length - ol;
310
- }
311
- if(hasMatch)
312
- await fs.promises.writeFile(filePath, fileText, 'utf-8');
313
- else if(!opt?.glob) SLogger.error(`UtilDT.commentMacro 无法找到注释 ${commentId}`);
314
- }
315
- plist.push(UtilFunc.queueProc(path.normalize(filePath),queuefunc))
316
- }
317
- await Promise.all(plist);
318
- }
319
- /**将codeText写入对应文件 不存在则创建
320
- * @param codeText - 文本
321
- * @param opt - 可选参数
322
- * @param opt.filePath - 目标文件 默认为去除".macro"的同名文件
323
- * @param opt.glob - 使用glob匹配而非文件路径
324
- */
325
- export async function fileMacro(codeText:string|((opt:Omit<CodeTextOpt,'ident'|'matchId'|'execArr'>)=>string|Promise<string>),opt?:MacroOpt){
326
- const plist:Promise<void>[] = [];
327
- const filePaths = await parseMacroPaths(opt);
328
- for(const filePath of filePaths){
329
- const queuefunc = async ()=>{
330
- await UtilFT.ensurePathExists(filePath);
331
- const text = await readFile(filePath);
332
- const parseCode = await parseCodeText(codeText,{
333
- matchId:'',
334
- execArr:/''/.exec('')!,
335
- text,
336
- inent:'',
337
- filePath
338
- });
339
- await fs.promises.writeFile(filePath, parseCode, 'utf-8');
340
- }
341
- plist.push(UtilFunc.queueProc(path.normalize(filePath),queuefunc))
342
- }
343
- await Promise.all(plist);
344
- }
345
- }
@@ -1,5 +0,0 @@
1
-
2
- /**用于辅助解析只能补全的类型
3
- * 输出后替换为 ^.*$ 的 string 匹配
4
- */
5
- export type SchemaString = `${string}SchemaString`;
package/src/UtilMacro.ts DELETED
@@ -1,23 +0,0 @@
1
- import { UtilFT } from "@zwa73/utils";
2
- import { commentMacro } from "./QuickFunc"
3
- import path from "pathe";
4
-
5
-
6
- export namespace UtilMacro{
7
- /**根据注释批量生成导出
8
- * @param glob - 应用的golb
9
- * @example
10
- */
11
- export function exportComment(glob:string){
12
- commentMacro(/export (\S*)/,async ({filePath,matchId,execArr})=>{
13
- const basedir = path.dirname(filePath);
14
- const result = (await UtilFT.fileSearchGlob(basedir,execArr[1]))
15
- .map((file)=>path.relative(basedir,file))
16
- .map((file)=>path.parse(file).name)
17
- .filter((file)=>file!=path.parse(filePath).name)
18
- .map((file)=>`export * from './${file}'`)
19
- .join(';');
20
- return result.length>0? `${result};` : result;
21
- },{glob:true,filePath:glob});
22
- }
23
- }
package/src/index.ts DELETED
@@ -1,5 +0,0 @@
1
- export * from './UtilDevTool';
2
- export * from './Command';
3
- export * from "./QuickFunc";
4
- export * from './UtilMacro';
5
- export * from './UtilInterface';
package/test.js DELETED
@@ -1,29 +0,0 @@
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
- const {UtilFT} = require('@zwa73/utils')
10
- const fs = require('fs');
11
- const path = require('path');
12
- async function main(){
13
- const root = __filename;
14
- const dirpath = path.join(__dirname,'src');
15
-
16
- const ts = await fs.promises.readdir(dirpath,{withFileTypes:true})
17
- ts//?
18
- const list = await UtilFT.fileSearchRegex(dirpath, '')
19
- .map((filePath)=>path.relative(dirpath, filePath));
20
-
21
- console.log(
22
- path.parse(root),
23
- path.basename(root)
24
- );
25
- list.forEach((file)=>{
26
- console.log(file)
27
- })
28
- }
29
- main();
@@ -1,4 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "include": ["./src/**/*.ts", "./src/**/*.js"]
4
- }
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
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