Skip to content

Commit

Permalink
Merge pull request #22 from emereum/emereum-devices-with-paths-ignore…
Browse files Browse the repository at this point in the history
…-incomplete

Ignore incomplete device info when retrieving devices with paths
  • Loading branch information
UrielCh authored Nov 7, 2024
2 parents 73e65e3 + d28f04d commit d010008
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
42 changes: 21 additions & 21 deletions src/adb/command/host/HostDevicesWithPathsCommand.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Command from '../../command';
import DeviceWithPath from '../../../models/DeviceWithPath';
import DeviceClient from '../../DeviceClient';
import { DeviceType } from '../../../models/Device';
import { isDeviceType } from '../../../models/Device';


export default class HostDevicesWithPathsCommand extends Command<DeviceWithPath[]> {
Expand All @@ -18,25 +18,25 @@ export default class HostDevicesWithPathsCommand extends Command<DeviceWithPath[
}

private _parseDevices(value: string): DeviceWithPath[] {
return value
.split('\n')
.filter((e) => e)
.map((line: string) => {
// eslint-disable-next-line prefer-const
let [id, type, path, product, model, device, transportId] = line.split(/\s+/);
model = model.replace('model:', '');
product = product.replace('product:', '');
transportId = transportId.replace('transport_id:', '');
return {
id,
type: type as DeviceType,
path,
product,
model,
device,
transportId,
getClient: () => new DeviceClient(this.connection.parent, id),
};
});
const regexp = /^([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+product:([^\s]+)\s+model:([^\s]+)\s+device:([^\s]+)\s+transport_id:([^\s]+)$/gm;
const devices: DeviceWithPath[] = [];
let match;
while ((match = regexp.exec(value)) !== null) {
const [, id, type, path, product, model, device, transportId] = match;
if (!isDeviceType(type)) {
continue;
}
devices.push({
id,
type,
path,
product,
model,
device,
transportId,
getClient: () => new DeviceClient(this.connection.parent, id),
})
}
return devices;
}
}
6 changes: 5 additions & 1 deletion src/models/Device.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { DeviceClient } from "..";

const deviceTypes = ['emulator', 'device', 'offline', 'unauthorized', 'recovery'] as const;
/**
* adb device starts
*/
export type DeviceType = 'emulator' | 'device' | 'offline' | 'unauthorized' | 'recovery';
export type DeviceType = typeof deviceTypes[number];
export function isDeviceType(value: string): value is DeviceType {
return deviceTypes.includes(value as DeviceType);
}

export default interface Device {
/**
Expand Down

0 comments on commit d010008

Please sign in to comment.