@zwa73/dev-utils 1.0.53 → 1.0.54

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.
@@ -7,30 +7,35 @@ exports.CmdGenI18n = void 0;
7
7
  const utils_1 = require("@zwa73/utils");
8
8
  const ts_morph_1 = require("ts-morph");
9
9
  const UtilAst_1 = require("../UtilAst");
10
- const fs_1 = __importDefault(require("fs"));
11
10
  const pathe_1 = __importDefault(require("pathe"));
12
- async function procFile(p, filePath) {
11
+ function format(str) {
12
+ return str.replace(/\\(.)/g, '$1').replace(/%/g, '\\%');
13
+ }
14
+ async function procFile(p, filePath, funckey) {
13
15
  const date = new Date().toISOString();
14
- const content = await fs_1.default.promises.readFile(filePath, 'utf-8');
15
- const b = p.createSourceFile(pathe_1.default.relative(process.cwd(), filePath), content);
16
+ //const content = await fs.promises.readFile(filePath,'utf-8');
17
+ const b = p.addSourceFileAtPath(filePath);
18
+ //const b = p.createSourceFile(path.relative(process.cwd(),filePath),content,{overwrite:true});
16
19
  const ds = b.getDescendantsOfKind(ts_morph_1.SyntaxKind.TaggedTemplateExpression);
17
20
  const dats = [];
18
21
  for (const d of ds) {
22
+ if (!funckey.includes(d.getTag().getText()))
23
+ continue;
19
24
  const template = d.getTemplate();
20
25
  let result = '';
21
26
  let i = 0;
22
27
  UtilAst_1.UtilAst.withKind(template, {
23
- [ts_morph_1.SyntaxKind.NoSubstitutionTemplateLiteral]: (t) => result += t.getLiteralText(),
28
+ [ts_morph_1.SyntaxKind.NoSubstitutionTemplateLiteral]: (t) => result += format(t.getLiteralText()),
24
29
  [ts_morph_1.SyntaxKind.TemplateExpression]: (t) => {
25
30
  t.getChildren().forEach((c) => {
26
31
  UtilAst_1.UtilAst.withKind(c, {
27
- [ts_morph_1.SyntaxKind.TemplateHead]: (sc) => result += sc.getLiteralText(),
32
+ [ts_morph_1.SyntaxKind.TemplateHead]: (sc) => result += format(sc.getLiteralText()),
28
33
  [ts_morph_1.SyntaxKind.SyntaxList]: (sc) => {
29
34
  sc.getChildrenOfKind(ts_morph_1.SyntaxKind.TemplateSpan).forEach((span) => {
30
35
  span.getChildren().forEach((ssc) => {
31
36
  result += UtilAst_1.UtilAst.withKind(ssc, {
32
- [ts_morph_1.SyntaxKind.TemplateMiddle]: (sssc) => sssc.getLiteralText(),
33
- [ts_morph_1.SyntaxKind.TemplateTail]: (sssc) => sssc.getLiteralText(),
37
+ [ts_morph_1.SyntaxKind.TemplateMiddle]: (sssc) => format(sssc.getLiteralText()),
38
+ [ts_morph_1.SyntaxKind.TemplateTail]: (sssc) => format(sssc.getLiteralText()),
34
39
  }) ?? String(`%${i++}`);
35
40
  });
36
41
  });
@@ -51,12 +56,14 @@ async function procFile(p, filePath) {
51
56
  }
52
57
  return dats;
53
58
  }
54
- async function scanI18n(i18nDataDir, include, exclude) {
59
+ async function scanI18n(i18nDataDir, include, exclude, funckey) {
60
+ const fixedKey = typeof funckey == 'string' ? [funckey] : funckey;
55
61
  const filePaths = (await utils_1.UtilFT.fileSearchGlob(process.cwd(), include, { ingore: exclude }))
56
62
  .filter(p => ['.ts', '.tsx'].includes(pathe_1.default.parse(p).ext));
57
63
  const project = new ts_morph_1.Project();
58
64
  await utils_1.SI18n.init(i18nDataDir, '*');
59
- (await Promise.all(filePaths.map(p => procFile(project, p))))
65
+ //console.log(filePaths);
66
+ (await Promise.all(filePaths.map(p => procFile(project, p, fixedKey))))
60
67
  .flat().forEach(dat => utils_1.SI18n.addOriginalText(dat));
61
68
  await utils_1.SI18n.saveTable();
62
69
  }
@@ -65,10 +72,23 @@ const CmdGenI18n = (program) => program
65
72
  .command("Gen-I18n")
66
73
  .alias("geni18n")
67
74
  .description("根据文件行为树生成i18n数据")
68
- .option("-o, --output <path>", "输出的数据目录位置")
69
- .option("-i, --include <glob>", "包含的glob 默认 src/**/*.macro.ts", "src/**/*.macro.ts")
75
+ .option("-o, --output <path>", "输出的数据目录位置 默认 i18n", 'i18n')
76
+ .option("-i, --include <glob>", "包含的glob 默认 src/**/*.ts", "src/**/*.ts")
70
77
  .option("-g, --exclude <glob>", "忽略的glob")
78
+ .option("-f, --funckey <string|string[]>", "扫描的目标函数名, 默认 t", (i) => {
79
+ const v = (0, utils_1.ivk)(() => {
80
+ try {
81
+ return JSON.parse(i);
82
+ }
83
+ catch (e) {
84
+ return undefined;
85
+ }
86
+ });
87
+ if (Array.isArray(v))
88
+ return v;
89
+ return i;
90
+ }, 't')
71
91
  .action(async (opt) => {
72
- await scanI18n(opt.output, opt.include, opt.exclude);
92
+ await scanI18n(opt.output, opt.include, opt.exclude, opt.funckey);
73
93
  });
74
94
  exports.CmdGenI18n = CmdGenI18n;
@@ -0,0 +1,23 @@
1
+ {
2
+ "base_lang": "zh-CN",
3
+ "texts": [
4
+ {
5
+ "original": "你好?%0,吗?",
6
+ "scan_time": "2024-07-22T04:08:33.552Z",
7
+ "position": "F:/Sosarciel/SosarcielCore/NodeJs/dev-utils/jest/UtilI18n/test.ts:6:13",
8
+ "source": "Ast"
9
+ },
10
+ {
11
+ "original": "abcdefg %0rst",
12
+ "scan_time": "2024-07-22T04:08:33.552Z",
13
+ "position": "F:/Sosarciel/SosarcielCore/NodeJs/dev-utils/jest/UtilI18n/test.ts:7:13",
14
+ "source": "Ast"
15
+ },
16
+ {
17
+ "original": "higklmn%0",
18
+ "scan_time": "2024-07-22T04:08:33.552Z",
19
+ "position": "F:/Sosarciel/SosarcielCore/NodeJs/dev-utils/jest/UtilI18n/test.ts:7:25",
20
+ "source": "Ast"
21
+ }
22
+ ]
23
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "base_lang": "zh-CN",
3
+ "modify_time": "2024-07-22T04:08:33.570Z",
4
+ "target_lang": "en",
5
+ "translate_table": {
6
+ "你好?%0,吗?": "hello? %0 \\%0",
7
+ "abcdefg %0rst": "ABCDEFG %0RST",
8
+ "higklmn%0": "HIJKLMN%0"
9
+ }
10
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "base_lang": "zh-CN",
3
+ "modify_time": "2024-07-22T04:08:33.570Z",
4
+ "target_lang": "template",
5
+ "translate_table": {
6
+ "你好?%0,吗?": null,
7
+ "abcdefg %0rst": null,
8
+ "higklmn%0": null
9
+ }
10
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/dev-utils",
3
- "version": "1.0.53",
3
+ "version": "1.0.54",
4
4
  "description": "编译与调试工具",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -16,7 +16,7 @@
16
16
  "author": "zwa73",
17
17
  "license": "ISC",
18
18
  "dependencies": {
19
- "@zwa73/utils": "^1.0.143",
19
+ "@zwa73/utils": "^1.0.144",
20
20
  "commander": "^11.1.0",
21
21
  "pathe": "^1.1.2",
22
22
  "ts-morph": "^23.0.0",
@@ -1,32 +1,37 @@
1
1
  import { Command } from "commander";
2
- import { I18nTextData, SI18n, UtilFT } from "@zwa73/utils";
3
- import { Project,SourceFile,SyntaxKind } from 'ts-morph';
2
+ import { AnyFunc, I18nTextData, SI18n, UtilFT, ivk } from "@zwa73/utils";
3
+ import { Project,SyntaxKind } from 'ts-morph';
4
4
  import { UtilAst } from "@src/UtilAst";
5
- import fs from 'fs';
6
5
  import path from 'pathe';
7
6
 
8
- async function procFile(p:Project,filePath:string){
7
+ function format(str:string){
8
+ return str.replace(/\\(.)/g,'$1').replace(/%/g,'\\%');
9
+ }
10
+
11
+ async function procFile(p:Project,filePath:string,funckey:string[]){
9
12
  const date = new Date().toISOString();
10
- const content = await fs.promises.readFile(filePath,'utf-8');
11
- const b = p.createSourceFile(path.relative(process.cwd(),filePath),content);
13
+ //const content = await fs.promises.readFile(filePath,'utf-8');
14
+ const b = p.addSourceFileAtPath(filePath);
15
+ //const b = p.createSourceFile(path.relative(process.cwd(),filePath),content,{overwrite:true});
12
16
  const ds = b.getDescendantsOfKind(SyntaxKind.TaggedTemplateExpression);
13
17
  const dats:I18nTextData[] = [];
14
18
  for(const d of ds){
19
+ if(!funckey.includes(d.getTag().getText())) continue;
15
20
  const template = d.getTemplate();
16
21
  let result = '';
17
22
  let i=0;
18
23
  UtilAst.withKind(template,{
19
- [SyntaxKind.NoSubstitutionTemplateLiteral]:(t)=>result+=t.getLiteralText(),
24
+ [SyntaxKind.NoSubstitutionTemplateLiteral]:(t)=>result+=format(t.getLiteralText()),
20
25
  [SyntaxKind.TemplateExpression]:(t)=>{
21
26
  t.getChildren().forEach((c)=>{
22
27
  UtilAst.withKind(c,{
23
- [SyntaxKind.TemplateHead]:(sc)=>result += sc.getLiteralText(),
28
+ [SyntaxKind.TemplateHead]:(sc)=>result += format(sc.getLiteralText()),
24
29
  [SyntaxKind.SyntaxList]:(sc)=>{
25
30
  sc.getChildrenOfKind(SyntaxKind.TemplateSpan).forEach((span)=>{
26
31
  span.getChildren().forEach((ssc)=>{
27
32
  result += UtilAst.withKind(ssc,{
28
- [SyntaxKind.TemplateMiddle]:(sssc)=>sssc.getLiteralText(),
29
- [SyntaxKind.TemplateTail] :(sssc)=>sssc.getLiteralText(),
33
+ [SyntaxKind.TemplateMiddle]:(sssc)=>format(sssc.getLiteralText()),
34
+ [SyntaxKind.TemplateTail] :(sssc)=>format(sssc.getLiteralText()),
30
35
  })??String(`%${i++}`);
31
36
  })
32
37
  })
@@ -48,12 +53,14 @@ async function procFile(p:Project,filePath:string){
48
53
  return dats;
49
54
  }
50
55
 
51
- async function scanI18n(i18nDataDir:string,include:string,exclude:string){
56
+ async function scanI18n(i18nDataDir:string,include:string,exclude:string,funckey:string[]|string){
57
+ const fixedKey = typeof funckey == 'string' ? [funckey] : funckey;
52
58
  const filePaths = (await UtilFT.fileSearchGlob(process.cwd(),include,{ingore:exclude}))
53
59
  .filter(p=>['.ts','.tsx'].includes(path.parse(p).ext));
54
60
  const project = new Project();
55
61
  await SI18n.init(i18nDataDir,'*');
56
- (await Promise.all(filePaths.map(p=>procFile(project,p))))
62
+ //console.log(filePaths);
63
+ (await Promise.all(filePaths.map(p=>procFile(project,p,fixedKey))))
57
64
  .flat().forEach(dat=>SI18n.addOriginalText(dat));
58
65
  await SI18n.saveTable();
59
66
  }
@@ -63,13 +70,25 @@ export const CmdGenI18n = (program:Command) => program
63
70
  .command("Gen-I18n")
64
71
  .alias("geni18n")
65
72
  .description("根据文件行为树生成i18n数据")
66
- .option("-o, --output <path>" , "输出的数据目录位置")
67
- .option("-i, --include <glob>", "包含的glob 默认 src/**/*.macro.ts","src/**/*.macro.ts")
73
+ .option("-o, --output <path>" , "输出的数据目录位置 默认 i18n",'i18n')
74
+ .option("-i, --include <glob>", "包含的glob 默认 src/**/*.ts","src/**/*.ts")
68
75
  .option("-g, --exclude <glob>", "忽略的glob")
76
+ .option("-f, --funckey <string|string[]>", "扫描的目标函数名, 默认 t",(i:string)=>{
77
+ const v = ivk(()=>{
78
+ try{
79
+ return JSON.parse(i);
80
+ } catch(e){
81
+ return undefined;
82
+ }
83
+ })
84
+ if(Array.isArray(v)) return v;
85
+ return i;
86
+ },'t')
69
87
  .action(async (opt) => {
70
88
  await scanI18n(
71
- opt.output,
89
+ opt.output ,
72
90
  opt.include,
73
91
  opt.exclude,
92
+ opt.funckey,
74
93
  );
75
94
  });