#!/usr/bin/env node /** * 同步服务端 shared 目录到客户端 * 用法: node sync-shared.js */ const fs = require('fs-extra'); const path = require('path'); // 路径配置 const SERVER_SHARED_PATH = path.resolve(__dirname, '../server/src/shared'); const CLIENT_SHARED_PATH = path.resolve(__dirname, 'assets/scripts/Shared'); /** * 同步目录 */ async function syncShared() { console.log('=========================================='); console.log('开始同步 Shared 协议目录'); console.log('=========================================='); console.log('源目录:', SERVER_SHARED_PATH); console.log('目标目录:', CLIENT_SHARED_PATH); console.log(''); try { // 检查源目录是否存在 if (!fs.existsSync(SERVER_SHARED_PATH)) { console.error('❌ 错误: 服务端 shared 目录不存在!'); console.error(' 路径:', SERVER_SHARED_PATH); console.error(' 请确保服务端项目在正确的位置'); process.exit(1); } // 确保目标目录存在 await fs.ensureDir(CLIENT_SHARED_PATH); // 复制目录 console.log('📦 正在复制文件...'); await fs.copy(SERVER_SHARED_PATH, CLIENT_SHARED_PATH, { overwrite: true, errorOnExist: false, filter: (src) => { // 过滤掉不需要的文件 const basename = path.basename(src); if (basename === 'node_modules' || basename === '.git') { return false; } return true; } }); console.log('✅ 同步完成!'); console.log(''); // 列出同步的文件 const files = await getFileList(CLIENT_SHARED_PATH); console.log(`共同步 ${files.length} 个文件:`); files.forEach(file => { const relativePath = path.relative(CLIENT_SHARED_PATH, file); console.log(' -', relativePath); }); console.log(''); console.log('=========================================='); } catch (error) { console.error('❌ 同步失败:', error.message); process.exit(1); } } /** * 递归获取目录下所有文件 */ async function getFileList(dir) { const files = []; async function walk(currentPath) { const items = await fs.readdir(currentPath); for (const item of items) { const itemPath = path.join(currentPath, item); const stat = await fs.stat(itemPath); if (stat.isDirectory()) { await walk(itemPath); } else { files.push(itemPath); } } } await walk(dir); return files; } // 执行同步 syncShared().catch(error => { console.error('未知错误:', error); process.exit(1); });