screenshot.js 3.7 KB

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