td-octopus 0.0.17 → 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/back/index.js +147 -142
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "td-octopus",
3
- "version": "0.0.17",
3
+ "version": "0.0.18",
4
4
  "description": "I18N tool",
5
5
  "author": "Anthony Li",
6
6
  "bin": {
package/src/back/index.js CHANGED
@@ -3,191 +3,196 @@
3
3
  * @Author: 郑泳健
4
4
  * @Date: 2022-06-01 13:56:18
5
5
  * @LastEditors: 郑泳健
6
- * @LastEditTime: 2023-01-11 15:14:00
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
- // 匹配I18N.
18
- const I18N_REGEX = /I18N(\.[a-zA-Z0-9_]+)+/g
18
+ // 匹配I18N.
19
+ const I18N_REGEX = /I18N(\.[a-zA-Z0-9_]+)+/g
19
20
 
20
- // 匹配I18N.template()里面的内容
21
- const TEMPLATE_INNER_REGEX = /I18N\.template\(([\s\S]*?})\)/g
21
+ // 匹配I18N.template()里面的内容
22
+ const TEMPLATE_INNER_REGEX = /I18N\.template\(([\s\S]*?})\)/g
22
23
 
23
- // 匹配{}之间的内容
24
- const JSON_KEY_REGEX = /{([\s\S]*?)}/g
24
+ // 匹配{}之间的内容
25
+ const JSON_KEY_REGEX = /{([\s\S]*?)}/g
25
26
 
26
- // 匹配{}之间的value
27
- const JSON_VALYR_REGEX = /(?<=: )[^,}]+/g
27
+ // 匹配{}之间的value
28
+ const JSON_VALYR_REGEX = /(?<=: )[^,}]+/g
28
29
 
29
- /**
30
- * 获取需要back的所有文件目录
31
- * @returns
32
- */
33
- function getFilePaths() {
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
- const dirPath = path.resolve(process.cwd(), i);
40
- const files = getSpecifiedFiles(dirPath)
41
- filePaths = filePaths.concat(files)
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
- let flag = false;
46
- for (let index = 0; index < CONFIG.fileSuffix.length; index++) {
47
- const element = CONFIG.fileSuffix[index];
48
- flag = file.endsWith(element);
49
- if (flag) {
50
- break;
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
- * 获取I18N.template里面{}每个value对应的value
59
- * @param {*} str {val1: h, val2: m, val3: s}
60
- * @returns [h, m, s]
61
- */
62
- function getTemplateValue(str) {
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
- const values = str.match(JSON_VALYR_REGEX)
65
- return values;
65
+ const values = str.match(JSON_VALYR_REGEX)
66
+ return values;
66
67
  } catch (e) {
67
- throw e;
68
+ throw e;
68
69
  }
69
- }
70
-
71
- /**
72
- * 如果文件中存在I18N.template,先转换一下
73
- * @param {*} code
74
- * @param {*} flatObj
75
- * @returns
76
- */
77
- function transformTemplate(filePath, code, flatObj) {
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
- const templates = code.match(TEMPLATE_INNER_REGEX) || [];
80
- let sum = Array.isArray(templates) ? templates.length : 0;
81
- if (Array.isArray(templates) && templates.length) {
82
- templates.forEach(i => {
83
- const [jsonStr] = i ? i.match(JSON_KEY_REGEX) : []
84
- const keys = getTemplateValue(jsonStr)
85
- let matchList = getValueByI18N(filePath, i, flatObj) || [];
86
- matchText = matchList[0] && matchList[0]['value']
87
- if (matchText) {
88
- matchText = matchText.replace(/{(val\d+)}/g, (match, group, index) => {
89
- if(!keys[0]) {
90
- throw new Error(`${filePath}文件中的 ${highlightText(i)} 转换有问题,请手动检查`)
91
- }
92
- const res = '${' + keys[0].trim() + '}'
93
- keys.splice(0, 1)
94
- return res
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
- code = code.replace(i, '`' + matchText + '`')
98
- }
99
- })
100
- }
101
- return { code, sum }
101
+ }
102
+ return { code, sum }
102
103
  } catch (e) {
103
- throw e;
104
+ throw e;
104
105
  }
105
- }
106
-
107
- /**
108
- * 获取I18N.xx.xx所对应的中文
109
- * @param {*} filePath 文件路径
110
- * @param {*} str
111
- * @param {*} flatObj
112
- * @returns { key: I18N.xx, value: '测试' }
113
- */
114
- function getValueByI18N(filePath, str, flatObj) {
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
- const matchs = str.match(I18N_REGEX);
117
- if (!Array.isArray(matchs) || !matchs.length) {
118
- return [];
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
- return matchList
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
- throw e;
136
+ throw e;
136
137
  }
137
- }
138
-
139
- /**
140
- * 获取需要被重写的文件
141
- * @param {*} filePath 文件路径
142
- * @param {*} flatObj 所有key对应的中文
143
- * @returns
144
- */
145
- function getNeedRewriteFiles(filePath, flatObj) {
138
+ }
139
+
140
+ /**
141
+ * 获取需要被重写的文件
142
+ * @param {*} filePath 文件路径
143
+ * @param {*} flatObj 所有key对应的中文
144
+ * @returns
145
+ */
146
+ function getNeedRewriteFiles(filePath, flatObj) {
146
147
  try {
147
- const str = fs.readFileSync(filePath, 'utf-8');
148
- let { code, sum } = transformTemplate(filePath, str, flatObj)
149
-
150
- let matchList = getValueByI18N(filePath, code, flatObj) || [];
151
- if (Array.isArray(matchList) && matchList.length) {
152
- sum += matchList.length;
153
- matchList.forEach(({ key, value }) => {
154
- const replaceVal = value && value.includes("'") ? '"' + value + '"' : "'" + value + "'"
155
- code = code.replace(key, replaceVal)
156
- })
157
- }
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
+ }
158
159
 
159
- return {
160
- filePath,
161
- code,
162
- sum
163
- }
160
+ return {
161
+ filePath,
162
+ code,
163
+ sum
164
+ }
164
165
  } catch (e) {
165
- throw e;
166
+ throw e;
166
167
  }
167
- }
168
+ }
168
169
 
169
- // 同步不同的语言包
170
- function back() {
170
+ // 同步不同的语言包
171
+ function back() {
171
172
  try {
172
- const filePaths = getFilePaths()
173
- const zhCN = syncLang('zh-CN');
174
- const zhCNFlat = flatObject(zhCN);
175
- /** 获取需要被重写的文件 */
176
- const needRewriteFiles = filePaths.map(i => getNeedRewriteFiles(i, zhCNFlat))
177
-
178
- needRewriteFiles.forEach(({ filePath, code, sum }) => {
179
- if (sum > 0) {
180
- console.log(`正在对 ${highlightText(filePath)} 文件进行回滚操作, 共计${highlightText(sum)}处`);
181
-
182
- fs.writeFileSync(filePath, code)
183
- console.log(`${highlightText(filePath)} 文件回滚完毕`);
184
- }
185
- })
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文件夹下的文件');
186
191
  } catch (e) {
187
- failInfo(e.message)
192
+ failInfo(e.message)
188
193
  }
189
- }
194
+ }
190
195
 
191
- module.exports = {
196
+ module.exports = {
192
197
  back
193
- }
198
+ }