Skip to content

Commit

Permalink
fix: correctly list ios devices and simulators (#1863)
Browse files Browse the repository at this point in the history
* fix: correctly list ios devices and simulators

* chore: fix docs and incorrect import

* chore: fix snapshot
  • Loading branch information
roryf authored Mar 12, 2023
1 parent 4cf0519 commit 8dcde34
Show file tree
Hide file tree
Showing 16 changed files with 366 additions and 599 deletions.
10 changes: 10 additions & 0 deletions __e2e__/__snapshots__/config.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ exports[`shows up current config without unnecessary output 1`] = `
"<<REPLACED>>"
]
},
{
"name": "build-ios",
"description": "builds your app on iOS simulator",
"examples": [
"<<REPLACED>>"
],
"options": [
"<<REPLACED>>"
]
},
{
"name": "log-android",
"description": "starts logkitty"
Expand Down
12 changes: 6 additions & 6 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,12 @@ Explicitly set `xcconfig` to use in build.

Location for iOS build artifacts. Corresponds to Xcode's `-derivedDataPath`.

#### `--list-devices`

> default: false
List all available iOS devices and simulators and let you choose one to run the app.

### `build-ios`

Usage:
Expand Down Expand Up @@ -571,12 +577,6 @@ Location for iOS build artifacts. Corresponds to Xcode's `-derivedDataPath`.

Installs passed binary instead of building a fresh one.

#### `--list-devices`

> default: false
List all available iOS devices and simulators and let you choose one to run the app.

### `start`

Usage:
Expand Down
4 changes: 2 additions & 2 deletions packages/cli-platform-ios/src/commands/buildIOS/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import {
import {Device} from '../../types';
import {BuildFlags, buildProject} from './buildProject';
import {getDestinationSimulator} from '../../tools/getDestinationSimulator';
import {getDevices} from '../../tools/getDevices';
import {getProjectInfo} from '../../tools/getProjectInfo';
import {checkIfConfigurationExists} from '../../tools/checkIfConfigurationExists';
import {getConfigurationScheme} from '../../tools/getConfigurationScheme';
import listIOSDevices from '../../tools/listIOSDevices';

export interface FlagsT extends BuildFlags {
configuration?: string;
Expand Down Expand Up @@ -116,7 +116,7 @@ function buildIOS(_: Array<string>, ctx: Config, args: FlagsT) {
);
}

const devices = getDevices();
const devices = listIOSDevices();

if (args.udid) {
const device = devices.find((d) => d.udid === args.udid);
Expand Down
25 changes: 17 additions & 8 deletions packages/cli-platform-ios/src/commands/runIOS/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import {logger, CLIError} from '@react-native-community/cli-tools';
import {BuildFlags, buildProject} from '../buildIOS/buildProject';
import {iosBuildOptions} from '../buildIOS';
import {Device} from '../../types';
import listIOSDevices, {promptForDeviceSelection} from './listIOSDevices';
import listIOSDevices from '../../tools/listIOSDevices';
import {checkIfConfigurationExists} from '../../tools/checkIfConfigurationExists';
import {getProjectInfo} from '../../tools/getProjectInfo';
import {getConfigurationScheme} from '../../tools/getConfigurationScheme';
import {promptForDeviceSelection} from '../../tools/prompts';

export interface FlagsT extends BuildFlags {
simulator?: string;
Expand Down Expand Up @@ -92,6 +93,8 @@ async function runIOS(_: Array<string>, ctx: Config, args: FlagsT) {
} "${chalk.bold(xcodeProject.name)}"`,
);

const availableDevices = listIOSDevices();

if (args.listDevices) {
if (args.device || args.udid) {
logger.warn(
Expand All @@ -100,7 +103,6 @@ async function runIOS(_: Array<string>, ctx: Config, args: FlagsT) {
} and "list-devices" parameters were passed to "run" command. We will list available devices and let you choose from one.`,
);
}
const availableDevices = await listIOSDevices();
const selectedDevice = await promptForDeviceSelection(availableDevices);
if (!selectedDevice) {
throw new CLIError(
Expand All @@ -114,10 +116,10 @@ async function runIOS(_: Array<string>, ctx: Config, args: FlagsT) {
}
}

const devices = await listIOSDevices();

if (!args.device && !args.udid && !args.simulator) {
const bootedDevices = devices.filter(({type}) => type === 'device');
const bootedDevices = availableDevices.filter(
({type, isAvailable}) => type === 'device' && isAvailable,
);

const simulators = getSimulators();
const bootedSimulators = Object.keys(simulators.devices)
Expand Down Expand Up @@ -151,12 +153,12 @@ async function runIOS(_: Array<string>, ctx: Config, args: FlagsT) {
}

if (args.udid) {
const device = devices.find((d) => d.udid === args.udid);
const device = availableDevices.find((d) => d.udid === args.udid);
if (!device) {
return logger.error(
`Could not find a device with udid: "${chalk.bold(
args.udid,
)}". ${printFoundDevices(devices)}`,
)}". ${printFoundDevices(availableDevices)}`,
);
}
if (device.type === 'simulator') {
Expand All @@ -165,7 +167,9 @@ async function runIOS(_: Array<string>, ctx: Config, args: FlagsT) {
return runOnDevice(device, scheme, xcodeProject, args);
}
} else if (args.device) {
const physicalDevices = devices.filter((d) => d.type !== 'simulator');
const physicalDevices = availableDevices.filter(
(d) => d.type !== 'simulator',
);
const device = matchingDevice(physicalDevices, args.device);
if (device) {
return runOnDevice(device, scheme, xcodeProject, args);
Expand Down Expand Up @@ -571,5 +575,10 @@ export default {
description:
'Path relative to project root where pre-built .app binary lives.',
},
{
name: '--list-devices',
description:
'List all available iOS devices and simulators and let you choose one to run the app. ',
},
],
};
46 changes: 0 additions & 46 deletions packages/cli-platform-ios/src/commands/runIOS/listIOSDevices.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -888,43 +888,6 @@ describe('findMatchingSimulator', () => {
).toEqual(null);
});

it('should return AppleTV devices if in the list', () => {
expect(
findMatchingSimulator(
{
devices: {
'com.apple.CoreSimulator.SimRuntime.tvOS-11-2': [
{
state: 'Booted',
availability: '(available)',
name: 'Apple TV',
udid: '816C30EA-38EA-41AC-BFDA-96FB632D522E',
},
{
state: 'Shutdown',
availability: '(available)',
name: 'Apple TV 4K',
udid: 'BCBB7E4B-D872-4D61-BC61-7C9805551075',
},
{
state: 'Shutdown',
availability: '(available)',
name: 'Apple TV 4K (at 1080p)',
udid: '1DE12308-1C14-4F0F-991E-A3ADC41BDFFC',
},
],
},
},
{simulator: 'Apple TV'},
),
).toEqual({
udid: '816C30EA-38EA-41AC-BFDA-96FB632D522E',
name: 'Apple TV',
booted: true,
version: 'tvOS 11.2',
});
});

it('should return a simulator by UDID', () => {
expect(
findMatchingSimulator(
Expand Down
Loading

0 comments on commit 8dcde34

Please sign in to comment.