Skip to content

Commit 850dccb

Browse files
committed
refactor: isolate platform inventory loading
1 parent cb452c0 commit 850dccb

2 files changed

Lines changed: 82 additions & 76 deletions

File tree

src/core/dispatch-resolve.ts

Lines changed: 2 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
resolveIosSimulatorDeviceSetPath,
1515
} from '../utils/device-isolation.ts';
1616
import type { CliFlags } from '../utils/cli-flags.ts';
17+
import { listLocalDeviceInventory, type DeviceInventoryRequest } from './platform-inventory.ts';
1718
type ResolveDeviceFlags = Pick<
1819
CliFlags,
1920
| 'platform'
@@ -28,15 +29,7 @@ type ResolveDeviceFlags = Pick<
2829
const resolveTargetDeviceCacheScope = new AsyncLocalStorage<Map<string, DeviceInfo>>();
2930
const deviceInventoryProviderScope = new AsyncLocalStorage<DeviceInventoryProvider>();
3031

31-
export type DeviceInventoryRequest = {
32-
platform?: PlatformSelector;
33-
target?: DeviceTarget;
34-
deviceName?: string;
35-
udid?: string;
36-
serial?: string;
37-
iosSimulatorSetPath?: string;
38-
androidSerialAllowlist?: string[];
39-
};
32+
export type { DeviceInventoryRequest };
4033

4134
export type DeviceInventoryProvider = (
4235
request: DeviceInventoryRequest,
@@ -219,16 +212,6 @@ export async function resolveTargetDevice(flags: ResolveDeviceFlags): Promise<De
219212
);
220213
}
221214

222-
function shouldUseHostMacFastPath(selector: {
223-
platform?: PlatformSelector;
224-
target?: DeviceTarget;
225-
}): boolean {
226-
return (
227-
selector.platform === 'macos' ||
228-
(selector.platform === 'apple' && selector.target === 'desktop')
229-
);
230-
}
231-
232215
export async function withResolveTargetDeviceCacheScope<T>(task: () => Promise<T>): Promise<T> {
233216
if (resolveTargetDeviceCacheScope.getStore()) return await task();
234217
return await resolveTargetDeviceCacheScope.run(new Map(), task);
@@ -266,63 +249,6 @@ async function readInjectedDeviceInventory(
266249
return devices.map((device) => ({ ...device }));
267250
}
268251

269-
async function listLocalDeviceInventory(request: DeviceInventoryRequest): Promise<DeviceInfo[]> {
270-
if (shouldUseHostMacFastPath(request)) {
271-
const { listMacosDevices } = await import('../platforms/macos/devices.ts');
272-
return await listMacosDevices();
273-
}
274-
275-
if (request.platform === 'linux') {
276-
const { listLinuxDevices } = await import('../platforms/linux/devices.ts');
277-
return await listLinuxDevices();
278-
}
279-
280-
if (request.platform === 'android') {
281-
const { listAndroidDevices } = await import('../platforms/android/devices.ts');
282-
return await listAndroidDevices({
283-
serialAllowlist: request.androidSerialAllowlist
284-
? new Set(request.androidSerialAllowlist)
285-
: undefined,
286-
});
287-
}
288-
289-
if (request.platform) {
290-
const { listAppleDevices } = await import('../platforms/ios/devices.ts');
291-
return await listAppleDevices({
292-
simulatorSetPath: request.iosSimulatorSetPath,
293-
udid: request.udid,
294-
});
295-
}
296-
297-
const devices: DeviceInfo[] = [];
298-
try {
299-
const { listAndroidDevices } = await import('../platforms/android/devices.ts');
300-
devices.push(
301-
...(await listAndroidDevices({
302-
serialAllowlist: request.androidSerialAllowlist
303-
? new Set(request.androidSerialAllowlist)
304-
: undefined,
305-
})),
306-
);
307-
} catch {}
308-
try {
309-
const { listAppleDevices } = await import('../platforms/ios/devices.ts');
310-
devices.push(
311-
...(await listAppleDevices({
312-
simulatorSetPath: request.iosSimulatorSetPath,
313-
udid: request.udid,
314-
})),
315-
);
316-
} catch {}
317-
// Linux local device is appended last so it does not displace
318-
// connected Android/Apple devices in implicit auto-selection.
319-
try {
320-
const { listLinuxDevices } = await import('../platforms/linux/devices.ts');
321-
devices.push(...(await listLinuxDevices()));
322-
} catch {}
323-
return devices;
324-
}
325-
326252
function isAppleResolutionSelector(selector: {
327253
platform?: PlatformSelector;
328254
target?: DeviceTarget;

src/core/platform-inventory.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import type { DeviceInfo, DeviceTarget, PlatformSelector } from '../utils/device.ts';
2+
3+
export type DeviceInventoryRequest = {
4+
platform?: PlatformSelector;
5+
target?: DeviceTarget;
6+
deviceName?: string;
7+
udid?: string;
8+
serial?: string;
9+
iosSimulatorSetPath?: string;
10+
androidSerialAllowlist?: string[];
11+
};
12+
13+
export async function listLocalDeviceInventory(
14+
request: DeviceInventoryRequest,
15+
): Promise<DeviceInfo[]> {
16+
if (shouldUseHostMacFastPath(request)) {
17+
const { listMacosDevices } = await import('../platforms/macos/devices.ts');
18+
return await listMacosDevices();
19+
}
20+
21+
if (request.platform === 'linux') {
22+
const { listLinuxDevices } = await import('../platforms/linux/devices.ts');
23+
return await listLinuxDevices();
24+
}
25+
26+
if (request.platform === 'android') {
27+
const { listAndroidDevices } = await import('../platforms/android/devices.ts');
28+
return await listAndroidDevices({
29+
serialAllowlist: request.androidSerialAllowlist
30+
? new Set(request.androidSerialAllowlist)
31+
: undefined,
32+
});
33+
}
34+
35+
if (request.platform) {
36+
const { listAppleDevices } = await import('../platforms/ios/devices.ts');
37+
return await listAppleDevices({
38+
simulatorSetPath: request.iosSimulatorSetPath,
39+
udid: request.udid,
40+
});
41+
}
42+
43+
const devices: DeviceInfo[] = [];
44+
try {
45+
const { listAndroidDevices } = await import('../platforms/android/devices.ts');
46+
devices.push(
47+
...(await listAndroidDevices({
48+
serialAllowlist: request.androidSerialAllowlist
49+
? new Set(request.androidSerialAllowlist)
50+
: undefined,
51+
})),
52+
);
53+
} catch {}
54+
try {
55+
const { listAppleDevices } = await import('../platforms/ios/devices.ts');
56+
devices.push(
57+
...(await listAppleDevices({
58+
simulatorSetPath: request.iosSimulatorSetPath,
59+
udid: request.udid,
60+
})),
61+
);
62+
} catch {}
63+
// Linux local device is appended last so it does not displace
64+
// connected Android/Apple devices in implicit auto-selection.
65+
try {
66+
const { listLinuxDevices } = await import('../platforms/linux/devices.ts');
67+
devices.push(...(await listLinuxDevices()));
68+
} catch {}
69+
return devices;
70+
}
71+
72+
function shouldUseHostMacFastPath(selector: {
73+
platform?: PlatformSelector;
74+
target?: DeviceTarget;
75+
}): boolean {
76+
return (
77+
selector.platform === 'macos' ||
78+
(selector.platform === 'apple' && selector.target === 'desktop')
79+
);
80+
}

0 commit comments

Comments
 (0)