adb-interact.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env node
  2. const { execSync } = require('child_process')
  3. const path = require('path')
  4. const projectRoot = path.resolve(__dirname, '..', '..')
  5. const adbPath = path.join(projectRoot, 'exe', 'adb', 'adb.exe')
  6. const action = process.argv[2]
  7. if (!action) {
  8. process.exit(1)
  9. }
  10. switch (action) {
  11. case 'tap': {
  12. const coordX = process.argv[3]
  13. const coordY = process.argv[4]
  14. const deviceId = process.argv[5] || ''
  15. if (!coordX || !coordY) {
  16. process.exit(1)
  17. }
  18. const deviceFlag = deviceId && deviceId.includes(':') ? `-s ${deviceId} ` : ''
  19. const tapCommand = `"${adbPath}" ${deviceFlag}shell input tap ${coordX} ${coordY}`
  20. execSync(tapCommand, { encoding: 'utf-8', timeout: 1000 })
  21. process.exit(0)
  22. }
  23. case 'swipe': {
  24. const direction = process.argv[3]
  25. const distance = parseInt(process.argv[4])
  26. const duration = parseInt(process.argv[5])
  27. const deviceId = process.argv[6] || ''
  28. if (!direction || !distance || !duration) {
  29. process.exit(1)
  30. }
  31. const deviceFlag = deviceId && deviceId.includes(':') ? `-s ${deviceId} ` : ''
  32. const sizeCommand = `"${adbPath}" ${deviceFlag}shell wm size`
  33. const sizeOutput = execSync(sizeCommand, { encoding: 'utf-8', timeout: 1000 })
  34. const sizeMatch = sizeOutput.match(/(\d+)x(\d+)/)
  35. if (!sizeMatch) {
  36. process.exit(1)
  37. }
  38. const screenWidth = parseInt(sizeMatch[1])
  39. const screenHeight = parseInt(sizeMatch[2])
  40. const centerX = Math.floor(screenWidth / 2)
  41. const centerY = Math.floor(screenHeight / 2)
  42. let startX = centerX
  43. let startY = centerY
  44. let endX = centerX
  45. let endY = centerY
  46. switch (direction) {
  47. case 'up-down':
  48. startY = centerY - Math.floor(distance / 2)
  49. endY = centerY + Math.floor(distance / 2)
  50. break
  51. case 'down-up':
  52. startY = centerY + Math.floor(distance / 2)
  53. endY = centerY - Math.floor(distance / 2)
  54. break
  55. case 'left-right':
  56. startX = centerX - Math.floor(distance / 2)
  57. endX = centerX + Math.floor(distance / 2)
  58. break
  59. case 'right-left':
  60. startX = centerX + Math.floor(distance / 2)
  61. endX = centerX - Math.floor(distance / 2)
  62. break
  63. default:
  64. process.exit(1)
  65. }
  66. const swipeCommand = `"${adbPath}" ${deviceFlag}shell input swipe ${startX} ${startY} ${endX} ${endY} ${duration}`
  67. execSync(swipeCommand, { encoding: 'utf-8', timeout: 2000 })
  68. process.exit(0)
  69. }
  70. default:
  71. process.exit(1)
  72. }