td-octopus 0.0.16 → 0.0.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 +1 -1
- package/src/back/index.js +147 -141
package/package.json
CHANGED
package/src/back/index.js
CHANGED
|
@@ -3,190 +3,196 @@
|
|
|
3
3
|
* @Author: 郑泳健
|
|
4
4
|
* @Date: 2022-06-01 13:56:18
|
|
5
5
|
* @LastEditors: 郑泳健
|
|
6
|
-
* @LastEditTime: 2023-01-11
|
|
6
|
+
* @LastEditTime: 2023-01-11 17:23:29
|
|
7
7
|
*/
|
|
8
8
|
const path = require('path')
|
|
9
9
|
const fs = require('fs')
|
|
10
10
|
const ts = require('typescript');
|
|
11
|
+
const shell = require('shelljs');
|
|
11
12
|
const { getSpecifiedFiles, isFile } = require('../utils/file')
|
|
12
13
|
const syncLang = require('../utils/syncLang')
|
|
13
14
|
const { flatObject } = require('../utils/translate');
|
|
14
15
|
const { failInfo, highlightText } = require('../utils/colors');
|
|
15
16
|
const { getProjectConfig } = require('../utils/index')
|
|
16
17
|
|
|
17
|
-
|
|
18
|
-
|
|
18
|
+
// 匹配I18N.
|
|
19
|
+
const I18N_REGEX = /I18N(\.[a-zA-Z0-9_]+)+/g
|
|
19
20
|
|
|
20
|
-
|
|
21
|
-
|
|
21
|
+
// 匹配I18N.template()里面的内容
|
|
22
|
+
const TEMPLATE_INNER_REGEX = /I18N\.template\(([\s\S]*?})\)/g
|
|
22
23
|
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
// 匹配{}之间的内容
|
|
25
|
+
const JSON_KEY_REGEX = /{([\s\S]*?)}/g
|
|
25
26
|
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
// 匹配{}之间的value
|
|
28
|
+
const JSON_VALYR_REGEX = /(?<=: )[^,}]+/g
|
|
28
29
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
/**
|
|
31
|
+
* 获取需要back的所有文件目录
|
|
32
|
+
* @returns
|
|
33
|
+
*/
|
|
34
|
+
function getFilePaths() {
|
|
34
35
|
const CONFIG = getProjectConfig()
|
|
35
36
|
const dirArr = CONFIG.include && CONFIG.include.length > 0 ? CONFIG.include : ['./'];
|
|
36
37
|
let filePaths = []
|
|
37
38
|
|
|
38
39
|
dirArr.forEach(i => {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
const dirPath = path.resolve(process.cwd(), i);
|
|
41
|
+
const files = getSpecifiedFiles(dirPath)
|
|
42
|
+
filePaths = filePaths.concat(files)
|
|
42
43
|
})
|
|
43
44
|
|
|
44
45
|
return filePaths.filter(file => {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
46
|
+
let flag = false;
|
|
47
|
+
for (let index = 0; index < CONFIG.fileSuffix.length; index++) {
|
|
48
|
+
const element = CONFIG.fileSuffix[index];
|
|
49
|
+
flag = file.endsWith(element);
|
|
50
|
+
if (flag) {
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
51
53
|
}
|
|
52
|
-
|
|
53
|
-
return (isFile(file) && flag);
|
|
54
|
+
return (isFile(file) && flag);
|
|
54
55
|
});
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 获取I18N.template里面{}每个value对应的value
|
|
60
|
+
* @param {*} str {val1: h, val2: m, val3: s}
|
|
61
|
+
* @returns [h, m, s]
|
|
62
|
+
*/
|
|
63
|
+
function getTemplateValue(str) {
|
|
63
64
|
try {
|
|
64
|
-
|
|
65
|
-
|
|
65
|
+
const values = str.match(JSON_VALYR_REGEX)
|
|
66
|
+
return values;
|
|
66
67
|
} catch (e) {
|
|
67
|
-
|
|
68
|
+
throw e;
|
|
68
69
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 如果文件中存在I18N.template,先转换一下
|
|
74
|
+
* @param {*} code
|
|
75
|
+
* @param {*} flatObj
|
|
76
|
+
* @returns
|
|
77
|
+
*/
|
|
78
|
+
function transformTemplate(filePath, code, flatObj) {
|
|
78
79
|
try {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
80
|
+
const templates = code.match(TEMPLATE_INNER_REGEX) || [];
|
|
81
|
+
let sum = Array.isArray(templates) ? templates.length : 0;
|
|
82
|
+
if (Array.isArray(templates) && templates.length) {
|
|
83
|
+
templates.forEach(i => {
|
|
84
|
+
const [jsonStr] = i ? i.match(JSON_KEY_REGEX) : []
|
|
85
|
+
const keys = getTemplateValue(jsonStr)
|
|
86
|
+
let matchList = getValueByI18N(filePath, i, flatObj) || [];
|
|
87
|
+
matchText = matchList[0] && matchList[0]['value']
|
|
88
|
+
if (matchText) {
|
|
89
|
+
matchText = matchText.replace(/{(val\d+)}/g, (match, group, index) => {
|
|
90
|
+
if (!keys[0]) {
|
|
91
|
+
throw new Error(`${filePath}文件中的 ${highlightText(i)} 转换有问题,请手动检查`)
|
|
92
|
+
}
|
|
93
|
+
const res = '${' + keys[0].trim() + '}'
|
|
94
|
+
keys.splice(0, 1)
|
|
95
|
+
return res
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
code = code.replace(i, '`' + matchText + '`')
|
|
99
|
+
}
|
|
95
100
|
})
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
}
|
|
99
|
-
})
|
|
100
|
-
}
|
|
101
|
-
return { code, sum }
|
|
101
|
+
}
|
|
102
|
+
return { code, sum }
|
|
102
103
|
} catch (e) {
|
|
103
|
-
|
|
104
|
+
throw e;
|
|
104
105
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* 获取I18N.xx.xx所对应的中文
|
|
110
|
+
* @param {*} filePath 文件路径
|
|
111
|
+
* @param {*} str
|
|
112
|
+
* @param {*} flatObj
|
|
113
|
+
* @returns { key: I18N.xx, value: '测试' }
|
|
114
|
+
*/
|
|
115
|
+
function getValueByI18N(filePath, str, flatObj) {
|
|
115
116
|
try {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
const matchList = matchs.reduce((total, item) => {
|
|
122
|
-
if (!item.includes('I18N.template') && !item.includes('I18N.setLang')) {
|
|
123
|
-
const text = flatObj[item.replace('I18N.', '')]
|
|
124
|
-
if (text) {
|
|
125
|
-
total.push({ key: item, value: text })
|
|
126
|
-
} else {
|
|
127
|
-
throw new Error(`${filePath}文件中的 ${highlightText(item)} 未发现有对应的中文文案,请检查`);
|
|
128
|
-
}
|
|
117
|
+
const matchs = str.match(I18N_REGEX);
|
|
118
|
+
if (!Array.isArray(matchs) || !matchs.length) {
|
|
119
|
+
return [];
|
|
129
120
|
}
|
|
130
|
-
return total
|
|
131
|
-
}, [])
|
|
132
121
|
|
|
133
|
-
|
|
122
|
+
const matchList = matchs.reduce((total, item) => {
|
|
123
|
+
if (!item.includes('I18N.template') && !item.includes('I18N.setLang')) {
|
|
124
|
+
const text = flatObj[item.replace('I18N.', '')]
|
|
125
|
+
if (text) {
|
|
126
|
+
total.push({ key: item, value: text })
|
|
127
|
+
} else {
|
|
128
|
+
throw new Error(`${filePath}文件中的 ${highlightText(item)} 未发现有对应的中文文案,请检查`);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return total
|
|
132
|
+
}, [])
|
|
133
|
+
|
|
134
|
+
return matchList
|
|
134
135
|
} catch (e) {
|
|
135
|
-
|
|
136
|
+
throw e;
|
|
136
137
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* 获取需要被重写的文件
|
|
142
|
+
* @param {*} filePath 文件路径
|
|
143
|
+
* @param {*} flatObj 所有key对应的中文
|
|
144
|
+
* @returns
|
|
145
|
+
*/
|
|
146
|
+
function getNeedRewriteFiles(filePath, flatObj) {
|
|
146
147
|
try {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
148
|
+
const str = fs.readFileSync(filePath, 'utf-8');
|
|
149
|
+
let { code, sum } = transformTemplate(filePath, str, flatObj)
|
|
150
|
+
|
|
151
|
+
let matchList = getValueByI18N(filePath, code, flatObj) || [];
|
|
152
|
+
if (Array.isArray(matchList) && matchList.length) {
|
|
153
|
+
sum += matchList.length;
|
|
154
|
+
matchList.forEach(({ key, value }) => {
|
|
155
|
+
const replaceVal = value && value.includes("'") ? '"' + value + '"' : "'" + value + "'"
|
|
156
|
+
code = code.replace(key, replaceVal)
|
|
157
|
+
})
|
|
158
|
+
}
|
|
157
159
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
160
|
+
return {
|
|
161
|
+
filePath,
|
|
162
|
+
code,
|
|
163
|
+
sum
|
|
164
|
+
}
|
|
163
165
|
} catch (e) {
|
|
164
|
-
|
|
166
|
+
throw e;
|
|
165
167
|
}
|
|
166
|
-
|
|
168
|
+
}
|
|
167
169
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
+
// 同步不同的语言包
|
|
171
|
+
function back() {
|
|
170
172
|
try {
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
173
|
+
const filePaths = getFilePaths()
|
|
174
|
+
const zhCN = syncLang('zh-CN');
|
|
175
|
+
const zhCNFlat = flatObject(zhCN);
|
|
176
|
+
/** 获取需要被重写的文件 */
|
|
177
|
+
const needRewriteFiles = filePaths.map(i => getNeedRewriteFiles(i, zhCNFlat))
|
|
178
|
+
|
|
179
|
+
needRewriteFiles.forEach(({ filePath, code, sum }) => {
|
|
180
|
+
if (sum > 0) {
|
|
181
|
+
console.log(`正在对 ${highlightText(filePath)} 文件进行回滚操作, 共计${highlightText(sum)}处`);
|
|
182
|
+
|
|
183
|
+
fs.writeFileSync(filePath, code)
|
|
184
|
+
console.log(`${highlightText(filePath)} 文件回滚完毕`);
|
|
185
|
+
}
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
console.log('正在删除zh-CN文件夹下的文件');
|
|
189
|
+
shell.rm('-rf', path.resolve(process.cwd(), './.octopus/zh-CN/*'));
|
|
190
|
+
console.log('已成功删除zh-CN文件夹下的文件');
|
|
185
191
|
} catch (e) {
|
|
186
|
-
|
|
192
|
+
failInfo(e.message)
|
|
187
193
|
}
|
|
188
|
-
|
|
194
|
+
}
|
|
189
195
|
|
|
190
|
-
|
|
196
|
+
module.exports = {
|
|
191
197
|
back
|
|
192
|
-
|
|
198
|
+
}
|