|
@@ -14,12 +14,18 @@
|
|
|
|
|
|
|
|
const fs = require('fs');
|
|
const fs = require('fs');
|
|
|
const path = require('path');
|
|
const path = require('path');
|
|
|
|
|
+const { execSync } = require('child_process');
|
|
|
|
|
+const readline = require('readline');
|
|
|
|
|
|
|
|
// 配置
|
|
// 配置
|
|
|
const CONFIG = {
|
|
const CONFIG = {
|
|
|
inputDir: path.join(__dirname, '../src/assets/i18n/messages'),
|
|
inputDir: path.join(__dirname, '../src/assets/i18n/messages'),
|
|
|
outputDir: path.join(__dirname, 'output/i18n'),
|
|
outputDir: path.join(__dirname, 'output/i18n'),
|
|
|
- files: ['zh.js', 'en.js']
|
|
|
|
|
|
|
+ files: ['zh.js', 'en.js'],
|
|
|
|
|
+ scpHost: '192.168.110.245',
|
|
|
|
|
+ scpUser: 'root',
|
|
|
|
|
+ scpZhPath: '/root/gydr/trans/zh_CN/zh.js',
|
|
|
|
|
+ scpEnPath: '/root/gydr/trans/en_US/en.js'
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -65,58 +71,127 @@ function ensureDirectoryExists(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);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 主函数
|
|
* 主函数
|
|
|
*/
|
|
*/
|
|
|
-function main() {
|
|
|
|
|
|
|
+async function main() {
|
|
|
console.log('🚀 开始提取多语言 JSON...\n');
|
|
console.log('🚀 开始提取多语言 JSON...\n');
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 确保输出目录存在
|
|
// 确保输出目录存在
|
|
|
ensureDirectoryExists(CONFIG.outputDir);
|
|
ensureDirectoryExists(CONFIG.outputDir);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
let successCount = 0;
|
|
let successCount = 0;
|
|
|
let failCount = 0;
|
|
let failCount = 0;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 处理每个文件
|
|
// 处理每个文件
|
|
|
CONFIG.files.forEach(filename => {
|
|
CONFIG.files.forEach(filename => {
|
|
|
try {
|
|
try {
|
|
|
const inputPath = path.join(CONFIG.inputDir, filename);
|
|
const inputPath = path.join(CONFIG.inputDir, filename);
|
|
|
const outputFilename = filename; // 保持 .js 扩展名
|
|
const outputFilename = filename; // 保持 .js 扩展名
|
|
|
const outputPath = path.join(CONFIG.outputDir, outputFilename);
|
|
const outputPath = path.join(CONFIG.outputDir, outputFilename);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
console.log(`📄 处理: ${filename}`);
|
|
console.log(`📄 处理: ${filename}`);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 检查输入文件是否存在
|
|
// 检查输入文件是否存在
|
|
|
if (!fs.existsSync(inputPath)) {
|
|
if (!fs.existsSync(inputPath)) {
|
|
|
console.warn(`⚠️ 跳过: 文件不存在 ${inputPath}\n`);
|
|
console.warn(`⚠️ 跳过: 文件不存在 ${inputPath}\n`);
|
|
|
failCount++;
|
|
failCount++;
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 提取 JSON
|
|
// 提取 JSON
|
|
|
const jsonObj = extractJsonFromJs(inputPath);
|
|
const jsonObj = extractJsonFromJs(inputPath);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 写入 JSON 文件(格式化输出,缩进2个空格)
|
|
// 写入 JSON 文件(格式化输出,缩进2个空格)
|
|
|
fs.writeFileSync(
|
|
fs.writeFileSync(
|
|
|
outputPath,
|
|
outputPath,
|
|
|
JSON.stringify(jsonObj, null, 2),
|
|
JSON.stringify(jsonObj, null, 2),
|
|
|
'utf-8'
|
|
'utf-8'
|
|
|
);
|
|
);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
console.log(`✅ 成功: ${outputFilename} (${Object.keys(jsonObj).length} 条翻译)`);
|
|
console.log(`✅ 成功: ${outputFilename} (${Object.keys(jsonObj).length} 条翻译)`);
|
|
|
console.log(` 输出: ${outputPath}\n`);
|
|
console.log(` 输出: ${outputPath}\n`);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
successCount++;
|
|
successCount++;
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
console.error(`❌ 失败: ${filename}\n`);
|
|
console.error(`❌ 失败: ${filename}\n`);
|
|
|
failCount++;
|
|
failCount++;
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 汇总
|
|
// 汇总
|
|
|
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
|
|
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
|
|
|
console.log(`✨ 完成! 成功: ${successCount}, 失败: ${failCount}`);
|
|
console.log(`✨ 完成! 成功: ${successCount}, 失败: ${failCount}`);
|
|
|
console.log(`📂 输出目录: ${CONFIG.outputDir}`);
|
|
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');
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 执行
|
|
// 执行
|