ztxkutils 2.10.66-14 → 2.10.66-15

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.
@@ -1,342 +1,351 @@
1
- const fs = require('fs');
2
- const path = require('path');
3
- const glob = require('glob');
4
- const { transformZeroToDi18n } = require('../../../zti18n-core');
5
- const mergeOptions = require('../utils/mergeOptions');
6
- const isChinese = require('../utils/isChinese');
7
- const log = require('../utils/log');
8
-
9
- function getSourceFiles({ entry, exclude }) {
10
- return glob.sync(`${entry}/**/*.{js,ts,tsx,jsx,vue}`, {
11
- ignore: exclude || [],
12
- });
13
- }
14
-
15
- /**
16
- * 同步一个非中文语言的配置
17
- */
18
- async function syncOneNonChineseLocalesConf(
19
- key,
20
- option,
21
- chineseLocales,
22
- updatedTranslatedWord,
23
- publish,
24
- confService
25
- ) {
26
- const confName = key;
27
- const { disableAutoTranslate, translator } = option;
28
-
29
- const translate = translator
30
- ? require(path.join(process.cwd(), translator))
31
- : require('../translate/google');
32
-
33
- log.warning(`start to update conf ${confName}`);
34
-
35
- const res = await confService.getConf(confName, key);
36
-
37
- if (res.code !== 0) {
38
- log.error(`! failed to read apollo conf, ${res.message}`);
39
- return null;
40
- }
41
-
42
- const allKeys = Object.keys(chineseLocales);
43
- let mergedLocales = {};
44
-
45
- res.data.forEach((item) => {
46
- if (updatedTranslatedWord.hasOwnProperty(item.key)) {
47
- mergedLocales[updatedTranslatedWord[item.key]] = item.value;
48
- } else if (allKeys.indexOf(item.key) >= 0) {
49
- mergedLocales[item.key] = item.value;
50
- }
51
- });
52
-
53
- mergedLocales = { ...chineseLocales, ...mergedLocales };
54
-
55
- const translateTasks = [];
56
-
57
- Object.keys(mergedLocales).forEach((k) => {
58
- const text = mergedLocales[k];
59
- let targetLang = key.split(/_|-/)[0];
60
- if (isChinese(text)) {
61
- if (disableAutoTranslate) {
62
- mergedLocales[k] = text;
63
- } else {
64
- translateTasks.push([k, targetLang]);
65
- }
66
- }
67
- });
68
-
69
- const total = translateTasks.length;
70
-
71
- // 控制并发数
72
- const concurrenceCount = 3;
73
-
74
- while (translateTasks.length > 0) {
75
- const tasks = translateTasks.splice(0, concurrenceCount);
76
-
77
- await Promise.all(
78
- tasks.map(([k, targetLang]) => {
79
- const text = mergedLocales[k];
80
-
81
- return translate(text, targetLang)
82
- .then((res) => {
83
- mergedLocales[k] = res.text;
84
- })
85
- .catch((err) => {
86
- log.warning(`[AutoTranslate]: ${err.message}`);
87
- mergedLocales[k] = text;
88
- });
89
- })
90
- );
91
-
92
- log.info(`[AutoTranslate]: ${total - translateTasks.length}/${total}`);
93
- }
94
-
95
- const updateConfRes = await confService.updateConf(
96
- confName,
97
- mergedLocales,
98
- chineseLocales,
99
- key
100
- );
101
- if (updateConfRes.code !== 0) {
102
- log.error(`update conf ${confName} fail, ${res.message}`);
103
- return [];
104
- }
105
-
106
- log.success(`updated conf ${confName}`);
107
-
108
- if (publish) {
109
- log.warning(`start to publish conf ${confName}`);
110
- const publishRes = await confService.publishConf(confName);
111
- if (publishRes.code === 0) {
112
- log.success(`published conf ${confName}`);
113
- } else {
114
- log.error(`publish conf ${confName} failed, ${publishRes.message}`);
115
- }
116
- } else {
117
- log.warning(`${confName} is updated but unpublished!`);
118
- }
119
-
120
- return {
121
- key,
122
- locales: mergedLocales,
123
- };
124
- }
125
-
126
- /**
127
- * 同步更新非中文配置
128
- * @param {object} chineseLocales 中文资源
129
- * @param {object} updatedTranslatedWord 在远端做过中文文案修改的 key/value
130
- * @param {object} option 参数
131
- * @param {boolean} publish 更新后是否立即发布配置
132
- */
133
- async function syncNonChineseLocalesConf(
134
- chineseLocales,
135
- updatedTranslatedWord,
136
- option,
137
- publish,
138
- confService
139
- ) {
140
- const { primaryLocale, supportedLocales } = option;
141
-
142
- const syncTasks = supportedLocales
143
- .filter((key) => key !== primaryLocale)
144
- .map((key) =>
145
- syncOneNonChineseLocalesConf(
146
- key,
147
- option,
148
- chineseLocales,
149
- updatedTranslatedWord,
150
- publish,
151
- confService
152
- )
153
- );
154
-
155
- const nonChineseLocales = await Promise.all(syncTasks);
156
-
157
- return nonChineseLocales;
158
- }
159
-
160
- /**
161
- * 收集源码中的中文字符
162
- * @param {object} option 命令行或者配置文件中传入的参数信息
163
- * @param {boolean} publish 同步后,是否立即发布新的配置
164
- */
165
- async function collectChineseWords(option, publish, confService) {
166
- const { entry, output, exclude } = option;
167
-
168
- if (!Array.isArray(entry) && typeof entry !== 'string') {
169
- log.error('entry must be a string or array');
170
- process.exit(2);
171
- }
172
-
173
- let zhData = {};
174
- const allTranslatedWord = {};
175
- const updatedTranslatedWord = {}; // 在远端做过中文文案修改的 key/value
176
- const keysInUse = [];
177
-
178
- const zhConfName = 'zh-CN';
179
-
180
- const res = await confService.getConf(zhConfName, 'zh-CN');
181
-
182
- if (res.code !== 0) {
183
- log.error(`failed to read apollo conf, ${res.message}`);
184
- process.exit(2);
185
- }
186
-
187
- res.data.forEach((item) => {
188
- zhData[item.key] = item.value;
189
- });
190
-
191
- Object.keys(zhData).forEach((k) => {
192
- const chineseValue = zhData[k];
193
- if (chineseValue.trim() !== k.replace(/\{context,\s*\S+\}$/, '').trim()) {
194
- updatedTranslatedWord[k] = chineseValue;
195
- } else {
196
- if (Array.isArray(allTranslatedWord[chineseValue])) {
197
- allTranslatedWord[chineseValue].push(k);
198
- } else {
199
- allTranslatedWord[chineseValue] = [k];
200
- }
201
- }
202
- });
203
-
204
- const outputs = output ? [].concat(output) : [];
205
- const targetFiles = [].concat(entry).reduce((prev, cur, index) => {
206
- const files = getSourceFiles({ entry: cur, exclude }).map((file) => ({
207
- filePath: file,
208
- currentEntry: cur,
209
- currentOutput: outputs[index],
210
- ext: path.extname(file),
211
- }));
212
- return prev.concat(files);
213
- }, []);
214
-
215
- targetFiles.forEach((codeFileInfo) => {
216
- transformZeroToDi18n(
217
- codeFileInfo,
218
- allTranslatedWord,
219
- updatedTranslatedWord,
220
- keysInUse,
221
- option
222
- );
223
- log.success(`done: ${codeFileInfo.filePath}`);
224
- });
225
-
226
- const localeResouce = {};
227
- const unusedKeys = [];
228
- Object.keys(allTranslatedWord).forEach((k) => {
229
- allTranslatedWord[k].forEach((key) => {
230
- if (keysInUse.includes(key)) {
231
- localeResouce[key] = k;
232
- } else {
233
- unusedKeys.push(key);
234
- }
235
- });
236
- });
237
-
238
- if (unusedKeys.length > 0) {
239
- log.info('the following keys are unused and will be removed:');
240
- log.info(unusedKeys);
241
- }
242
-
243
- const allTranslatedWordKeysCount = Object.keys(allTranslatedWord).reduce(
244
- (prev, cur) => prev + allTranslatedWord[cur].length,
245
- 0
246
- );
247
- const allLocalesKeyCount = Object.keys(localeResouce).length;
248
- const allTranstedInUseCount = allTranslatedWordKeysCount - unusedKeys.length;
249
-
250
- log.info(`keys in use count: ${allTranstedInUseCount}`);
251
- log.info(`locales in use count: ${allLocalesKeyCount}`);
252
-
253
- if (allTranstedInUseCount !== allLocalesKeyCount) {
254
- log.error('ATTENTION:keys and locales are not match');
255
- }
256
-
257
- log.info(`start to update conf ${zhConfName}`);
258
-
259
- // 更新配置
260
- const updateConfRes = await confService.updateConf(
261
- zhConfName,
262
- localeResouce,
263
- {},
264
- 'zh-CN'
265
- );
266
-
267
- if (updateConfRes.code !== 0) {
268
- log.error(`update conf ${zhConfName} failed, ${updateConfRes.message}`);
269
- return null;
270
- }
271
-
272
- log.success(`updated conf ${zhConfName}`);
273
-
274
- const unChineseLocales = await syncNonChineseLocalesConf(
275
- localeResouce,
276
- updatedTranslatedWord,
277
- option,
278
- publish,
279
- confService
280
- );
281
-
282
- // XXX: 英文 publish 也放到这里
283
- if (publish) {
284
- log.warning(`start to publish conf ${zhConfName}`);
285
- const publishRes = await confService.publishConf(zhConfName);
286
- if (publishRes.code === 0) {
287
- log.success(`published conf ${zhConfName}`);
288
- } else {
289
- log.error(`publish conf ${zhConfName} failed, ${publishRes.message}`);
290
- }
291
- } else {
292
- log.warning(`${zhConfName} is synced, but unpublihsed !!`);
293
- }
294
-
295
- return unChineseLocales.concat({
296
- key: 'zh-CN',
297
- locales: localeResouce,
298
- });
299
- }
300
-
301
- module.exports = function doCollectChineseWords(
302
- programOption,
303
- publish = false
304
- ) {
305
- const options = mergeOptions(programOption, ['entry', 'exclude', 'locales']);
306
-
307
- let confService = null;
308
- if (options.localeConf.type === 'file') {
309
- const FileConf = require('../conf/FileConf');
310
- confService = new FileConf(options.localeConf.folder);
311
- } else {
312
- const Conf = require(options.localeConf.path);
313
- confService = new Conf(options.localeConf);
314
- }
315
-
316
- if (!options.entry) {
317
- log.error('no entry is specified');
318
- process.exit(2);
319
- }
320
-
321
- if (Array.isArray(options.entry)) {
322
- if (!Array.isArray(options.output)) {
323
- log.error('output should be array too, since entry is array');
324
- process.exit(2);
325
- }
326
-
327
- if (options.output.length !== options.entry.length) {
328
- log.error('output length are not equal to entry length');
329
- process.exit(2);
330
- }
331
- }
332
-
333
- if (options.output) {
334
- [].concat(options.output).forEach((outputDir) => {
335
- if (!fs.existsSync(outputDir)) {
336
- fs.mkdirSync(outputDir);
337
- }
338
- });
339
- }
340
-
341
- return collectChineseWords(options, publish, confService);
342
- };
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const glob = require('glob');
4
+ const { transformZeroToDi18n } = require('../../../zti18n-core');
5
+ const mergeOptions = require('../utils/mergeOptions');
6
+ const isChinese = require('../utils/isChinese');
7
+ const log = require('../utils/log');
8
+
9
+ function getSourceFiles({ entry, exclude }) {
10
+ return glob.sync(`${entry}/**/*.{js,ts,tsx,jsx,vue}`, {
11
+ ignore: exclude || [],
12
+ });
13
+ }
14
+
15
+ /**
16
+ * 同步一个非中文语言的配置
17
+ */
18
+ async function syncOneNonChineseLocalesConf(
19
+ key,
20
+ option,
21
+ chineseLocales,
22
+ updatedTranslatedWord,
23
+ publish,
24
+ confService
25
+ ) {
26
+ const confName = key;
27
+ const { disableAutoTranslate, translator } = option;
28
+
29
+ const translate = translator
30
+ ? require(path.join(process.cwd(), translator))
31
+ : require('../translate/google');
32
+
33
+ log.warning(`start to update conf ${confName}`);
34
+
35
+ const res = await confService.getConf(confName, key);
36
+
37
+ if (res.code !== 0) {
38
+ log.error(`! failed to read apollo conf, ${res.message}`);
39
+ return null;
40
+ }
41
+
42
+ const allKeys = Object.keys(chineseLocales);
43
+ let mergedLocales = {};
44
+
45
+ res.data.forEach((item) => {
46
+ if (updatedTranslatedWord.hasOwnProperty(item.key)) {
47
+ mergedLocales[updatedTranslatedWord[item.key]] = item.value;
48
+ } else if (allKeys.indexOf(item.key) >= 0) {
49
+ mergedLocales[item.key] = item.value;
50
+ }
51
+ });
52
+
53
+ mergedLocales = { ...chineseLocales, ...mergedLocales };
54
+
55
+ const translateTasks = [];
56
+
57
+ Object.keys(mergedLocales).forEach((k) => {
58
+ const text = mergedLocales[k];
59
+ let targetLang = key.split(/_|-/)[0];
60
+ if (isChinese(text)) {
61
+ if (disableAutoTranslate) {
62
+ mergedLocales[k] = text;
63
+ } else {
64
+ translateTasks.push([k, targetLang]);
65
+ }
66
+ }
67
+ });
68
+
69
+ const total = translateTasks.length;
70
+
71
+ // 控制并发数
72
+ const concurrenceCount = 3;
73
+
74
+ while (translateTasks.length > 0) {
75
+ const tasks = translateTasks.splice(0, concurrenceCount);
76
+
77
+ await Promise.all(
78
+ tasks.map(([k, targetLang]) => {
79
+ const text = mergedLocales[k];
80
+
81
+ return translate(text, targetLang)
82
+ .then((res) => {
83
+ mergedLocales[k] = res.text;
84
+ })
85
+ .catch((err) => {
86
+ log.warning(`[AutoTranslate]: ${err.message}`);
87
+ mergedLocales[k] = text;
88
+ });
89
+ })
90
+ );
91
+
92
+ log.info(`[AutoTranslate]: ${total - translateTasks.length}/${total}`);
93
+ }
94
+
95
+ const updateConfRes = await confService.updateConf(
96
+ confName,
97
+ mergedLocales,
98
+ chineseLocales,
99
+ key
100
+ );
101
+ if (updateConfRes.code !== 0) {
102
+ log.error(`update conf ${confName} fail, ${res.message}`);
103
+ return [];
104
+ }
105
+
106
+ log.success(`updated conf ${confName}`);
107
+
108
+ if (publish) {
109
+ log.warning(`start to publish conf ${confName}`);
110
+ const publishRes = await confService.publishConf(confName);
111
+ if (publishRes.code === 0) {
112
+ log.success(`published conf ${confName}`);
113
+ } else {
114
+ log.error(`publish conf ${confName} failed, ${publishRes.message}`);
115
+ }
116
+ } else {
117
+ log.warning(`${confName} is updated but unpublished!`);
118
+ }
119
+
120
+ return {
121
+ key,
122
+ locales: mergedLocales,
123
+ };
124
+ }
125
+
126
+ /**
127
+ * 同步更新非中文配置
128
+ * @param {object} chineseLocales 中文资源
129
+ * @param {object} updatedTranslatedWord 在远端做过中文文案修改的 key/value
130
+ * @param {object} option 参数
131
+ * @param {boolean} publish 更新后是否立即发布配置
132
+ */
133
+ async function syncNonChineseLocalesConf(
134
+ chineseLocales,
135
+ updatedTranslatedWord,
136
+ option,
137
+ publish,
138
+ confService
139
+ ) {
140
+ const { primaryLocale, supportedLocales } = option;
141
+
142
+ const syncTasks = supportedLocales
143
+ .filter((key) => key !== primaryLocale)
144
+ .map((key) =>
145
+ syncOneNonChineseLocalesConf(
146
+ key,
147
+ option,
148
+ chineseLocales,
149
+ updatedTranslatedWord,
150
+ publish,
151
+ confService
152
+ )
153
+ );
154
+
155
+ const nonChineseLocales = await Promise.all(syncTasks);
156
+
157
+ return nonChineseLocales;
158
+ }
159
+
160
+ /**
161
+ * 收集源码中的中文字符
162
+ * @param {object} option 命令行或者配置文件中传入的参数信息
163
+ * @param {boolean} publish 同步后,是否立即发布新的配置
164
+ */
165
+ async function collectChineseWords(option, publish, confService) {
166
+ const { entry, output, exclude, entryNoImport = [] } = option;
167
+
168
+ if (!Array.isArray(entry) && typeof entry !== 'string') {
169
+ log.error('entry must be a string or array');
170
+ process.exit(2);
171
+ }
172
+
173
+ let zhData = {};
174
+ const allTranslatedWord = {};
175
+ const updatedTranslatedWord = {}; // 在远端做过中文文案修改的 key/value
176
+ const keysInUse = [];
177
+
178
+ const zhConfName = 'zh-CN';
179
+
180
+ const res = await confService.getConf(zhConfName, 'zh-CN');
181
+
182
+ if (res.code !== 0) {
183
+ log.error(`failed to read apollo conf, ${res.message}`);
184
+ process.exit(2);
185
+ }
186
+
187
+ res.data.forEach((item) => {
188
+ zhData[item.key] = item.value;
189
+ });
190
+
191
+ Object.keys(zhData).forEach((k) => {
192
+ const chineseValue = zhData[k];
193
+ if (chineseValue.trim() !== k.replace(/\{context,\s*\S+\}$/, '').trim()) {
194
+ updatedTranslatedWord[k] = chineseValue;
195
+ } else {
196
+ if (Array.isArray(allTranslatedWord[chineseValue])) {
197
+ allTranslatedWord[chineseValue].push(k);
198
+ } else {
199
+ allTranslatedWord[chineseValue] = [k];
200
+ }
201
+ }
202
+ });
203
+ const outputs = output ? [].concat(output) : [];
204
+ let targetFiles = [].concat(entry).reduce((prev, cur, index) => {
205
+ const files = getSourceFiles({ entry: cur, exclude }).map((file) => ({
206
+ filePath: file,
207
+ currentEntry: cur,
208
+ currentOutput: outputs[index],
209
+ ext: path.extname(file),
210
+ }));
211
+ return prev.concat(files);
212
+ }, []);
213
+ targetFiles = targetFiles.concat(
214
+ entryNoImport.reduce((prev, cur, index) => {
215
+ const files = getSourceFiles({ entry: cur, exclude: [] }).map((file) => ({
216
+ filePath: file,
217
+ currentEntry: cur,
218
+ ext: path.extname(file),
219
+ noImport: true,
220
+ }));
221
+ return prev.concat(files);
222
+ }, [])
223
+ );
224
+
225
+ targetFiles.forEach((codeFileInfo) => {
226
+ transformZeroToDi18n(
227
+ codeFileInfo,
228
+ allTranslatedWord,
229
+ updatedTranslatedWord,
230
+ keysInUse,
231
+ option
232
+ );
233
+ log.success(`done: ${codeFileInfo.filePath}`);
234
+ });
235
+
236
+ const localeResouce = {};
237
+ const unusedKeys = [];
238
+ Object.keys(allTranslatedWord).forEach((k) => {
239
+ allTranslatedWord[k].forEach((key) => {
240
+ if (keysInUse.includes(key)) {
241
+ localeResouce[key] = k;
242
+ } else {
243
+ unusedKeys.push(key);
244
+ }
245
+ });
246
+ });
247
+
248
+ if (unusedKeys.length > 0) {
249
+ log.info('the following keys are unused and will be removed:');
250
+ log.info(unusedKeys);
251
+ }
252
+
253
+ const allTranslatedWordKeysCount = Object.keys(allTranslatedWord).reduce(
254
+ (prev, cur) => prev + allTranslatedWord[cur].length,
255
+ 0
256
+ );
257
+ const allLocalesKeyCount = Object.keys(localeResouce).length;
258
+ const allTranstedInUseCount = allTranslatedWordKeysCount - unusedKeys.length;
259
+
260
+ log.info(`keys in use count: ${allTranstedInUseCount}`);
261
+ log.info(`locales in use count: ${allLocalesKeyCount}`);
262
+
263
+ if (allTranstedInUseCount !== allLocalesKeyCount) {
264
+ log.error('ATTENTION:keys and locales are not match');
265
+ }
266
+
267
+ log.info(`start to update conf ${zhConfName}`);
268
+ // 更新配置
269
+ const updateConfRes = await confService.updateConf(
270
+ zhConfName,
271
+ localeResouce,
272
+ {},
273
+ 'zh-CN'
274
+ );
275
+
276
+ if (updateConfRes.code !== 0) {
277
+ log.error(`update conf ${zhConfName} failed, ${updateConfRes.message}`);
278
+ return null;
279
+ }
280
+
281
+ log.success(`updated conf ${zhConfName}`);
282
+
283
+ const unChineseLocales = await syncNonChineseLocalesConf(
284
+ localeResouce,
285
+ updatedTranslatedWord,
286
+ option,
287
+ publish,
288
+ confService
289
+ );
290
+
291
+ // XXX: 英文 publish 也放到这里
292
+ if (publish) {
293
+ log.warning(`start to publish conf ${zhConfName}`);
294
+ const publishRes = await confService.publishConf(zhConfName);
295
+ if (publishRes.code === 0) {
296
+ log.success(`published conf ${zhConfName}`);
297
+ } else {
298
+ log.error(`publish conf ${zhConfName} failed, ${publishRes.message}`);
299
+ }
300
+ } else {
301
+ log.warning(`${zhConfName} is synced, but unpublihsed !!`);
302
+ }
303
+
304
+ return unChineseLocales.concat({
305
+ key: 'zh-CN',
306
+ locales: localeResouce,
307
+ });
308
+ }
309
+
310
+ module.exports = function doCollectChineseWords(
311
+ programOption,
312
+ publish = false
313
+ ) {
314
+ const options = mergeOptions(programOption, ['entry', 'exclude', 'locales']);
315
+
316
+ let confService = null;
317
+ if (options.localeConf.type === 'file') {
318
+ const FileConf = require('../conf/FileConf');
319
+ confService = new FileConf(options.localeConf.folder);
320
+ } else {
321
+ const Conf = require(options.localeConf.path);
322
+ confService = new Conf(options.localeConf);
323
+ }
324
+
325
+ if (!options.entry) {
326
+ log.error('no entry is specified');
327
+ process.exit(2);
328
+ }
329
+
330
+ if (Array.isArray(options.entry)) {
331
+ if (!Array.isArray(options.output)) {
332
+ log.error('output should be array too, since entry is array');
333
+ process.exit(2);
334
+ }
335
+
336
+ if (options.output.length !== options.entry.length) {
337
+ log.error('output length are not equal to entry length');
338
+ process.exit(2);
339
+ }
340
+ }
341
+
342
+ if (options.output) {
343
+ [].concat(options.output).forEach((outputDir) => {
344
+ if (!fs.existsSync(outputDir)) {
345
+ fs.mkdirSync(outputDir);
346
+ }
347
+ });
348
+ }
349
+
350
+ return collectChineseWords(options, publish, confService);
351
+ };