Skip to content

Commit 8068be3

Browse files
fix: correctly list ios devices and simulators (#1823)
* fix: correctly list ios devices and simulators * Update packages/cli-platform-ios/src/commands/runIOS/index.ts Co-authored-by: Szymon Rybczak <[email protected]> * Update packages/cli-platform-ios/src/tools/listIOSDevices.ts Co-authored-by: Szymon Rybczak <[email protected]> * fix no configuration passed to xcodebuild command --------- Co-authored-by: Szymon Rybczak <[email protected]>
1 parent 1e54606 commit 8068be3

14 files changed

+318
-584
lines changed

packages/cli-platform-ios/src/commands/buildIOS/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ import {Device} from '../../types';
1818
import {BuildFlags, buildProject} from './buildProject';
1919
import {getDestinationSimulator} from '../../tools/getDestinationSimulator';
2020
import {selectFromInteractiveMode} from '../../tools/selectFromInteractiveMode';
21-
import {getDevices} from '../../tools/getDevices';
2221
import {getProjectInfo} from '../../tools/getProjectInfo';
2322
import {checkIfConfigurationExists} from '../../tools/checkIfConfigurationExists';
2423
import {getConfigurationScheme} from '../../tools/getConfigurationScheme';
24+
import listIOSDevices from '../../tools/listIOSDevices';
2525

2626
export interface FlagsT extends BuildFlags {
2727
configuration?: string;
@@ -136,7 +136,7 @@ async function buildIOS(_: Array<string>, ctx: Config, args: FlagsT) {
136136
);
137137
}
138138

139-
const devices = getDevices();
139+
const devices = await listIOSDevices();
140140

141141
if (args.udid) {
142142
const device = devices.find((d) => d.udid === args.udid);

packages/cli-platform-ios/src/commands/runIOS/index.ts

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {logger, CLIError} from '@react-native-community/cli-tools';
1616
import {BuildFlags, buildProject} from '../buildIOS/buildProject';
1717
import {iosBuildOptions} from '../buildIOS';
1818
import {Device} from '../../types';
19-
import listIOSDevices from './listIOSDevices';
19+
import listIOSDevices from '../../tools/listIOSDevices';
2020
import {checkIfConfigurationExists} from '../../tools/checkIfConfigurationExists';
2121
import {getProjectInfo} from '../../tools/getProjectInfo';
2222
import {getConfigurationScheme} from '../../tools/getConfigurationScheme';
@@ -99,7 +99,7 @@ async function runIOS(_: Array<string>, ctx: Config, args: FlagsT) {
9999

100100
const modifiedArgs = {...args, scheme, mode};
101101

102-
args.mode = getConfigurationScheme(
102+
modifiedArgs.mode = getConfigurationScheme(
103103
{scheme: args.scheme, mode: args.mode},
104104
sourceDir,
105105
);
@@ -110,15 +110,15 @@ async function runIOS(_: Array<string>, ctx: Config, args: FlagsT) {
110110
} "${chalk.bold(xcodeProject.name)}"`,
111111
);
112112

113-
if (args.listDevices) {
114-
if (args.device || args.udid) {
113+
const availableDevices = await listIOSDevices();
114+
if (modifiedArgs.listDevices) {
115+
if (modifiedArgs.device || modifiedArgs.udid) {
115116
logger.warn(
116117
`Both ${
117-
args.device ? 'device' : 'udid'
118+
modifiedArgs.device ? 'device' : 'udid'
118119
} and "list-devices" parameters were passed to "run" command. We will list available devices and let you choose from one.`,
119120
);
120121
}
121-
const availableDevices = await listIOSDevices();
122122
const selectedDevice = await promptForDeviceSelection(availableDevices);
123123
if (!selectedDevice) {
124124
throw new CLIError(
@@ -132,10 +132,10 @@ async function runIOS(_: Array<string>, ctx: Config, args: FlagsT) {
132132
}
133133
}
134134

135-
const devices = await listIOSDevices();
136-
137-
if (!args.device && !args.udid && !args.simulator) {
138-
const bootedDevices = devices.filter(({type}) => type === 'device');
135+
if (!modifiedArgs.device && !modifiedArgs.udid && !modifiedArgs.simulator) {
136+
const bootedDevices = availableDevices.filter(
137+
({type, isAvailable}) => type === 'device' && isAvailable,
138+
);
139139

140140
const simulators = getSimulators();
141141
const bootedSimulators = Object.keys(simulators.devices)
@@ -162,29 +162,31 @@ async function runIOS(_: Array<string>, ctx: Config, args: FlagsT) {
162162
);
163163
}
164164

165-
if (args.device && args.udid) {
165+
if (modifiedArgs.device && modifiedArgs.udid) {
166166
return logger.error(
167167
'The `device` and `udid` options are mutually exclusive.',
168168
);
169169
}
170170

171-
if (args.udid) {
172-
const device = devices.find((d) => d.udid === args.udid);
171+
if (modifiedArgs.udid) {
172+
const device = availableDevices.find((d) => d.udid === modifiedArgs.udid);
173173
if (!device) {
174174
return logger.error(
175175
`Could not find a device with udid: "${chalk.bold(
176-
args.udid,
177-
)}". ${printFoundDevices(devices)}`,
176+
modifiedArgs.udid,
177+
)}". ${printFoundDevices(availableDevices)}`,
178178
);
179179
}
180180
if (device.type === 'simulator') {
181181
return runOnSimulator(xcodeProject, scheme, modifiedArgs);
182182
} else {
183183
return runOnDevice(device, scheme, xcodeProject, modifiedArgs);
184184
}
185-
} else if (args.device) {
186-
const physicalDevices = devices.filter((d) => d.type !== 'simulator');
187-
const device = matchingDevice(physicalDevices, args.device);
185+
} else if (modifiedArgs.device) {
186+
const physicalDevices = availableDevices.filter(
187+
({type}) => type !== 'simulator',
188+
);
189+
const device = matchingDevice(physicalDevices, modifiedArgs.device);
188190
if (device) {
189191
return runOnDevice(device, scheme, xcodeProject, modifiedArgs);
190192
}
@@ -590,5 +592,10 @@ export default {
590592
description:
591593
'Path relative to project root where pre-built .app binary lives.',
592594
},
595+
{
596+
name: '--list-devices',
597+
description:
598+
'List all available iOS devices and simulators and let you choose one to run the app. ',
599+
},
593600
],
594601
};

packages/cli-platform-ios/src/commands/runIOS/listIOSDevices.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

packages/cli-platform-ios/src/tools/__tests__/findMatchingSimulator.test.ts

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -888,43 +888,6 @@ describe('findMatchingSimulator', () => {
888888
).toEqual(null);
889889
});
890890

891-
it('should return AppleTV devices if in the list', () => {
892-
expect(
893-
findMatchingSimulator(
894-
{
895-
devices: {
896-
'com.apple.CoreSimulator.SimRuntime.tvOS-11-2': [
897-
{
898-
state: 'Booted',
899-
availability: '(available)',
900-
name: 'Apple TV',
901-
udid: '816C30EA-38EA-41AC-BFDA-96FB632D522E',
902-
},
903-
{
904-
state: 'Shutdown',
905-
availability: '(available)',
906-
name: 'Apple TV 4K',
907-
udid: 'BCBB7E4B-D872-4D61-BC61-7C9805551075',
908-
},
909-
{
910-
state: 'Shutdown',
911-
availability: '(available)',
912-
name: 'Apple TV 4K (at 1080p)',
913-
udid: '1DE12308-1C14-4F0F-991E-A3ADC41BDFFC',
914-
},
915-
],
916-
},
917-
},
918-
{simulator: 'Apple TV'},
919-
),
920-
).toEqual({
921-
udid: '816C30EA-38EA-41AC-BFDA-96FB632D522E',
922-
name: 'Apple TV',
923-
booted: true,
924-
version: 'tvOS 11.2',
925-
});
926-
});
927-
928891
it('should return a simulator by UDID', () => {
929892
expect(
930893
findMatchingSimulator(

0 commit comments

Comments
 (0)