td-octopus 0.1.16 → 0.1.17

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.
@@ -0,0 +1,9 @@
1
+ const { main } = require('../src/storage');
2
+
3
+ exports.command = 'storage';
4
+
5
+ exports.describe = 'storage 将项目中用到的中英文存储到缓存中';
6
+
7
+ exports.handler = () => {
8
+ main()
9
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "td-octopus",
3
- "version": "0.1.16",
3
+ "version": "0.1.17",
4
4
  "description": "I18N tool",
5
5
  "author": "Anthony Li",
6
6
  "bin": {
@@ -3,29 +3,24 @@
3
3
  * @Author: 郑泳健
4
4
  * @Date: 2024-12-12 15:00:24
5
5
  * @LastEditors: 郑泳健
6
- * @LastEditTime: 2025-09-11 09:56:54
6
+ * @LastEditTime: 2025-10-13 11:40:42
7
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, autoImportJSFiles } = require('../utils/index')
8
+ const path = require('path');
9
+ const fs = require('fs');
10
+ const syncLang = require('../utils/syncLang');
11
+ const { flatObject, rewriteFiles, getFileKeyValueList } = require('../utils/translate');
12
+ const { autoImportJSFiles } = require('../utils/index');
16
13
  const ora = require('ora');
17
14
 
18
- const CONFIG = getProjectConfig();
19
-
20
15
  const spinner = ora('开始check');
21
16
 
22
17
  function extractThreeLevelI18NKeys(code) {
23
18
  // 匹配恰好三级的I18N键值(I18N.xxx.xxx.xxx)
24
19
  // 使用正则表达式确保前面不是点,后面也不是点或字母数字下划线
25
20
  const pattern = /(?<!\.)I18N\.[a-zA-Z0-9_]+\.[a-zA-Z0-9_]+\.[a-zA-Z0-9_]+(?![a-zA-Z0-9_\.])/g;
26
-
21
+
27
22
  const matches = code.match(pattern) || [];
28
-
23
+
29
24
  // 去重并返回
30
25
  return [...new Set(matches)];
31
26
  }
@@ -33,84 +28,79 @@ function extractThreeLevelI18NKeys(code) {
33
28
  // 递归读取文件夹中所有 .js 文件内容
34
29
  function readJsFiles(folderPath) {
35
30
  let jsContent = '';
36
-
31
+
37
32
  const files = fs.readdirSync(folderPath); // 读取文件夹内容
38
33
  for (const file of files) {
39
- const fullPath = path.join(folderPath, file);
40
- const stats = fs.statSync(fullPath); // 获取文件或文件夹状态
41
-
42
- if (stats.isDirectory()) {
43
- // 如果是文件夹,递归处理
44
- jsContent += readJsFiles(fullPath);
45
- } else if (path.extname(fullPath) === '.js') {
46
- // 如果是 .js 文件,读取文件内容
47
- const content = fs.readFileSync(fullPath, 'utf-8');
48
- jsContent += `\n/* File: ${fullPath} */\n${content}\n`;
49
- }
34
+ const fullPath = path.join(folderPath, file);
35
+ const stats = fs.statSync(fullPath); // 获取文件或文件夹状态
36
+
37
+ if (stats.isDirectory()) {
38
+ // 如果是文件夹,递归处理
39
+ jsContent += readJsFiles(fullPath);
40
+ } else if (path.extname(fullPath) === '.js') {
41
+ // 如果是 .js 文件,读取文件内容
42
+ const content = fs.readFileSync(fullPath, 'utf-8');
43
+ jsContent += `\n/* File: ${fullPath} */\n${content}\n`;
44
+ }
50
45
  }
51
-
46
+
52
47
  return jsContent;
53
- }
48
+ }
54
49
 
55
50
  // 同步不同的语言包
56
51
  function main() {
57
52
  (async () => {
58
- const distLang = Array.isArray(CONFIG.distLangs) ? CONFIG.distLangs : []
59
53
  const zhCN = syncLang('zh-CN');
60
54
  const zhCNFlat = flatObject(zhCN);
61
- // 当前项目所有的key
62
- const keys = Object.keys(zhCNFlat)
63
- const totalText = readJsFiles(path.resolve(process.cwd(), 'src'))
55
+ const totalText = readJsFiles(path.resolve(process.cwd(), 'src'));
64
56
  const totalTranslateList = extractThreeLevelI18NKeys(totalText);
65
- const zhCnKey = Object.keys(zhCNFlat)
66
- let result = {}
67
- const lostKey = totalTranslateList.filter(item => !zhCnKey.includes(item.replace('I18N.', '')))
68
- fs.writeFileSync(path.resolve(process.cwd(), 'lostI18N.js'), JSON.stringify(lostKey, null, 4), 'utf-8')
69
-
70
- if(!lostKey.length) {
71
- spinner.succeed(`查询完毕,无丢失`);
72
- return
57
+ const zhCnKey = Object.keys(zhCNFlat);
58
+ const lostKey = totalTranslateList.filter((item) => !zhCnKey.includes(item.replace('I18N.', '')));
59
+ fs.writeFileSync(path.resolve(process.cwd(), 'lostI18N.js'), JSON.stringify(lostKey, null, 4), 'utf-8');
60
+
61
+ if (!lostKey.length) {
62
+ spinner.succeed('查询完毕,无丢失');
63
+ return;
73
64
  }
74
-
75
- if (!fs.existsSync(path.resolve(process.cwd(), 'public/.octopus'))) {
65
+
66
+ if (!fs.existsSync(path.resolve(__dirname, '../octopus/zh-CN.js'))) {
76
67
  spinner.succeed(`查询完毕,共计丢失${lostKey.length}个,请在lostI18N.js中查看`);
77
- spinner.warn('请将原始项目的.octopus复制到当前项目public目录下,会帮你自助将缺失的key补充上去');
68
+ spinner.warn('请先执行otp storage');
78
69
  return;
79
70
  }
80
71
  spinner.start(`查询完毕,共计丢失${lostKey.length}个,开始同步开始`);
81
- const zhFrom = syncLang('zh-CN', path.resolve(process.cwd(), 'public/.octopus'))
82
- const enFrom = syncLang('en-US', path.resolve(process.cwd(), 'public/.octopus'))
83
- const zhFromFlat = flatObject(zhFrom);
84
- const enFromFlat = flatObject(enFrom);
72
+ const zhFromFlat = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../octopus/zh-CN.js'), 'utf-8'));
73
+ const enFromFlat = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../octopus/en-US.js'), 'utf-8'));
85
74
 
86
75
  const enUS = syncLang('en-US');
87
76
  const enUSFlat = flatObject(enUS);
88
77
 
89
78
  const zhCNResult = lostKey.reduce((total, item) => {
90
- if(zhFromFlat[item.replace('I18N.', '')]){
91
- total[item.replace('I18N.', '')] = zhFromFlat[item.replace('I18N.', '')]
79
+ if (zhFromFlat[item.replace('I18N.', '')]) {
80
+ total[item.replace('I18N.', '')] = zhFromFlat[item.replace('I18N.', '')];
92
81
  }
93
82
  return total;
94
- }, zhCNFlat)
95
-
83
+ }, zhCNFlat);
84
+
96
85
  const enCNResult = lostKey.reduce((total, item) => {
97
- if(enFromFlat[item.replace('I18N.', '')]){
98
- total[item.replace('I18N.', '')] = enFromFlat[item.replace('I18N.', '')]
86
+ if (enFromFlat[item.replace('I18N.', '')]) {
87
+ total[item.replace('I18N.', '')] = enFromFlat[item.replace('I18N.', '')];
99
88
  }
100
89
  return total;
101
- }, enUSFlat)
90
+ }, enUSFlat);
102
91
 
103
92
  rewriteFiles(getFileKeyValueList(zhCNResult), 'zh-CN');
104
93
  rewriteFiles(getFileKeyValueList(enCNResult), 'en-US');
105
94
  const cnJSON = fs.readFileSync(path.resolve(process.cwd(), '.octopus/zh-CN/index.js'), 'utf-8');
106
95
  const enJSON = fs.readFileSync(path.resolve(process.cwd(), '.octopus/en-US/index.js'), 'utf-8');
107
96
 
108
- autoImportJSFiles(path.resolve(process.cwd(), '.octopus/zh-CN'), cnJSON)
109
- autoImportJSFiles(path.resolve(process.cwd(), '.octopus/en-US'), enJSON)
110
- spinner.succeed(`同步完成`);
111
- })()
97
+ autoImportJSFiles(path.resolve(process.cwd(), '.octopus/zh-CN'), cnJSON);
98
+ autoImportJSFiles(path.resolve(process.cwd(), '.octopus/en-US'), enJSON);
99
+ fs.rmSync(path.resolve(__dirname, '../octopus'), { recursive: true, force: true });
100
+ spinner.succeed('同步完成,如果需要重新check,请先执行otp storage');
101
+ })();
112
102
  }
113
103
 
114
104
  module.exports = {
115
105
  main
116
- }
106
+ };
@@ -0,0 +1,35 @@
1
+ /*
2
+ * @Descripttion: 将项目中用到的中英文存储到缓存中
3
+ * @Author: 郑泳健
4
+ * @Date: 2024-12-12 15:00:24
5
+ * @LastEditors: 郑泳健
6
+ * @LastEditTime: 2025-10-13 10:55:39
7
+ */
8
+ const path = require('path');
9
+ const fs = require('fs');
10
+ const syncLang = require('../utils/syncLang');
11
+ const { flatObject } = require('../utils/translate');
12
+ const ora = require('ora');
13
+
14
+ const spinner = ora('开始check');
15
+
16
+ // 同步不同的语言包
17
+ function main() {
18
+ (async () => {
19
+ const zhCN = syncLang('zh-CN');
20
+ const zhCNFlat = flatObject(zhCN);
21
+ const enUS = syncLang('en-US');
22
+ const enUSFlat = flatObject(enUS);
23
+ const outpuDir = path.resolve(__dirname, '../octopus');
24
+
25
+ fs.mkdirSync(outpuDir, { recursive: true });
26
+ 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');
29
+ spinner.succeed('缓存完成');
30
+ })();
31
+ }
32
+
33
+ module.exports = {
34
+ main
35
+ };