Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use preferred device in default mode #2363

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading