td-octopus 0.1.14 → 0.1.16
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 +1 -1
- package/src/check/index.js +7 -2
- package/src/utils/index.js +51 -1
package/package.json
CHANGED
package/src/check/index.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* @Author: 郑泳健
|
|
4
4
|
* @Date: 2024-12-12 15:00:24
|
|
5
5
|
* @LastEditors: 郑泳健
|
|
6
|
-
* @LastEditTime: 2025-09-
|
|
6
|
+
* @LastEditTime: 2025-09-11 09:56:54
|
|
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, autoImportJSFiles } = require('../utils/index')
|
|
16
16
|
const ora = require('ora');
|
|
17
17
|
|
|
18
18
|
const CONFIG = getProjectConfig();
|
|
@@ -102,6 +102,11 @@ function main() {
|
|
|
102
102
|
|
|
103
103
|
rewriteFiles(getFileKeyValueList(zhCNResult), 'zh-CN');
|
|
104
104
|
rewriteFiles(getFileKeyValueList(enCNResult), 'en-US');
|
|
105
|
+
const cnJSON = fs.readFileSync(path.resolve(process.cwd(), '.octopus/zh-CN/index.js'), 'utf-8');
|
|
106
|
+
const enJSON = fs.readFileSync(path.resolve(process.cwd(), '.octopus/en-US/index.js'), 'utf-8');
|
|
107
|
+
|
|
108
|
+
autoImportJSFiles(path.resolve(process.cwd(), '.octopus/zh-CN'), cnJSON)
|
|
109
|
+
autoImportJSFiles(path.resolve(process.cwd(), '.octopus/en-US'), enJSON)
|
|
105
110
|
spinner.succeed(`同步完成`);
|
|
106
111
|
})()
|
|
107
112
|
}
|
package/src/utils/index.js
CHANGED
|
@@ -270,6 +270,55 @@ function prettierFile(fileContent, proType) {
|
|
|
270
270
|
}
|
|
271
271
|
}
|
|
272
272
|
|
|
273
|
+
function autoImportJSFiles(__dirname, indexContent) {
|
|
274
|
+
// 1. 提取已有 import 的模块名
|
|
275
|
+
const importRegex = /import\s+([a-zA-Z0-9_$]+)\s+from\s+'\.\/([^']+)'/g;
|
|
276
|
+
const existingImports = new Map();
|
|
277
|
+
let match;
|
|
278
|
+
|
|
279
|
+
while ((match = importRegex.exec(indexContent)) !== null) {
|
|
280
|
+
existingImports.set(match[2], match[1]);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// 2. 获取目录下所有 .js 文件(排除 index.js)
|
|
284
|
+
const allFiles = fs.readdirSync(__dirname)
|
|
285
|
+
.filter(file => file.endsWith('.js') && file !== 'index.js')
|
|
286
|
+
.map(file => path.basename(file, '.js'));
|
|
287
|
+
|
|
288
|
+
// 3. 找出缺少的
|
|
289
|
+
const missing = allFiles.filter(file => !existingImports.has(file));
|
|
290
|
+
|
|
291
|
+
if (missing.length === 0) {
|
|
292
|
+
process.exit(0);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// 4. 构造 import 和 export 内容
|
|
296
|
+
let newImports = '';
|
|
297
|
+
let newExports = '';
|
|
298
|
+
|
|
299
|
+
missing.forEach((file) => {
|
|
300
|
+
newImports += `import ${file} from './${file}';\n`;
|
|
301
|
+
newExports += ` ${file},\n`;
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
// 5. 插入到 index.js
|
|
305
|
+
// 找到最后一个 import 的位置
|
|
306
|
+
const lastImportIndex = indexContent.lastIndexOf("import ");
|
|
307
|
+
const firstExportIndex = indexContent.indexOf("export default");
|
|
308
|
+
|
|
309
|
+
const updatedContent =
|
|
310
|
+
indexContent.slice(0, lastImportIndex) +
|
|
311
|
+
indexContent.slice(lastImportIndex, firstExportIndex) + newImports + '\n' +
|
|
312
|
+
indexContent.slice(firstExportIndex).replace(
|
|
313
|
+
/(export default Object\.assign\(\{\},\s*\{[\s\S]*?)(\}\);)/,
|
|
314
|
+
`$1\n${newExports}$2`
|
|
315
|
+
);
|
|
316
|
+
|
|
317
|
+
// 6. 写回 index.js
|
|
318
|
+
fs.writeFileSync(path.resolve(__dirname, 'index.js'), updatedContent, 'utf-8');
|
|
319
|
+
|
|
320
|
+
}
|
|
321
|
+
|
|
273
322
|
module.exports = {
|
|
274
323
|
getOtpDir,
|
|
275
324
|
getLangDir,
|
|
@@ -285,5 +334,6 @@ module.exports = {
|
|
|
285
334
|
lookForFiles,
|
|
286
335
|
translateKeyText,
|
|
287
336
|
spining,
|
|
288
|
-
prettierFile
|
|
337
|
+
prettierFile,
|
|
338
|
+
autoImportJSFiles
|
|
289
339
|
};
|