- 将 electron 代码重构为基于服务的模块化架构,提升可维护性 - 将 `electron/games/manager.ts` 移至 `electron/services/game.ts` 并重构为 GameService - 新增多个服务模块:category, database, game-scanner, node, process, proxy-monitor, singbox, system, updater - 新增工具模块:command, format, id, ping, port, process-helper - 新增公共模块:store(基于 JSON 的持久化存储) - 新增文档:singbox-config.md 和 electron-structure.md 说明新架构 - 更新主入口文件 `electron/main/index.ts` 以初始化所有服务 - 更新预加载脚本 `electron/preload/index.ts` 暴露新的 API - 更新构建配置以支持新的目录结构 - 修复主入口文件路径配置
46 lines
1.5 KiB
TypeScript
46 lines
1.5 KiB
TypeScript
import { spawn } from 'child_process'
|
||
|
||
/**
|
||
* 执行系统命令并返回结果
|
||
*
|
||
* 封装了 child_process.spawn,支持超时控制和 Windows 窗口隐藏。
|
||
*
|
||
* @param command - 要执行的命令(如 'ping', 'ipconfig')
|
||
* @param args - 命令参数数组
|
||
* @param timeoutMs - 超时时间(毫秒),默认 15000ms
|
||
* @returns Promise<{ code: number, output: string }>
|
||
* - code: 进程退出码,超时或错误时为 -1
|
||
* - output: 标准输出和标准错误合并后的字符串
|
||
*/
|
||
export function runCommand(command: string, args: string[], timeoutMs: number = 15000): Promise<{ code: number, output: string }> {
|
||
return new Promise((resolve) => {
|
||
const p = spawn(command, args, { windowsHide: true })
|
||
const chunks: Buffer[] = []
|
||
const onData = (d: any) => chunks.push(Buffer.isBuffer(d) ? d : Buffer.from(String(d)))
|
||
|
||
p.stdout?.on('data', onData)
|
||
p.stderr?.on('data', onData)
|
||
|
||
let timedOut = false
|
||
const timer = setTimeout(() => {
|
||
timedOut = true
|
||
p.kill()
|
||
}, timeoutMs)
|
||
|
||
p.on('close', (code) => {
|
||
clearTimeout(timer)
|
||
const output = Buffer.concat(chunks).toString('utf8').trim()
|
||
if (timedOut) {
|
||
resolve({ code: -1, output: output || 'Command timeout' })
|
||
return
|
||
}
|
||
resolve({ code: typeof code === 'number' ? code : -1, output })
|
||
})
|
||
|
||
p.on('error', (err) => {
|
||
clearTimeout(timer)
|
||
resolve({ code: -1, output: String(err?.message || err) })
|
||
})
|
||
})
|
||
}
|