td-octopus 0.2.8 → 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.8",
3
+ "version": "0.2.9",
4
4
  "description": "I18N tool",
5
5
  "author": "Anthony Li",
6
6
  "bin": {
@@ -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);
@@ -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
  };