| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- /**
- * 多语言文件 JS 转 JSON 工具
- *
- * 功能:将 src/assets/i18n/messages/*.js 文件转换为纯 JSON 格式
- * 目的:移除 JavaScript 语法(export default 和结尾的 ;),方便用于其他用途
- *
- * 使用方法:
- * node scripts/extract-i18n-json.js
- *
- * 输出:
- * scripts/output/i18n/zh.json - 中文翻译(纯JSON格式)
- * scripts/output/i18n/en.json - 英文翻译(纯JSON格式)
- */
- const fs = require('fs');
- const path = require('path');
- const { execSync } = require('child_process');
- const readline = require('readline');
- // 配置
- const CONFIG = {
- inputDir: path.join(__dirname, '../src/assets/i18n/messages'),
- outputDir: path.join(__dirname, 'output/i18n'),
- files: ['zh.js', 'en.js'],
- scpHost: '192.168.110.245',
- scpUser: 'ccos',
- scpZhPath: '/home/ccos/dros/linux-arm64-unpacked/langs/zh_CN/zh.js',
- scpEnPath: '/home/ccos/dros/linux-arm64-unpacked/langs/en_US/en.js'
- };
- /**
- * 从 JS 文件提取 JSON 对象
- * @param {string} filePath - JS 文件路径
- * @returns {object} - 解析后的 JSON 对象
- */
- function extractJsonFromJs(filePath) {
- try {
- // 读取文件内容
- const content = fs.readFileSync(filePath, 'utf-8');
-
- // 移除 export default 语句和结尾的分号
- let jsonString = content
- .replace(/export\s+default\s+/g, '') // 移除 export default
- .trim(); // 去除首尾空白
-
- // 移除末尾的分号(如果存在)
- if (jsonString.endsWith(';')) {
- jsonString = jsonString.slice(0, -1);
- }
-
- // 使用 eval 解析对象(在受控环境中使用)
- // 注意:这里假设文件内容是可信的
- const jsonObj = eval('(' + jsonString + ')');
-
- return jsonObj;
- } catch (error) {
- console.error(`❌ 解析文件失败: ${filePath}`);
- console.error(error.message);
- throw error;
- }
- }
- /**
- * 确保目录存在
- * @param {string} dirPath - 目录路径
- */
- function ensureDirectoryExists(dirPath) {
- if (!fs.existsSync(dirPath)) {
- fs.mkdirSync(dirPath, { recursive: true });
- console.log(`📁 创建目录: ${dirPath}`);
- }
- }
- /**
- * 询问用户SCP配置
- * @returns {Promise<Object>} - SCP配置对象
- */
- function promptScpConfig() {
- return new Promise((resolve) => {
- const rl = readline.createInterface({
- input: process.stdin,
- output: process.stdout
- });
- const config = {
- host: CONFIG.scpHost,
- user: CONFIG.scpUser,
- zhPath: CONFIG.scpZhPath,
- enPath: CONFIG.scpEnPath
- };
- console.log('\n📤 SCP配置 (按Enter使用默认值):');
- rl.question(`IP地址 (默认: ${CONFIG.scpHost}): `, (host) => {
- config.host = host || CONFIG.scpHost;
- rl.question(`用户名 (默认: ${CONFIG.scpUser}): `, (user) => {
- config.user = user || CONFIG.scpUser;
- rl.question(`中文文件路径 (默认: ${CONFIG.scpZhPath}): `, (zhPath) => {
- config.zhPath = zhPath || CONFIG.scpZhPath;
- rl.question(`英文文件路径 (默认: ${CONFIG.scpEnPath}): `, (enPath) => {
- config.enPath = enPath || CONFIG.scpEnPath;
- rl.close();
- resolve(config);
- });
- });
- });
- });
- });
- }
- /**
- * 通过 SCP 将文件上传到远程服务器
- * @param {string} localPath - 本地文件路径
- * @param {string} remotePath - 远程文件路径
- * @param {Object} scpConfig - SCP配置
- */
- function scpToRemote(localPath, remotePath, scpConfig) {
- try {
- const command = `scp ${localPath} ${scpConfig.user}@${scpConfig.host}:${remotePath}`;
- execSync(command, { stdio: 'inherit' });
- console.log(`📤 SCP 成功: ${localPath} -> ${remotePath}`);
- } catch (error) {
- console.error(`❌ SCP 失败: ${localPath} -> ${remotePath}`);
- console.error(error.message);
- }
- }
- /**
- * 主函数
- */
- async function main() {
- console.log('🚀 开始提取多语言 JSON...\n');
- // 确保输出目录存在
- ensureDirectoryExists(CONFIG.outputDir);
- let successCount = 0;
- let failCount = 0;
- // 处理每个文件
- CONFIG.files.forEach(filename => {
- try {
- const inputPath = path.join(CONFIG.inputDir, filename);
- const outputFilename = filename; // 保持 .js 扩展名
- const outputPath = path.join(CONFIG.outputDir, outputFilename);
- console.log(`📄 处理: ${filename}`);
- // 检查输入文件是否存在
- if (!fs.existsSync(inputPath)) {
- console.warn(`⚠️ 跳过: 文件不存在 ${inputPath}\n`);
- failCount++;
- return;
- }
- // 提取 JSON
- const jsonObj = extractJsonFromJs(inputPath);
- // 写入 JSON 文件(格式化输出,缩进2个空格)
- fs.writeFileSync(
- outputPath,
- JSON.stringify(jsonObj, null, 2),
- 'utf-8'
- );
- console.log(`✅ 成功: ${outputFilename} (${Object.keys(jsonObj).length} 条翻译)`);
- console.log(` 输出: ${outputPath}\n`);
- successCount++;
- } catch (error) {
- console.error(`❌ 失败: ${filename}\n`);
- failCount++;
- }
- });
- // 汇总
- console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
- console.log(`✨ 完成! 成功: ${successCount}, 失败: ${failCount}`);
- console.log(`📂 输出目录: ${CONFIG.outputDir}`);
- // 通过 SCP 上传到远程服务器
- if (successCount > 0) {
- const scpConfig = await promptScpConfig();
- console.log('\n📤 开始上传到远程服务器...\n');
- const zhOutputPath = path.join(CONFIG.outputDir, 'zh.js');
- const enOutputPath = path.join(CONFIG.outputDir, 'en.js');
- scpToRemote(zhOutputPath, scpConfig.zhPath, scpConfig);
- scpToRemote(enOutputPath, scpConfig.enPath, scpConfig);
- console.log('\n📤 远程上传完成!\n');
- }
- }
- // 执行
- main();
|