Skip to content

Commit

Permalink
feat(ios): cache last used device (#2162)
Browse files Browse the repository at this point in the history
* feat(ios): save last used device

* fix: do not save cache when same device was chosen

* fix: create iOS specific options

* Update packages/cli-platform-ios/src/tools/prompts.ts

Co-authored-by: Szymon Rybczak <[email protected]>

---------

Co-authored-by: Michał Pierzchała <[email protected]>
  • Loading branch information
szymonrybczak and thymikee authored Dec 19, 2023
1 parent 2fc5965 commit 3945227
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
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];
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

0 comments on commit 3945227

Please sign in to comment.