@zwa73/dev-utils 1.0.71 → 1.0.81

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 (46) hide show
  1. package/data/InitElectron/src/Backend/BridgeBackend.ts +7 -0
  2. package/dist/Command/Release.js +28 -3
  3. package/dist/UtilInterface.d.ts +4 -0
  4. package/dist/UtilInterface.js +2 -0
  5. package/dist/index.d.ts +1 -0
  6. package/dist/index.js +1 -0
  7. package/dist/test/testFileMacro.d.ts +1 -0
  8. package/dist/test/testFileMacro.js +1 -0
  9. package/dist/test/testGlobMacro.d.ts +1 -0
  10. package/dist/test/testGlobMacro.js +2 -0
  11. package/dist/test/testRegex.d.ts +1 -0
  12. package/dist/test/testRegex.js +10 -0
  13. package/dist/test/testRegex.macro.d.ts +1 -0
  14. package/dist/test/testRegex.macro.js +12 -0
  15. package/dist/test/testRegexa.d.ts +1 -0
  16. package/dist/test/testRegexa.js +1 -0
  17. package/dist/test/testRegexa.macro.d.ts +1 -0
  18. package/dist/test/testRegexa.macro.js +5 -0
  19. package/package.json +8 -2
  20. package/UnableTypes.txt +0 -1
  21. package/jest.config.js +0 -16
  22. package/scripts/compile.ps1 +0 -5
  23. package/scripts/postinstall.js +0 -54
  24. package/scripts/release.ps1 +0 -4
  25. package/scripts/watch.ps1 +0 -10
  26. package/src/Command/ExpandMacro.ts +0 -18
  27. package/src/Command/GenI18n.ts +0 -85
  28. package/src/Command/GenSchema.ts +0 -24
  29. package/src/Command/Init.ts +0 -65
  30. package/src/Command/InitElectron.ts +0 -42
  31. package/src/Command/MapPath.ts +0 -64
  32. package/src/Command/Node.ts +0 -16
  33. package/src/Command/Release.ts +0 -65
  34. package/src/Command/Route.ts +0 -24
  35. package/src/Command/RouteInterface.ts +0 -63
  36. package/src/Command/ScanDups.ts +0 -47
  37. package/src/Command/index.ts +0 -1
  38. package/src/Macro.macro.ts +0 -4
  39. package/src/QuickFunc.ts +0 -7
  40. package/src/UtilAst.ts +0 -25
  41. package/src/UtilDevTool.ts +0 -345
  42. package/src/UtilMacro.ts +0 -23
  43. package/src/index.ts +0 -4
  44. package/test.js +0 -29
  45. package/tsconfig.compile.json +0 -4
  46. package/tsconfig.json +0 -22
@@ -1,65 +0,0 @@
1
- import { Command } from 'commander';
2
- import path from 'pathe';
3
-
4
- import { SLogger, throwError, UtilFT, UtilFunc } from '@zwa73/utils';
5
-
6
- import { checkProject, OFFICIAL_SOURCE, PROJECT_PATH } from './RouteInterface';
7
-
8
- /**解析版本号为number数组 */
9
- function parseVersion(version: string){
10
- const arr = version.split(".").map(Number);
11
- if (arr.length !== 3)
12
- throwError(`${version} 不是有效的版本号`,'error');
13
- for (let i = 0; i < 3; i++){
14
- if (!isFinite(arr[i]))
15
- throwError(`${version} 不是有效的版本号`,'error');
16
- }
17
- return arr;
18
- }
19
- /**判断版本号是否有效 */
20
- function checkVersion(oldVersion: string, newVersion: string) {
21
- const oldParts = parseVersion(oldVersion);
22
- const newParts = parseVersion(newVersion);
23
- for (let i = 0; i < 3; i++) {
24
- if (newParts[i] < oldParts[i])
25
- throwError(`${newVersion} 低于当前版本号 ${oldVersion}`,'error');
26
- if (newParts[i] > oldParts[i])
27
- break;
28
- }
29
- }
30
- /**更新版本号 */
31
- async function updateVersion(newVersion?: string): Promise<string> {
32
- const packagePath = path.join(PROJECT_PATH, "package.json");
33
- const packageData = await UtilFT.loadJSONFile(packagePath) as any;
34
- if (newVersion) {
35
- checkVersion(packageData.version, newVersion);
36
- packageData.version = newVersion;
37
- } else {
38
- const version = packageData.version.split(".");
39
- version[2] = (parseInt(version[2]) + 1).toString();
40
- packageData.version = version.join(".");
41
- }
42
- await UtilFT.writeJSONFile(packagePath, packageData);
43
- return packageData.version;
44
- }
45
- /**更新版本号并发布npm包 */
46
- export const CmdRelease = (program: Command) =>program
47
- .command("Release")
48
- .alias("release")
49
- .description("更新版本号并发布包")
50
- .option("-v, --version <version>", "指定发布的版本号 `${number}.${number}.${number}`")
51
- .option("-a, --access <access>", "npm publish 的 access 参数","public")
52
- .action(async (opt) => {
53
- checkProject();
54
- SLogger.info(`开始发布项目`);
55
- try {
56
- const newVersion = await updateVersion(opt.version);
57
- SLogger.info(`新版本号: ${newVersion}`);
58
- SLogger.info("正在发布...");
59
- const cmd = `npm publish --registry ${OFFICIAL_SOURCE} --access ${opt.access}`;
60
- await UtilFunc.exec(cmd,{errlvl:'info'});
61
- SLogger.info("发布完成");
62
- } catch (error) {
63
- SLogger.error(error);
64
- }
65
- });
@@ -1,24 +0,0 @@
1
- import { program } from 'commander';
2
-
3
- import { CmdInit } from './Init';
4
- import { CmdNode } from './Node';
5
- import { CmdRelease } from './Release';
6
- import { CmdMapPath } from './MapPath';
7
- import { CmdScanDups } from './ScanDups';
8
- import { CmdGenSchema } from './GenSchema';
9
- import { CmdExpandMacro } from './ExpandMacro';
10
- import { CmdGenI18n } from './GenI18n';
11
- import { CmdInitElectron } from './InitElectron';
12
-
13
- export async function cliRoute(){
14
- CmdInit(program);
15
- CmdInitElectron(program);
16
- CmdNode(program);
17
- CmdRelease(program);
18
- CmdMapPath(program);
19
- CmdScanDups(program);
20
- CmdGenSchema(program);
21
- CmdExpandMacro(program);
22
- CmdGenI18n(program);
23
- program.parse(process.argv);
24
- }
@@ -1,63 +0,0 @@
1
- import path from 'pathe';
2
-
3
- import { SLogger, throwError, UtilFT, UtilFunc } from '@zwa73/utils';
4
-
5
- /**npm镜像源 */
6
- export const MIRROR_SOURCE = "https://registry.npmmirror.com/";
7
- /**npm官方源 */
8
- export const OFFICIAL_SOURCE = "https://registry.npmjs.org/";
9
- /**data文件夹路径 */
10
- export const DATA_PATH = path.join(__dirname,'..','..','data');
11
- /**项目路径 */
12
- export const PROJECT_PATH = process.cwd();
13
-
14
- /** 检测非浮点的整数类型 */
15
- export function parseInteger(value: string): number {
16
- const num = Number(value);
17
- if(!(typeof num === 'number' && isFinite(num) && Math.floor(num) === num))
18
- throwError(`${value} 不是有效的整数数字`);
19
- return num;
20
- }
21
-
22
- /** 检测任意数字类型 */
23
- export function parseNumber(value: string):number {
24
- const num = Number(value);
25
- if(!(typeof value === 'number' && isFinite(value)))
26
- throwError(`${value} 不是有效的数字`);
27
- return num;
28
- }
29
-
30
- /** 检测布尔类型 */
31
- export function parseBoolean(value: string): boolean {
32
- return value? true:false;
33
- }
34
-
35
-
36
- /**在镜像源内镜像操作 */
37
- export async function withMirrorSource(func:(()=>any)){
38
- const out = await UtilFunc.exec('npm config get registry');
39
- const OriginSource = out.stdout.trim()//?
40
- SLogger.info(`正在修改npm源为 镜像源: ${MIRROR_SOURCE}`);
41
- await UtilFunc.exec(`npm config set registry ${MIRROR_SOURCE}`);
42
- await func();
43
- SLogger.info(`正在修改npm源为 原始源: ${OriginSource}`);
44
- await UtilFunc.exec(`npm config set registry ${OriginSource}`);
45
- }
46
-
47
- /**在官方源内镜像操作 */
48
- export async function withOfficialSource(func:(()=>any)){
49
- const out = await UtilFunc.exec('npm config get registry');
50
- const OriginSource = out.stdout.trim()//?
51
- SLogger.info(`正在修改npm源为 官方源: ${OFFICIAL_SOURCE}`);
52
- await UtilFunc.exec(`npm config set registry ${OFFICIAL_SOURCE}`);
53
- await func();
54
- SLogger.info(`正在修改npm源为 原始源: ${OriginSource}`);
55
- await UtilFunc.exec(`npm config set registry ${OriginSource}`);
56
- }
57
-
58
- /**检查目录是否为项目 */
59
- export async function checkProject() {
60
- const filePath = path.join(PROJECT_PATH,"package.json");
61
- if(!(await UtilFT.pathExists(filePath)))
62
- throwError(`当前目录: ${PROJECT_PATH} 不是npm项目目录, 请先使用 npm init`);
63
- }
@@ -1,47 +0,0 @@
1
- import { SLogger, UtilFT } from "@zwa73/utils";
2
- import { Command } from "commander";
3
- import fs from "fs";
4
- import crypto from 'crypto';
5
- import path from "pathe";
6
-
7
- async function calculateHash(filePath:string) {
8
- return new Promise<string>((resolve, reject) => {
9
- const hash = crypto.createHash('md5');
10
- const stream = fs.createReadStream(filePath);
11
- stream.on('data', (data) => hash.update(data));
12
- stream.on('end', () => resolve(hash.digest('hex')));
13
- stream.on('error', reject);
14
- });
15
- }
16
-
17
-
18
- /**重命名文件或路径 scan_duplicates */
19
- export const CmdScanDups = (program: Command) => program
20
- .command("Scan-Dups")
21
- .alias("scandups")
22
- .description("扫描当前目录下hash重复的文件")
23
- .option("-re, --regex <regex>", "文件的正则表达式, 使用posix路径",".*")
24
- .option("-o, --out <dir|console>", "输出的json文件路径, 默认 scandupsOut.json, 为 \"console\" 时无文件输出", "scandupsOut")
25
- .option("-r, --recursive", "是否处理子目录, 默认 true", true)
26
- .action(async (options) => {
27
- const regex = new RegExp(options.regex);
28
- const basePath = process.cwd();
29
- const pList = (await UtilFT.fileSearchRegex(basePath, regex.source, {
30
- relative: options.recursive,
31
- })).map(async (filePath) => ({ filePath, hash: await calculateHash(filePath) }));
32
-
33
- const hashMap = (await Promise.all(pList)).reduce((obj,cur)=>{
34
- obj[cur.hash] = (obj[cur.hash]??[]).concat(cur.filePath);
35
- return obj;
36
- },{} as Record<string,string[]>);
37
- const out:Record<string,string[]> = {};
38
- for (const hash in hashMap) {
39
- const duplicateFiles = hashMap[hash];
40
- if(duplicateFiles.length<=1) continue;
41
- out[hash] = duplicateFiles;
42
- }
43
- if(options.out==="console")
44
- SLogger.info(out);
45
- else
46
- await UtilFT.writeJSONFile(path.join(basePath,options.out),out);
47
- });
@@ -1 +0,0 @@
1
- export * from './Route';
@@ -1,4 +0,0 @@
1
- import { UtilMacro } from "./UtilMacro";
2
-
3
-
4
- //UtilMacro.exportComment('src/**/*.ts');
package/src/QuickFunc.ts DELETED
@@ -1,7 +0,0 @@
1
- import { UtilDT } from "./UtilDevTool";
2
-
3
- export const {
4
- regionMacro ,
5
- fileMacro ,
6
- commentMacro,
7
- } = UtilDT;
package/src/UtilAst.ts DELETED
@@ -1,25 +0,0 @@
1
- import { AnyFunc,I18nTextData } from "@zwa73/utils";
2
- import { SyntaxKind , KindToNodeMappings, Node} from "ts-morph";
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
- export namespace UtilAst{
11
- /**匹配Node的类型进行处理 无法匹配时返回undefined
12
- * @param node - ast节点
13
- * @param funcMap - 处理函数映射表
14
- */
15
- export function withKind<T extends Partial<{
16
- [P in SyntaxKind]:((n:KindToNodeMappings[P])=>unknown)
17
- }>>(node:Node,funcMap:T):{
18
- [P in keyof T]:T[P] extends AnyFunc ? ReturnType<T[P]> : never
19
- }[keyof T]|undefined{
20
- const kind = node.getKind();
21
- const func = funcMap[kind] as any;
22
- if(func) return func(node);
23
- return undefined;
24
- }
25
- }
@@ -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
- }
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,4 +0,0 @@
1
- export * from './UtilDevTool';
2
- export * from './Command';
3
- export * from "./QuickFunc";
4
- export * from './UtilMacro';
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
- }
package/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
- }