/** * 编译工具脚本为 JavaScript * 跨平台兼容: Windows/macOS/Linux */ import { execSync } from 'child_process'; import * as fs from 'fs'; import * as path from 'path'; const SCRIPTS_DIR = path.join(process.cwd(), 'scripts'); console.log('═══════════════════════════════════════════════════════════'); console.log(' 编译工具脚本'); console.log('═══════════════════════════════════════════════════════════\n'); // 确保主项目已编译 console.log('1. 编译主项目...'); try { execSync('npm run build', { stdio: 'inherit' }); console.log(' ✅ 主项目编译完成\n'); } catch (err) { console.error(' ❌ 主项目编译失败'); process.exit(1); } // 编译工具脚本 console.log('2. 编译工具脚本...'); const scriptFiles = [ 'service-find.ts', 'service-restart.ts', 'service-status.ts', 'service-logs.ts', 'service-list.ts' ]; for (const file of scriptFiles) { const srcPath = path.join(SCRIPTS_DIR, file); const outPath = path.join(SCRIPTS_DIR, file.replace('.ts', '.js')); if (!fs.existsSync(srcPath)) { console.log(` ⏭️ 跳过 ${file} (不存在)`); continue; } try { execSync(`npx tsx ${srcPath} --transpile-only`, { stdio: 'pipe', cwd: process.cwd() }); // 使用 esbuild 快速编译 execSync(`npx esbuild ${srcPath} --bundle --platform=node --outfile=${outPath}`, { stdio: 'inherit' }); console.log(` ✅ ${file} -> ${file.replace('.ts', '.js')}`); } catch (err) { console.log(` ⚠️ ${file} 编译警告`); } } console.log('\n3. 添加可执行权限 (Unix)...'); if (process.platform !== 'win32') { for (const file of scriptFiles) { const jsPath = path.join(SCRIPTS_DIR, file.replace('.ts', '.js')); if (fs.existsSync(jsPath)) { try { fs.chmodSync(jsPath, 0o755); } catch (err) { // 忽略权限错误 } } } console.log(' ✅ 完成'); } else { console.log(' ⏭️ 跳过 (Windows)'); } console.log('\n═══════════════════════════════════════════════════════════'); console.log(' 编译完成!'); console.log('═══════════════════════════════════════════════════════════\n'); console.log('使用方法:'); console.log(' node scripts/service-find.js nginx'); console.log(' node scripts/service-restart.js '); console.log(' node scripts/service-status.js '); console.log(' node scripts/service-logs.js 50'); console.log(' node scripts/service-list.js'); console.log('');