瀏覽代碼

feat: 增强多语言文件提取脚本,支持SCP上传和交互式配置

为多语言文件提取脚本添加了SCP远程上传功能和交互式配置系统,实现自动化部署流程。

- 添加SCP远程上传功能,支持将提取的JSON文件自动上传到远程服务器
- 实现交互式配置系统,允许用户自定义IP地址、用户名和文件路径
- 集成readline模块提供友好的命令行交互体验
- 新增npm脚本extract:i18n,简化脚本执行流程
- 添加child_process和readline模块依赖,支持异步SCP配置和上传

改动文件:
- scripts/extract-i18n-json.js - 添加SCP上传和交互式配置功能
- package.json - 添加extract:i18n脚本
dengdx 3 周之前
父節點
當前提交
f199c9eb6b
共有 2 個文件被更改,包括 88 次插入12 次删除
  1. 1 0
      package.json
  2. 87 12
      scripts/extract-i18n-json.js

+ 1 - 0
package.json

@@ -44,6 +44,7 @@
     "pkg": "node ./.build/h5_for_webserver.build.js",
     "h5:browser": "cross-env TARO_API_URL= TARO_MQTT_URL=/mqtt node ./.build/h5_for_production.js",
     "h5:electron": "cross-env TARO_API_URL=http://localhost:6001 TARO_MQTT_URL=ws://localhost:8083/mqtt node ./.build/h5_for_production.js",
+    "extract:i18n": "node scripts/extract-i18n-json.js",
     "clean:cache": "node ./.build/clean-cache.js"
   },
   "browserslist": [

+ 87 - 12
scripts/extract-i18n-json.js

@@ -14,12 +14,18 @@
 
 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']
+  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');
-  
+
   // 确保输出目录存在
   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');
+  }
 }
 
 // 执行