/** * 在本目录启动 exe,若有报错则写入 启动报错.txt * 用法:node run.js 或双击 run.bat */ const path = require('path') const fs = require('fs') const { spawn } = require('child_process') const dir = __dirname const errorLogPath = path.join(dir, '启动报错.txt') const exeNames = ['AndroidRemoteController.exe', 'electron-react-vite-app.exe'] let exePath = null for (const name of exeNames) { const p = path.join(dir, name) if (fs.existsSync(p)) { exePath = p break } } if (!exePath) { const msg = `[${new Date().toISOString()}] 未找到 exe(${exeNames.join(' / ')}) ` fs.writeFileSync(errorLogPath, msg, 'utf8') console.error(msg.trim()) process.exit(1) } const chunks = [] function writeErrorLog(extra) { const header = `[${new Date().toISOString()}] 运行 ${path.basename(exePath)} 异常 ` const body = chunks.length ? Buffer.concat(chunks).toString('utf8') : '' const tail = extra ? ` ${extra}` : '' fs.writeFileSync(errorLogPath, header + body + tail, 'utf8') } const child = spawn(exePath, [], { cwd: dir, stdio: ['ignore', 'pipe', 'pipe'], windowsHide: false }) child.stdout.on('data', (data) => { process.stdout.write(data) }) child.stderr.on('data', (data) => { chunks.push(data) process.stderr.write(data) }) child.on('error', (err) => { writeErrorLog(`进程启动失败: ${err.message}`) console.error(err) process.exit(1) }) child.on('exit', (code, signal) => { if (code !== 0 && code != null) { writeErrorLog(`退出码: ${code}${signal ? ` 信号: ${signal}` : ''}`) } })