screenshot.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. const { spawn } = require('child_process')
  2. const { execSync } = require('child_process')
  3. const path = require('path')
  4. const fs = require('fs')
  5. const config = require(path.join(__dirname, '..', '..', 'configs', 'config.js'))
  6. let projectRoot = path.resolve(__dirname, '..', '..')
  7. if (projectRoot.includes('app.asar') && !projectRoot.includes('app.asar.unpacked')) {
  8. projectRoot = projectRoot.replace('app.asar', 'app.asar.unpacked')
  9. }
  10. const adbPath = path.resolve(projectRoot, config.adbPath.path)
  11. const scrcpyDir = path.dirname(adbPath)
  12. const scrcpyPath = path.join(scrcpyDir, 'scrcpy.exe')
  13. const staticRoot = process.env.STATIC_ROOT ? path.resolve(process.env.STATIC_ROOT) : path.join(projectRoot, 'static')
  14. const pidFile = path.join(staticRoot, 'scrcpy-pid.json')
  15. const action = process.argv[2]
  16. const ipArg = process.argv[3]
  17. const portArg = process.argv[4]
  18. // 支持两种传参:1) ip, port 分开 2) ip:port 合并(无默认 port,只用传入值)
  19. let ip, port
  20. if (portArg !== undefined && portArg !== '') {
  21. ip = ipArg
  22. port = portArg
  23. } else if (ipArg && String(ipArg).includes(':')) {
  24. const parts = String(ipArg).split(':')
  25. ip = parts[0] || ''
  26. port = parts[1] || ''
  27. } else {
  28. ip = ipArg || ''
  29. port = portArg || ''
  30. }
  31. /** 终止指定 PID 的进程 */
  32. function killPidIfRunning(pid) {
  33. spawn('taskkill.exe', ['/F', '/PID', pid.toString()], {
  34. stdio: 'ignore',
  35. detached: true
  36. }).unref()
  37. }
  38. /** 从 tasklist 获取 scrcpy.exe 的 PID(用 bat start 启动后需轮询得到 pid) */
  39. function getScrcpyPid() {
  40. const output = execSync('tasklist /FI "IMAGENAME eq scrcpy.exe" /FO CSV', { encoding: 'utf-8' })
  41. const lines = output.split('\n').filter(line => line.includes('scrcpy.exe'))
  42. if (lines.length === 0) return null
  43. const pidMatch = lines[0].match(/"(\d+)"/)
  44. return pidMatch ? parseInt(pidMatch[1]) : null
  45. }
  46. /** 判断是否为 IP(或 IP:port),用于无线设备选择器 */
  47. function isDeviceIp(val) {
  48. return val && !/^\d+$/.test(val) && /[\d.]/.test(val)
  49. }
  50. /** 启动 scrcpy:先 adb connect(若有 IP),再用 scrcpy-noconsole.bat(start "" scrcpy.exe %*)无控制台启动 */
  51. function startScrcpy() {
  52. if (fs.existsSync(pidFile)) {
  53. const { pid } = JSON.parse(fs.readFileSync(pidFile, 'utf-8'))
  54. killPidIfRunning(pid)
  55. }
  56. let deviceSelector = ''
  57. if (ip && port && isDeviceIp(ip)) {
  58. deviceSelector = `${ip}:${port}`
  59. execSync(`"${adbPath}" connect ${deviceSelector}`, { encoding: 'utf-8', cwd: scrcpyDir })
  60. }
  61. const serial = deviceSelector
  62. const args = serial
  63. ? ['--pause-on-exit=if-error', '-s', serial]
  64. : ['-e', '--pause-on-exit=if-error']
  65. const argsStr = args.join(' ')
  66. const vbsPath = path.join(scrcpyDir, 'scrcpy-noconsole.vbs')
  67. execSync(`wscript "${vbsPath.replace(/"/g, '""')}" ${argsStr}`, { cwd: scrcpyDir })
  68. const sleep = (ms) => { const t = Date.now(); while (Date.now() - t < ms) {} }
  69. sleep(1500)
  70. const runningPid = getScrcpyPid()
  71. if (!runningPid) {
  72. console.log(JSON.stringify({ success: false, error: 'scrcpy did not appear in tasklist' }))
  73. process.exit(1)
  74. }
  75. fs.writeFileSync(pidFile, JSON.stringify({ pid: runningPid }), 'utf-8')
  76. console.log(JSON.stringify({ success: true, pid: runningPid }))
  77. process.exit(0)
  78. }
  79. /** 停止 scrcpy:有 pid 则 taskkill 并删文件,返回 action: 'stop';无 pid 文件返回 action: 'none',供前端区分是否继续执行 start */
  80. function stopScrcpy() {
  81. if (!fs.existsSync(pidFile)) {
  82. console.log(JSON.stringify({ success: true, action: 'none' }))
  83. process.exit(0)
  84. return
  85. }
  86. const pid = JSON.parse(fs.readFileSync(pidFile, 'utf-8')).pid
  87. killPidIfRunning(pid)
  88. fs.unlinkSync(pidFile)
  89. console.log(JSON.stringify({ success: true, action: 'stop' }))
  90. process.exit(0)
  91. }
  92. switch (action) {
  93. case 'start':
  94. startScrcpy()
  95. break
  96. case 'stop':
  97. stopScrcpy()
  98. break
  99. default:
  100. console.log(JSON.stringify({
  101. success: false,
  102. error: 'Usage: node screenshot.js [start|stop] [ip] [port]'
  103. }))
  104. process.exit(1)
  105. }