td-octopus 0.2.7 → 0.2.9

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "td-octopus",
3
- "version": "0.2.7",
3
+ "version": "0.2.9",
4
4
  "description": "I18N tool",
5
5
  "author": "Anthony Li",
6
6
  "bin": {
package/src/pop/index.js CHANGED
@@ -3,7 +3,7 @@
3
3
  * @Author: 郑泳健
4
4
  * @Date: 2024-12-12 15:00:24
5
5
  * @LastEditors: 郑泳健
6
- * @LastEditTime: 2026-02-12 10:40:03
6
+ * @LastEditTime: 2026-04-20 15:29:52
7
7
  */
8
8
  const path = require('path');
9
9
  const fs = require('fs');
@@ -11,7 +11,7 @@ const { parse } = require('@babel/parser');
11
11
  const generate = require('@babel/generator').default;
12
12
  const syncLang = require('../utils/syncLang');
13
13
  const { flatObject, rewriteFiles, getFileKeyValueList } = require('../utils/translate');
14
- const { autoImportJSFiles } = require('../utils/index');
14
+ const { autoImportJSFiles, getProjectConfig } = require('../utils/index');
15
15
 
16
16
  const { ESLint } = require('eslint');
17
17
 
@@ -58,6 +58,8 @@ function readJsFiles(folderPath) {
58
58
  // 同步不同的语言包
59
59
  function main() {
60
60
  (async () => {
61
+ const config = getProjectConfig();
62
+ const distLangs = config.distLangs || ['en-US'];
61
63
  const zhCN = syncLang('zh-CN');
62
64
  const zhCNFlat = flatObject(zhCN);
63
65
  const totalText = readJsFiles(path.resolve(process.cwd(), 'src'));
@@ -77,33 +79,31 @@ function main() {
77
79
  return;
78
80
  }
79
81
  spinner.start(`查询完毕,共计丢失${lostKey.length}个,开始同步开始`);
80
- const zhFromFlat = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../octopus/zh-CN.js'), 'utf-8'));
81
- const enFromFlat = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../octopus/en-US.js'), 'utf-8'));
82
-
83
- const enUS = syncLang('en-US');
84
- const enUSFlat = flatObject(enUS);
85
-
86
- const zhCNResult = lostKey.reduce((total, item) => {
87
- if (zhFromFlat[item.replace('I18N.', '')]) {
88
- total[item.replace('I18N.', '')] = zhFromFlat[item.replace('I18N.', '')];
89
- }
90
- return total;
91
- }, zhCNFlat);
92
-
93
- const enCNResult = lostKey.reduce((total, item) => {
94
- if (enFromFlat[item.replace('I18N.', '')]) {
95
- total[item.replace('I18N.', '')] = enFromFlat[item.replace('I18N.', '')];
96
- }
97
- return total;
98
- }, enUSFlat);
99
-
100
- rewriteFiles(getFileKeyValueList(zhCNResult), 'zh-CN');
101
- rewriteFiles(getFileKeyValueList(enCNResult), 'en-US');
102
- const cnJSON = fs.readFileSync(path.resolve(process.cwd(), '.octopus/zh-CN/index.js'), 'utf-8');
103
- const enJSON = fs.readFileSync(path.resolve(process.cwd(), '.octopus/en-US/index.js'), 'utf-8');
104
-
105
- autoImportJSFiles(path.resolve(process.cwd(), '.octopus/zh-CN'), cnJSON);
106
- autoImportJSFiles(path.resolve(process.cwd(), '.octopus/en-US'), enJSON);
82
+
83
+ // 所有需要同步的语言,包含 zh-CN distLangs
84
+ const allLangs = ['zh-CN', ...distLangs];
85
+ const langDataMap = { 'zh-CN': { data: zhCN, flat: zhCNFlat } };
86
+
87
+ // 遍历所有语言进行同步
88
+ for (const lang of allLangs) {
89
+ const langFromFlat = JSON.parse(fs.readFileSync(path.resolve(__dirname, `../octopus/${lang}.js`), 'utf-8'));
90
+ const { data: langData, flat: langFlat } = langDataMap[lang] || (langDataMap[lang] = {
91
+ data: syncLang(lang),
92
+ flat: flatObject(syncLang(lang))
93
+ });
94
+
95
+ const langResult = lostKey.reduce((total, item) => {
96
+ if (langFromFlat[item.replace('I18N.', '')]) {
97
+ total[item.replace('I18N.', '')] = langFromFlat[item.replace('I18N.', '')];
98
+ }
99
+ return total;
100
+ }, langFlat);
101
+
102
+ rewriteFiles(getFileKeyValueList(langResult), lang);
103
+ const langJSON = fs.readFileSync(path.resolve(process.cwd(), `.octopus/${lang}/index.js`), 'utf-8');
104
+ autoImportJSFiles(path.resolve(process.cwd(), `.octopus/${lang}`), langJSON);
105
+ }
106
+
107
107
  fs.rmSync(path.resolve(__dirname, '../octopus'), { recursive: true, force: true });
108
108
  spinner.succeed('同步完成,如果需要重新check,请先执行otp stash');
109
109
  })();
@@ -3,7 +3,7 @@
3
3
  * @Author: 郑泳健
4
4
  * @Date: 2024-12-12 15:00:24
5
5
  * @LastEditors: 郑泳健
6
- * @LastEditTime: 2024-12-12 15:00:33
6
+ * @LastEditTime: 2026-05-18 15:01:42
7
7
  */
8
8
  const path = require('path')
9
9
  const fs = require('fs')
@@ -12,7 +12,7 @@ const { getSpecifiedFiles, isFile } = require('../utils/file')
12
12
  const syncLang = require('../utils/syncLang')
13
13
  const { flatObject, rewriteFiles, getFileKeyValueList, getAdjustLangObjAndAddList } = require('../utils/translate');
14
14
  const { failInfo, highlightText } = require('../utils/colors');
15
- const { getProjectConfig } = require('../utils/index')
15
+ const { getProjectConfig, removeUnusedLangFiles } = require('../utils/index')
16
16
  const ora = require('ora');
17
17
 
18
18
  const CONFIG = getProjectConfig();
@@ -58,8 +58,10 @@ function main() {
58
58
  result[i] = zhCNFlat[i]
59
59
  }
60
60
  }
61
-
62
- rewriteFiles(getFileKeyValueList(result), 'zh-CN')
61
+
62
+ const fileKeyValueList = getFileKeyValueList(result);
63
+ removeUnusedLangFiles(fileKeyValueList, 'zh-CN');
64
+ rewriteFiles(fileKeyValueList, 'zh-CN')
63
65
 
64
66
  for (const lang of distLang) {
65
67
  const currentLangMap = syncLang(lang);
@@ -67,6 +69,7 @@ function main() {
67
69
  spinner.start(`正在清理${lang}下多余的key`)
68
70
  // 删除掉多余的key,增加新的key,同时提取没有翻译过的key的列表
69
71
  const { fileKeyValueList } = await getAdjustLangObjAndAddList({ lang, langObj: langFlat, zhCNObj: result, spinner });
72
+ removeUnusedLangFiles(fileKeyValueList, lang);
70
73
  spinner.succeed(`已完成清理${lang}下多余的key`)
71
74
  // 重写文件
72
75
  rewriteFiles(fileKeyValueList, lang);
@@ -3,29 +3,30 @@
3
3
  * @Author: 郑泳健
4
4
  * @Date: 2024-12-12 15:00:24
5
5
  * @LastEditors: 郑泳健
6
- * @LastEditTime: 2025-10-13 10:55:39
6
+ * @LastEditTime: 2026-04-20 15:34:33
7
7
  */
8
8
  const path = require('path');
9
9
  const fs = require('fs');
10
10
  const syncLang = require('../utils/syncLang');
11
11
  const { flatObject } = require('../utils/translate');
12
12
  const ora = require('ora');
13
-
13
+ const { getProjectConfig } = require('../utils/index');
14
14
  const spinner = ora('开始check');
15
15
 
16
16
  // 同步不同的语言包
17
17
  function main() {
18
18
  (async () => {
19
- const zhCN = syncLang('zh-CN');
20
- const zhCNFlat = flatObject(zhCN);
21
- const enUS = syncLang('en-US');
22
- const enUSFlat = flatObject(enUS);
19
+ const config = getProjectConfig();
20
+ const distLangs = config.distLangs || ['en-US'];
23
21
  const outpuDir = path.resolve(__dirname, '../octopus');
24
-
25
22
  fs.mkdirSync(outpuDir, { recursive: true });
23
+ const allLangs = ['zh-CN', ...distLangs];
26
24
  spinner.start('开始缓存');
27
- fs.writeFileSync(path.resolve(outpuDir, 'zh-CN.js'), JSON.stringify(zhCNFlat, null, 2), 'utf-8');
28
- fs.writeFileSync(path.resolve(outpuDir, 'en-US.js'), JSON.stringify(enUSFlat, null, 2), 'utf-8');
25
+ for (const lang of allLangs) {
26
+ const syncLangData = syncLang(lang);
27
+ const syncLangDataFlat = flatObject(syncLangData);
28
+ fs.writeFileSync(path.resolve(outpuDir, `${lang}.js`), JSON.stringify(syncLangDataFlat, null, 2), 'utf-8');
29
+ }
29
30
  spinner.succeed('缓存完成');
30
31
  })();
31
32
  }
@@ -319,6 +319,57 @@ function prettierFile(fileContent, proType) {
319
319
 
320
320
  }
321
321
 
322
+ /**
323
+ * 删除语言目录中不在 fileKeyValueList 的多余文件,并清理 index.js 中的引用
324
+ * @param {Array} fileKeyValueList - [{fileName, value}]
325
+ * @param {string} lang - 语言目录名,如 'zh-CN'
326
+ */
327
+ function removeUnusedLangFiles(fileKeyValueList, lang) {
328
+ const config = getProjectConfig();
329
+ const otpPath = path.resolve(process.cwd(), config.otpDir);
330
+ const langDir = path.join(otpPath, lang);
331
+ if (!fs.existsSync(langDir)) return;
332
+
333
+ const keepFileNames = new Set(fileKeyValueList.map(i => i.fileName));
334
+ const indexFiles = new Set(['index.js', 'index.jsx', 'index.ts', 'index.tsx']);
335
+ const filesToDelete = fs.readdirSync(langDir).filter(file => {
336
+ if (indexFiles.has(file)) return false;
337
+ return !keepFileNames.has(path.basename(file, path.extname(file)));
338
+ });
339
+
340
+ if (filesToDelete.length === 0) return;
341
+
342
+ filesToDelete.forEach(file => {
343
+ fs.unlinkSync(path.join(langDir, file));
344
+ });
345
+
346
+ const indexPath = path.join(langDir, 'index.js');
347
+ if (!fs.existsSync(indexPath)) return;
348
+
349
+ const indexContent = fs.readFileSync(indexPath, 'utf-8');
350
+ const deleteBaseNames = new Set(filesToDelete.map(file => path.basename(file, path.extname(file))));
351
+
352
+ // 检测 Object.assign 块内的缩进风格
353
+ const indentMatch = indexContent.match(/export default Object\.assign\(\{\},\s*\{[\r\n]+([ \t]+)\w/);
354
+ const indent = indentMatch ? indentMatch[1] : ' ';
355
+
356
+ // 解析现有 import,过滤掉要删除的
357
+ const importRegex = /^import (\w+) from '\.\/([^']+)';$/gm;
358
+ const remaining = [];
359
+ let match;
360
+ while ((match = importRegex.exec(indexContent)) !== null) {
361
+ if (!deleteBaseNames.has(match[1])) {
362
+ remaining.push({ name: match[1], from: match[2] });
363
+ }
364
+ }
365
+
366
+ if (remaining.length === 0) return;
367
+
368
+ const importLines = remaining.map(i => `import ${i.name} from './${i.from}';`).join('\n');
369
+ const exportKeys = remaining.map(i => `${indent}${i.name},`).join('\n');
370
+ fs.writeFileSync(indexPath, `${importLines}\n\nexport default Object.assign({}, {\n${exportKeys}\n});\n`, 'utf-8');
371
+ }
372
+
322
373
  module.exports = {
323
374
  getOtpDir,
324
375
  getLangDir,
@@ -335,5 +386,6 @@ module.exports = {
335
386
  translateKeyText,
336
387
  spining,
337
388
  prettierFile,
338
- autoImportJSFiles
389
+ autoImportJSFiles,
390
+ removeUnusedLangFiles
339
391
  };