Skip to content

Commit

Permalink
fix: use preferred device in default mode
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonrybczak committed Jun 4, 2024
1 parent c500a51 commit c4017e1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
31 changes: 20 additions & 11 deletions packages/cli-platform-apple/src/commands/runCommand/createRun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,32 @@ const createRun =
return;
}

const devices = await listDevices(sdkNames);
let devices = await listDevices(sdkNames);

if (devices.length === 0) {
return logger.error(
`${platformReadableName} devices or simulators not detected. Install simulators via Xcode or connect a physical ${platformReadableName} device`,
);
}

const packageJson = getPackageJson(ctx.root);

const preferredDevice = cacheManager.get(
packageJson.name,
'lastUsedIOSDeviceId',
);

if (preferredDevice) {
const preferredDeviceIndex = devices.findIndex(
({udid}) => udid === preferredDevice,
);

if (preferredDeviceIndex > -1) {
const [device] = devices.splice(preferredDeviceIndex, 1);
devices.unshift(device);
}
}

const fallbackSimulator =
platformName === 'ios' ? getFallbackSimulator(args) : devices[0];

Expand All @@ -169,16 +187,7 @@ const createRun =
);
}

const packageJson = getPackageJson(ctx.root);
const preferredDevice = cacheManager.get(
packageJson.name,
'lastUsedIOSDeviceId',
);

const selectedDevice = await promptForDeviceSelection(
devices,
preferredDevice,
);
const selectedDevice = await promptForDeviceSelection(devices);

if (!selectedDevice) {
throw new CLIError(
Expand Down
15 changes: 1 addition & 14 deletions packages/cli-platform-apple/src/tools/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,12 @@ export async function promptForConfigurationSelection(

export async function promptForDeviceSelection(
devices: Device[],
lastUsedIOSDeviceId?: string,
): Promise<Device | undefined> {
const sortedDevices = [...devices];
const devicesIds = sortedDevices.map(({udid}) => udid);

if (lastUsedIOSDeviceId) {
const preferredDeviceIndex = devicesIds.indexOf(lastUsedIOSDeviceId);

if (preferredDeviceIndex > -1) {
const [preferredDevice] = sortedDevices.splice(preferredDeviceIndex, 1);
sortedDevices.unshift(preferredDevice);
}
}

const {device} = await prompt({
type: 'select',
name: 'device',
message: 'Select the device you want to use',
choices: sortedDevices
choices: devices
.filter(({type}) => type === 'device' || type === 'simulator')
.map((d) => {
const availability =
Expand Down

0 comments on commit c4017e1

Please sign in to comment.