td-octopus 0.1.7 → 0.1.8
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/cmds/reduce.js +9 -0
- package/package.json +2 -2
- package/src/reduce/index.js +79 -0
package/cmds/reduce.js
ADDED
package/package.json
CHANGED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @Descripttion: 删除多余的key
|
|
3
|
+
* @Author: 郑泳健
|
|
4
|
+
* @Date: 2024-12-12 15:00:24
|
|
5
|
+
* @LastEditors: 郑泳健
|
|
6
|
+
* @LastEditTime: 2024-12-12 15:00:33
|
|
7
|
+
*/
|
|
8
|
+
const path = require('path')
|
|
9
|
+
const fs = require('fs')
|
|
10
|
+
const shell = require('shelljs');
|
|
11
|
+
const { getSpecifiedFiles, isFile } = require('../utils/file')
|
|
12
|
+
const syncLang = require('../utils/syncLang')
|
|
13
|
+
const { flatObject, rewriteFiles, getFileKeyValueList, getAdjustLangObjAndAddList } = require('../utils/translate');
|
|
14
|
+
const { failInfo, highlightText } = require('../utils/colors');
|
|
15
|
+
const { getProjectConfig } = require('../utils/index')
|
|
16
|
+
const ora = require('ora');
|
|
17
|
+
|
|
18
|
+
const CONFIG = getProjectConfig();
|
|
19
|
+
|
|
20
|
+
const spinner = ora('开始reduce');
|
|
21
|
+
|
|
22
|
+
// 递归读取文件夹中所有 .js 文件内容
|
|
23
|
+
function readJsFiles(folderPath) {
|
|
24
|
+
let jsContent = '';
|
|
25
|
+
|
|
26
|
+
const files = fs.readdirSync(folderPath); // 读取文件夹内容
|
|
27
|
+
for (const file of files) {
|
|
28
|
+
const fullPath = path.join(folderPath, file);
|
|
29
|
+
const stats = fs.statSync(fullPath); // 获取文件或文件夹状态
|
|
30
|
+
|
|
31
|
+
if (stats.isDirectory()) {
|
|
32
|
+
// 如果是文件夹,递归处理
|
|
33
|
+
jsContent += readJsFiles(fullPath);
|
|
34
|
+
} else if (path.extname(fullPath) === '.js') {
|
|
35
|
+
// 如果是 .js 文件,读取文件内容
|
|
36
|
+
const content = fs.readFileSync(fullPath, 'utf-8');
|
|
37
|
+
jsContent += `\n/* File: ${fullPath} */\n${content}\n`;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return jsContent;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// 同步不同的语言包
|
|
45
|
+
function main() {
|
|
46
|
+
(async () => {
|
|
47
|
+
const distLang = Array.isArray(CONFIG.distLangs) ? CONFIG.distLangs : []
|
|
48
|
+
|
|
49
|
+
// 删除中文下躲雨的key
|
|
50
|
+
const zhCN = syncLang('zh-CN');
|
|
51
|
+
const zhCNFlat = flatObject(zhCN);
|
|
52
|
+
// 当前项目所有的key
|
|
53
|
+
const keys = Object.keys(zhCNFlat)
|
|
54
|
+
const totalText = readJsFiles(path.resolve(process.cwd(), 'src'))
|
|
55
|
+
let result = {}
|
|
56
|
+
for(let i in zhCNFlat){
|
|
57
|
+
if(totalText.includes(i)){
|
|
58
|
+
result[i] = zhCNFlat[i]
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
rewriteFiles(getFileKeyValueList(result), 'zh-CN')
|
|
63
|
+
|
|
64
|
+
for (const lang of distLang) {
|
|
65
|
+
const currentLangMap = syncLang(lang);
|
|
66
|
+
const langFlat = flatObject(currentLangMap);
|
|
67
|
+
spinner.start(`正在清理${lang}下多余的key`)
|
|
68
|
+
// 删除掉多余的key,增加新的key,同时提取没有翻译过的key的列表
|
|
69
|
+
const { fileKeyValueList, addList } = await getAdjustLangObjAndAddList(lang, langFlat, result, '', spinner);
|
|
70
|
+
spinner.succeed(`已完成清理${lang}下多余的key`)
|
|
71
|
+
// 重写文件
|
|
72
|
+
rewriteFiles(fileKeyValueList, lang);
|
|
73
|
+
}
|
|
74
|
+
})()
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
module.exports = {
|
|
78
|
+
main
|
|
79
|
+
}
|