screenshot.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. const projectRoot = path.resolve(__dirname, '..', '..')
  7. const adbPath = path.resolve(projectRoot, config.adbPath.path)
  8. const scrcpyDir = path.dirname(adbPath)
  9. const scrcpyPath = path.join(scrcpyDir, 'scrcpy.exe')
  10. const scrcpyVbs = path.join(scrcpyDir, 'scrcpy-noconsole.vbs')
  11. const stopBat = path.join(scrcpyDir, 'stop.bat')
  12. const pidFile = path.join(projectRoot, 'static', 'scrcpy-pid.json')
  13. const action = process.argv[2]
  14. const pidArg = process.argv[3]
  15. // 获取 scrcpy 进程 PID
  16. function getScrcpyPid() {
  17. const output = execSync(`tasklist /FI "IMAGENAME eq scrcpy.exe" /FO CSV`, { encoding: 'utf-8' })
  18. const lines = output.split('\n').filter(line => line.includes('scrcpy.exe'))
  19. if (lines.length === 0) {
  20. return null
  21. }
  22. const pidMatch = lines[0].match(/"(\d+)"/)
  23. if (!pidMatch) {
  24. return null
  25. }
  26. return parseInt(pidMatch[1])
  27. }
  28. // 检查并终止指定 PID 的进程
  29. function killPidIfRunning(pid) {
  30. spawn('taskkill.exe', ['/F', '/PID', pid.toString()], {
  31. stdio: 'ignore',
  32. detached: true
  33. }).unref()
  34. }
  35. // 启动 scrcpy
  36. function startScrcpy() {
  37. const targetPid = pidArg && /^\d+$/.test(pidArg) ? parseInt(pidArg) : 7788
  38. killPidIfRunning(targetPid)
  39. const vbsArgs = pidArg && !/^\d+$/.test(pidArg) ? ` "${pidArg}"` : ''
  40. execSync(`wscript.exe "${scrcpyVbs}"${vbsArgs}`, {
  41. cwd: scrcpyDir,
  42. stdio: 'ignore'
  43. })
  44. const sleep = (ms) => {
  45. const start = Date.now()
  46. while (Date.now() - start < ms) {}
  47. }
  48. sleep(2000)
  49. const runningPid = getScrcpyPid()
  50. if (!runningPid) {
  51. console.log(JSON.stringify({
  52. success: false,
  53. error: 'Failed to start scrcpy'
  54. }))
  55. process.exit(1)
  56. return
  57. }
  58. fs.writeFileSync(pidFile, JSON.stringify({ pid: runningPid }), 'utf-8')
  59. console.log(JSON.stringify({
  60. success: true,
  61. pid: runningPid
  62. }))
  63. process.exit(0)
  64. }
  65. // 停止 scrcpy
  66. function stopScrcpy() {
  67. const pid = JSON.parse(fs.readFileSync(pidFile, 'utf-8')).pid
  68. execSync(`"${stopBat}" ${pid}`, { stdio: 'ignore', cwd: scrcpyDir })
  69. fs.unlinkSync(pidFile)
  70. console.log(JSON.stringify({
  71. success: true
  72. }))
  73. process.exit(0)
  74. }
  75. switch (action) {
  76. case 'start':
  77. startScrcpy()
  78. break
  79. case 'stop':
  80. stopScrcpy()
  81. break
  82. default:
  83. console.log(JSON.stringify({
  84. success: false,
  85. error: 'Usage: node screenshot.js [start|stop] [pid|deviceIp]'
  86. }))
  87. process.exit(1)
  88. }