59 lines
1.3 KiB
JavaScript
Raw Normal View History

import { Adb } from '@devicefarmer/adbkit'
import adbPath from '@resources/core/adb.exe?asset&asarUnpack'
2023-10-07 13:22:40 +08:00
const util = require('node:util')
const exec = util.promisify(require('node:child_process').exec)
let client = null
window.addEventListener('beforeunload', () => {
if (client) {
client.kill()
}
})
const getDevices = async () => await client.listDevicesWithPaths()
const shell = async (id, command) => await client.getDevice(id).shell(command)
2023-10-07 13:22:40 +08:00
const rawShell = async command => exec(`${adbPath} ${command}`)
const kill = async (...params) => await client.kill(...params)
const connect = async (...params) => await client.connect(...params)
const disconnect = async (...params) => await client.disconnect(...params)
const watch = async (callback) => {
const tracker = await client.trackDevices()
tracker.on('add', (device) => {
2023-10-07 13:22:40 +08:00
callback('add', device)
})
tracker.on('remove', (device) => {
2023-10-07 13:22:40 +08:00
callback('remove', device)
})
tracker.on('end', (ret) => {
2023-10-07 13:22:40 +08:00
callback('end', ret)
})
tracker.on('error', (err) => {
2023-10-07 13:22:40 +08:00
callback('error', err)
})
const close = () => tracker.end()
return close
}
export default () => {
client = Adb.createClient({ bin: adbPath })
console.log('client', client)
return {
getDevices,
shell,
2023-10-07 13:22:40 +08:00
rawShell,
kill,
connect,
disconnect,
watch,
}
}