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

feat(ios): cache last used device #2162

Merged
merged 4 commits into from
Dec 19, 2023
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
25 changes: 23 additions & 2 deletions packages/cli-platform-ios/src/commands/runIOS/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
getDefaultUserTerminal,
startServerInNewWindow,
findDevServerPort,
cacheManager,
} from '@react-native-community/cli-tools';
import {buildProject} from '../buildIOS/buildProject';
import {BuildFlags, buildOptions} from '../buildIOS/buildOptions';
Expand All @@ -28,7 +29,7 @@ import listIOSDevices from '../../tools/listIOSDevices';
import {promptForDeviceSelection} from '../../tools/prompts';
import getSimulators from '../../tools/getSimulators';
import {getXcodeProjectAndDir} from '../buildIOS/getXcodeProjectAndDir';
import resolvePods from '../../tools/pods';
import resolvePods, {getPackageJson} from '../../tools/pods';
import getArchitecture from '../../tools/getArchitecture';
import findXcodeProject from '../../config/findXcodeProject';

Expand Down Expand Up @@ -129,14 +130,34 @@ 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 selectedDevice = await promptForDeviceSelection(availableDevices);

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

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

if (!selectedDevice) {
throw new CLIError(
`Failed to select device, please try to run app without ${
args.listDevices ? 'list-devices' : 'interactive'
} command.`,
);
} else {
if (selectedDevice.udid !== preferredDevice) {
cacheManager.set(
packageJson.name,
'lastUsedIOSDeviceId',
selectedDevice.udid,
);
}
}

if (selectedDevice.type === 'simulator') {
return runOnSimulator(xcodeProject, mode, scheme, args, selectedDevice);
} else {
Expand Down
19 changes: 16 additions & 3 deletions packages/cli-platform-ios/src/tools/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,27 @@ export async function promptForConfigurationSelection(
}

export async function promptForDeviceSelection(
availableDevices: Device[],
devices: Device[],
lastUsedIOSDeviceId?: string,
): Promise<Device | undefined> {
const sortedDevices = [...devices];
Copy link
Contributor

@cipolleschi cipolleschi Dec 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are these devices sorted? I don't think they are... This line seems a no-op.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really sorted here, but I needed to create a value to do not mutate params.

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: availableDevices
.filter((d) => d.type === 'device' || d.type === 'simulator')
choices: sortedDevices
.filter(({type}) => type === 'device' || type === 'simulator')
.map((d) => {
const version = d.version
? ` (${d.version.match(/^(\d+\.\d+)/)?.[1]})`
Expand Down
7 changes: 6 additions & 1 deletion packages/cli-tools/src/cacheManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import appDirs from 'appdirsjs';
import chalk from 'chalk';
import logger from './logger';

type CacheKey = 'eTag' | 'lastChecked' | 'latestVersion' | 'dependencies';
type CacheKey =
| 'eTag'
| 'lastChecked'
| 'latestVersion'
| 'dependencies'
| 'lastUsedIOSDeviceId';
type Cache = {[key in CacheKey]?: string};

function loadCache(name: string): Cache | undefined {
Expand Down