td-octopus 0.1.17 → 0.1.18
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/package.json +3 -2
- package/src/back/index.js +32 -14
- package/src/check/index.js +10 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "td-octopus",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.18",
|
|
4
4
|
"description": "I18N tool",
|
|
5
5
|
"author": "Anthony Li",
|
|
6
6
|
"bin": {
|
|
@@ -27,7 +27,8 @@
|
|
|
27
27
|
"update-notifier": "^4.1.0",
|
|
28
28
|
"vue-template-compiler": "^2.6.14",
|
|
29
29
|
"xlsx": "0.16.9",
|
|
30
|
-
"yargs": "^15.3.1"
|
|
30
|
+
"yargs": "^15.3.1",
|
|
31
|
+
"eslint": "^8.15.0"
|
|
31
32
|
},
|
|
32
33
|
"keywords": [
|
|
33
34
|
"I18N",
|
package/src/back/index.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* @Author: 郑泳健
|
|
4
4
|
* @Date: 2022-06-01 13:56:18
|
|
5
5
|
* @LastEditors: 郑泳健
|
|
6
|
-
* @LastEditTime:
|
|
6
|
+
* @LastEditTime: 2025-11-11 10:10:55
|
|
7
7
|
*/
|
|
8
8
|
const path = require('path')
|
|
9
9
|
const fs = require('fs')
|
|
@@ -27,8 +27,6 @@ const TEMPLATE_INNER_REGEX = /I18N\.template\(([\s\S]*?})\)/g
|
|
|
27
27
|
// 匹配{}之间的内容
|
|
28
28
|
const JSON_KEY_REGEX = /{([\s\S]*?)}/g
|
|
29
29
|
|
|
30
|
-
// 匹配{}之间的value
|
|
31
|
-
const JSON_VALYR_REGEX = /(?<=: )[^,}]+/g
|
|
32
30
|
|
|
33
31
|
/**
|
|
34
32
|
* 获取需要back的所有文件目录
|
|
@@ -61,14 +59,39 @@ function getFilePaths() {
|
|
|
61
59
|
/**
|
|
62
60
|
* 获取I18N.template里面{}每个value对应的value
|
|
63
61
|
* @param {*} str {val1: h, val2: m, val3: s}
|
|
64
|
-
* @returns
|
|
62
|
+
* @returns obj
|
|
65
63
|
*/
|
|
66
64
|
function getTemplateValue(str) {
|
|
67
65
|
try {
|
|
68
|
-
const
|
|
69
|
-
|
|
66
|
+
const result = {};
|
|
67
|
+
const regex = /(\w+)\s*:\s*/g;
|
|
68
|
+
let match;
|
|
69
|
+
|
|
70
|
+
while ((match = regex.exec(objStr)) !== null) {
|
|
71
|
+
const key = match[1].trim();
|
|
72
|
+
let valueStart = regex.lastIndex;
|
|
73
|
+
let bracketCount = 0;
|
|
74
|
+
let valueEnd = valueStart;
|
|
75
|
+
|
|
76
|
+
for (let i = valueStart; i < objStr.length; i++) {
|
|
77
|
+
const char = objStr[i];
|
|
78
|
+
if (char === '(') bracketCount++;
|
|
79
|
+
if (char === ')') bracketCount--;
|
|
80
|
+
if ((char === ',' && bracketCount === 0) || (char === '}' && bracketCount === 0)) {
|
|
81
|
+
valueEnd = i;
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const value = objStr.slice(valueStart, valueEnd).trim();
|
|
87
|
+
result[key] = value;
|
|
88
|
+
|
|
89
|
+
regex.lastIndex = valueEnd + 1; // 继续匹配下一个
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return result;
|
|
70
93
|
} catch (e) {
|
|
71
|
-
throw e;
|
|
94
|
+
throw new Error(`获取I8N.template内的变量出错: ${e.message}`);
|
|
72
95
|
}
|
|
73
96
|
}
|
|
74
97
|
|
|
@@ -89,13 +112,8 @@ function transformTemplate(filePath, code, flatObj) {
|
|
|
89
112
|
let matchList = getValueByI18N(filePath, i, flatObj) || [];
|
|
90
113
|
matchText = matchList[0] && matchList[0]['value']
|
|
91
114
|
if (matchText) {
|
|
92
|
-
matchText = matchText.replace(/{(val\d+)}/g, (
|
|
93
|
-
|
|
94
|
-
throw new Error(`${filePath}文件中的 ${highlightText(i)} 转换有问题,请手动检查`)
|
|
95
|
-
}
|
|
96
|
-
const res = '${' + keys[0].trim() + '}'
|
|
97
|
-
keys.splice(0, 1)
|
|
98
|
-
return res
|
|
115
|
+
matchText = matchText.replace(/{(val\d+)}/g, (_, transKey) => {
|
|
116
|
+
return `\${${keys[transKey]}}`;
|
|
99
117
|
})
|
|
100
118
|
|
|
101
119
|
code = code.replace(i, '`' + matchText + '`')
|
package/src/check/index.js
CHANGED
|
@@ -3,13 +3,18 @@
|
|
|
3
3
|
* @Author: 郑泳健
|
|
4
4
|
* @Date: 2024-12-12 15:00:24
|
|
5
5
|
* @LastEditors: 郑泳健
|
|
6
|
-
* @LastEditTime: 2025-
|
|
6
|
+
* @LastEditTime: 2025-11-11 09:57:48
|
|
7
7
|
*/
|
|
8
8
|
const path = require('path');
|
|
9
9
|
const fs = require('fs');
|
|
10
|
+
const { parse } = require('@babel/parser');
|
|
11
|
+
const generate = require('@babel/generator').default;
|
|
10
12
|
const syncLang = require('../utils/syncLang');
|
|
11
13
|
const { flatObject, rewriteFiles, getFileKeyValueList } = require('../utils/translate');
|
|
12
14
|
const { autoImportJSFiles } = require('../utils/index');
|
|
15
|
+
|
|
16
|
+
const { ESLint } = require('eslint');
|
|
17
|
+
|
|
13
18
|
const ora = require('ora');
|
|
14
19
|
|
|
15
20
|
const spinner = ora('开始check');
|
|
@@ -39,7 +44,10 @@ function readJsFiles(folderPath) {
|
|
|
39
44
|
jsContent += readJsFiles(fullPath);
|
|
40
45
|
} else if (path.extname(fullPath) === '.js') {
|
|
41
46
|
// 如果是 .js 文件,读取文件内容
|
|
42
|
-
|
|
47
|
+
let content = fs.readFileSync(fullPath, 'utf-8');
|
|
48
|
+
const ast = parse(source, { sourceType: 'module', plugins: ['jsx', 'typescript'] });
|
|
49
|
+
const output = generate(ast, { comments: false });
|
|
50
|
+
content = output.code;
|
|
43
51
|
jsContent += `\n/* File: ${fullPath} */\n${content}\n`;
|
|
44
52
|
}
|
|
45
53
|
}
|