run-process.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env node
  2. /**
  3. * run-process.js
  4. * 接收两个参数:ip 数组 (JSON)、脚本名
  5. * 异步根据每个 ip 执行脚本;执行前先 adb connect 该设备,确保连接成功再跑流程。
  6. *
  7. * 调用示例:node run-process.js '["192.168.2.5","192.168.2.6"]' 'RedNoteAIThumbsUp'
  8. */
  9. const path = require('path')
  10. const fs = require('fs')
  11. const { execSync } = require('child_process')
  12. const projectRoot = path.resolve(path.join(__dirname, '..'))
  13. const config = require(path.join(projectRoot, 'configs', 'config.js'))
  14. const adbPath = config.adbPath?.path
  15. ? path.resolve(projectRoot, config.adbPath.path)
  16. : path.join(projectRoot, 'lib', 'scrcpy-adb', 'adb.exe')
  17. const ADB_PORT = 5555
  18. const ipListJson = process.argv[2]
  19. const scriptName = process.argv[3]
  20. if (!scriptName) {
  21. process.stderr.write('run-process: missing scriptName\n')
  22. process.exit(1)
  23. }
  24. const folderPath = path.resolve(path.join(__dirname, '..', 'static', 'process', scriptName))
  25. function writeLog(folderPath, message) {
  26. const logFile = path.join(folderPath, 'log.txt')
  27. if (!fs.existsSync(folderPath)) fs.mkdirSync(folderPath, { recursive: true })
  28. const ts = new Date().toISOString().replace('T', ' ').slice(0, 19)
  29. fs.appendFileSync(logFile, `[${ts}] ${message}\n`)
  30. }
  31. try {
  32. writeLog(folderPath, `[run-process] start scriptName=${scriptName} folderPath=${folderPath}`)
  33. } catch (e) {
  34. process.stderr.write(`run-process: writeLog failed ${e.message}\n`)
  35. }
  36. let ipList
  37. try {
  38. ipList = JSON.parse(ipListJson || '[]')
  39. } catch (e) {
  40. writeLog(folderPath, `[错误] 解析 IP 列表失败: ${e.message}`)
  41. process.exit(1)
  42. }
  43. let shouldStop = false
  44. let actions
  45. try {
  46. const { parseWorkflow } = require('./ef-compiler/ef-compiler.js')
  47. actions = parseWorkflow(JSON.parse(fs.readFileSync(path.join(folderPath, 'process.json'), 'utf8'))).actions
  48. } catch (e) {
  49. writeLog(folderPath, `[错误] 加载 process.json 失败: ${e.message}`)
  50. process.exit(1)
  51. }
  52. const { executeActionSequence } = require('./ef-compiler/ef-compiler.js')
  53. const resolution = { width: 1080, height: 1920 }
  54. /** 对指定 IP 执行 adb connect,确保设备在列表中后再跑流程 */
  55. function ensureDeviceConnected(ip, port) {
  56. const out = execSync(`"${adbPath}" connect ${ip}:${port}`, { encoding: 'utf-8' }).trim()
  57. return out.includes('connected') || out.includes('already connected')
  58. }
  59. /** 启动执行:遍历 ip 列表并异步执行脚本;任一台失败则停止全部并返回失败设备 IP */
  60. async function start() {
  61. if (!ipList || ipList.length === 0) {
  62. writeLog(folderPath, '[run-process] 无设备,退出')
  63. process.stdout.write(JSON.stringify({ success: true, results: [] }) + '\n')
  64. process.exit(0)
  65. }
  66. writeLog(folderPath, `[run-process] 开始执行 ${ipList.length} 台设备: ${ipList.join(', ')}`)
  67. let failedIp = null
  68. const runOne = async (ip) => {
  69. if (shouldStop) return { ip, success: false, stopped: true }
  70. const connected = ensureDeviceConnected(ip, ADB_PORT)
  71. if (!connected) {
  72. writeLog(folderPath, `[run-process] ${ip}:${ADB_PORT} 连接未就绪,跳过`)
  73. return { ip, success: false }
  74. }
  75. const result = await executeActionSequence(actions, `${ip}:${ADB_PORT}`, folderPath, resolution, 1000, null, () => shouldStop)
  76. if (!result.success) {
  77. if (!failedIp) { failedIp = ip; shouldStop = true }
  78. }
  79. return { ip, success: result.success }
  80. }
  81. const results = await Promise.all(ipList.map(ip => runOne(ip)))
  82. const output = failedIp
  83. ? { success: false, failedIp, results }
  84. : { success: true, results }
  85. process.stdout.write(JSON.stringify(output) + '\n')
  86. process.exit(failedIp ? 1 : 0)
  87. }
  88. /** 停止执行(SIGTERM/SIGINT 时调用) */
  89. function stop() {
  90. shouldStop = true
  91. }
  92. process.on('SIGTERM', () => { stop(); process.exit(130) })
  93. process.on('SIGINT', () => { stop(); process.exit(130) })
  94. start()