adb-connect.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 deviceIp = process.argv[2]
  7. const devicePort = process.argv[3] || '5555'
  8. if (!deviceIp) {
  9. process.exit(1)
  10. }
  11. const disconnectCommand = `"${adbPath}" disconnect ${deviceIp}:${devicePort}`
  12. try {
  13. execSync(disconnectCommand, { encoding: 'utf-8', stdio: 'ignore', timeout: 100 })
  14. } catch (error) {
  15. // Ignore errors on disconnect, as device might not be connected
  16. }
  17. const connectCommand = `"${adbPath}" connect ${deviceIp}:${devicePort}`
  18. let connectSuccess = false
  19. try {
  20. const connectOutput = execSync(connectCommand, { encoding: 'utf-8', timeout: 500 })
  21. connectSuccess = connectOutput.trim().includes('connected') || connectOutput.trim().includes('already connected')
  22. } catch (error) {
  23. // Connection failed, execSync might throw if timeout or command fails
  24. }
  25. if (!connectSuccess) {
  26. process.exit(1)
  27. }
  28. // Verify device is actually available in device list
  29. let isDeviceAvailable = false
  30. try {
  31. const devicesCommand = `"${adbPath}" devices`
  32. const devicesOutput = execSync(devicesCommand, { encoding: 'utf-8' })
  33. const deviceAddress = `${deviceIp}:${devicePort}`
  34. isDeviceAvailable = devicesOutput.includes(deviceAddress) && devicesOutput.includes('device')
  35. } catch (error) {
  36. // Error checking devices
  37. }
  38. if (isDeviceAvailable) {
  39. process.exit(0)
  40. } else {
  41. process.exit(1)
  42. }