Skip to content

Commit 8944aba

Browse files
feat(run-ios): allow passing UDID inside --device option (#2375)
* feat: allow passing UDID inside `--device` option * chore: explain device name condition
1 parent 039f684 commit 8944aba

File tree

4 files changed

+45
-17
lines changed

4 files changed

+45
-17
lines changed

packages/cli-platform-apple/src/commands/runCommand/createRun.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,36 @@ const createRun =
300300
);
301301
}
302302
} else if (args.device) {
303-
const physicalDevices = devices.filter(({type}) => type !== 'simulator');
304-
const device = matchingDevice(physicalDevices, args.device);
305-
if (device) {
303+
let device = matchingDevice(devices, args.device);
304+
305+
if (!device) {
306+
const deviceByUdid = devices.find((d) => d.udid === args.device);
307+
if (!deviceByUdid) {
308+
return logger.error(
309+
`Could not find a physical device with name or unique device identifier: "${chalk.bold(
310+
args.device,
311+
)}". ${printFoundDevices(devices, 'device')}`,
312+
);
313+
}
314+
315+
device = deviceByUdid;
316+
317+
if (deviceByUdid.type === 'simulator') {
318+
return logger.error(
319+
`The device with udid: "${chalk.bold(
320+
args.device,
321+
)}" is a simulator. If you want to run on a simulator, use the "--simulator" flag instead.`,
322+
);
323+
}
324+
}
325+
326+
if (device && device.type === 'simulator') {
327+
return logger.error(
328+
"`--device` flag is intended for physical devices. If you're trying to run on a simulator, use `--simulator` instead.",
329+
);
330+
}
331+
332+
if (device && device.type === 'device') {
306333
return runOnDevice(
307334
device,
308335
platformName,
Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import {logger} from '@react-native-community/cli-tools';
22
import chalk from 'chalk';
3-
import {Device} from '../../types';
3+
import {Device, DeviceType} from '../../types';
44

55
export function matchingDevice(
66
devices: Array<Device>,
77
deviceName: string | true | undefined,
88
) {
9+
// The condition specifically checks if the value is `true`, not just truthy to allow for `--device` flag without a value
910
if (deviceName === true) {
1011
const firstIOSDevice = devices.find((d) => d.type === 'device')!;
1112
if (firstIOSDevice) {
@@ -20,18 +21,10 @@ export function matchingDevice(
2021
return undefined;
2122
}
2223
}
23-
const deviceByName = devices.find(
24+
return devices.find(
2425
(device) =>
2526
device.name === deviceName || formattedDeviceName(device) === deviceName,
2627
);
27-
if (!deviceByName) {
28-
logger.error(
29-
`Could not find a device named: "${chalk.bold(
30-
String(deviceName),
31-
)}". ${printFoundDevices(devices)}`,
32-
);
33-
}
34-
return deviceByName;
3528
}
3629

3730
export function formattedDeviceName(simulator: Device) {
@@ -40,9 +33,15 @@ export function formattedDeviceName(simulator: Device) {
4033
: simulator.name;
4134
}
4235

43-
export function printFoundDevices(devices: Array<Device>) {
36+
export function printFoundDevices(devices: Array<Device>, type?: DeviceType) {
37+
let filteredDevice = [...devices];
38+
39+
if (type) {
40+
filteredDevice = filteredDevice.filter((device) => device.type === type);
41+
}
42+
4443
return [
4544
'Available devices:',
46-
...devices.map((device) => ` - ${device.name} (${device.udid})`),
45+
...filteredDevice.map(({name, udid}) => ` - ${name} (${udid})`),
4746
].join('\n');
4847
}

packages/cli-platform-apple/src/commands/runCommand/runOptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export const getRunOptions = ({platformName}: BuilderCommand) => {
4545
!isMac && {
4646
name: '--device [string]', // here we're intentionally using [] over <> to make passed value optional to allow users to run only on physical devices
4747
description:
48-
'Explicitly set the device to use by name. If the value is not provided,' +
48+
'Explicitly set the device to use by name or by unique device identifier . If the value is not provided,' +
4949
'the app will run on the first available physical device.',
5050
},
5151
...getBuildOptions({platformName}),

packages/cli-platform-apple/src/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ export interface Device {
1212
version?: string;
1313
sdk?: string;
1414
availabilityError?: string;
15-
type?: 'simulator' | 'device' | 'catalyst';
15+
type?: DeviceType;
1616
lastBootedAt?: string;
1717
}
1818

19+
export type DeviceType = 'simulator' | 'device' | 'catalyst';
20+
1921
export interface IosInfo {
2022
name: string;
2123
schemes?: string[];

0 commit comments

Comments
 (0)