62 lines
1.7 KiB
TypeScript
62 lines
1.7 KiB
TypeScript
import { type BrowserWindow } from 'electron/main'
|
|
import { AdbServerNodeTcpConnector } from '@yume-chan/adb-server-node-tcp'
|
|
import type { AdbServerDevice, AdbServerTransport } from '@yume-chan/adb'
|
|
import { Adb, AdbServerClient } from '@yume-chan/adb'
|
|
import { DecodeUtf8Stream, WritableStream } from '@yume-chan/stream-extra'
|
|
|
|
let adb: Adb | undefined
|
|
|
|
export function AdbDaemon(_win: BrowserWindow | null, ipcMain: Electron.IpcMain) {
|
|
ipcMain.handle('adb', async () => {
|
|
await createConnection()
|
|
})
|
|
|
|
ipcMain.handle('adb:shell', async (_event, command: string) => {
|
|
if (!adb) {
|
|
return
|
|
}
|
|
|
|
const process = await adb.subprocess.shell(command)
|
|
let result: string | null = null
|
|
await process.stdout.pipeThrough(new DecodeUtf8Stream()).pipeTo(
|
|
new WritableStream({
|
|
write(chunk) {
|
|
result += chunk
|
|
}
|
|
})
|
|
)
|
|
return result
|
|
})
|
|
}
|
|
|
|
async function createConnection() {
|
|
const connector: AdbServerNodeTcpConnector = new AdbServerNodeTcpConnector({
|
|
host: 'localhost',
|
|
port: 5037
|
|
})
|
|
|
|
console.log('Connecting to ADB server...')
|
|
connector.connect()
|
|
|
|
const client: AdbServerClient = new AdbServerClient(connector)
|
|
|
|
const devices: AdbServerDevice[] = await client.getDevices()
|
|
if (devices.length === 0) {
|
|
console.log('No device found')
|
|
return
|
|
}
|
|
|
|
console.log('Devices found:', devices.map(device => device.serial).join(', '))
|
|
|
|
const device: AdbServerDevice | undefined = devices.find(device => device.serial === 'd')
|
|
|
|
if (!device) {
|
|
console.log('No device found')
|
|
return
|
|
}
|
|
|
|
console.log('Device found:', device.serial)
|
|
|
|
const transport: AdbServerTransport = await client.createTransport(device)
|
|
adb = new Adb(transport)
|
|
}
|