adb-check-device.js 868 B

123456789101112131415161718192021222324252627282930
  1. #!/usr/bin/env node
  2. const { execSync } = require('child_process')
  3. const path = require('path')
  4. // 从配置文件读取 ADB 路径
  5. const config = require(path.join(__dirname, '..', '..', 'configs', 'config.js'))
  6. const projectRoot = path.resolve(__dirname, '..', '..')
  7. const adbPath = config.adbPath?.path
  8. ? path.resolve(projectRoot, config.adbPath.path)
  9. : path.join(projectRoot, 'lib', 'scrcpy-adb', 'adb.exe')
  10. const deviceIp = process.argv[2]
  11. const devicePort = process.argv[3] || '5555'
  12. if (!deviceIp) {
  13. process.exit(1)
  14. }
  15. const deviceId = `${deviceIp}:${devicePort}`
  16. const devicesCommand = `"${adbPath}" devices`
  17. const devicesOutput = execSync(devicesCommand, { encoding: 'utf-8', timeout: 1000 })
  18. const isConnected = devicesOutput.includes(deviceId) && devicesOutput.includes('device')
  19. if (isConnected) {
  20. process.exit(0)
  21. }
  22. else
  23. {
  24. process.exit(1)
  25. }