adb-connect.js 1.2 KB

12345678910111213141516171819202122232425262728
  1. #!/usr/bin/env node
  2. const { execSync } = require('child_process')
  3. const path = require('path')
  4. const fs = require('fs')
  5. // 根目录 = config 文件所在目录的上级(config 在 <根>/configs/config.js)
  6. const configPath = process.env.STATIC_ROOT
  7. ? path.join(path.dirname(process.env.STATIC_ROOT), 'configs', 'config.js')
  8. : path.join(__dirname, '..', '..', 'configs', 'config.js')
  9. const PROJECT_ROOT = path.dirname(path.dirname(path.resolve(configPath)))
  10. const config = fs.existsSync(configPath) ? require(configPath) : {}
  11. const adbPath = config.adbPath?.path
  12. ? (path.isAbsolute(config.adbPath.path) ? config.adbPath.path : path.resolve(PROJECT_ROOT, config.adbPath.path))
  13. : path.join(PROJECT_ROOT, 'lib', 'scrcpy-adb', process.platform === 'win32' ? 'adb.exe' : 'adb')
  14. const deviceIp = '192.168.0.101'
  15. const devicePort = '5555'
  16. /** Run adb connect and return whether connected. */
  17. function connect() {
  18. const out = execSync(`"${adbPath}" connect ${deviceIp}:${devicePort}`, { encoding: 'utf-8' }).trim()
  19. return out.includes('connected') || out.includes('already connected')
  20. }
  21. const ok = connect()
  22. console.log(ok ? `${deviceIp}:${devicePort} Connected` : `${deviceIp}:${devicePort} Connect failed`)
  23. process.exit(ok ? 0 : 1)