Skip to content

Commit 3945227

Browse files
feat(ios): cache last used device (#2162)
* 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 <szymon.rybczak@gmail.com> --------- Co-authored-by: Michał Pierzchała <thymikee@gmail.com>
1 parent 2fc5965 commit 3945227

3 files changed

Lines changed: 45 additions & 6 deletions

File tree

packages/cli-platform-ios/src/commands/runIOS/index.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
getDefaultUserTerminal,
2020
startServerInNewWindow,
2121
findDevServerPort,
22+
cacheManager,
2223
} from '@react-native-community/cli-tools';
2324
import {buildProject} from '../buildIOS/buildProject';
2425
import {BuildFlags, buildOptions} from '../buildIOS/buildOptions';
@@ -28,7 +29,7 @@ import listIOSDevices from '../../tools/listIOSDevices';
2829
import {promptForDeviceSelection} from '../../tools/prompts';
2930
import getSimulators from '../../tools/getSimulators';
3031
import {getXcodeProjectAndDir} from '../buildIOS/getXcodeProjectAndDir';
31-
import resolvePods from '../../tools/pods';
32+
import resolvePods, {getPackageJson} from '../../tools/pods';
3233
import getArchitecture from '../../tools/getArchitecture';
3334
import findXcodeProject from '../../config/findXcodeProject';
3435

@@ -129,14 +130,34 @@ async function runIOS(_: Array<string>, ctx: Config, args: FlagsT) {
129130
} and "list-devices" parameters were passed to "run" command. We will list available devices and let you choose from one.`,
130131
);
131132
}
132-
const selectedDevice = await promptForDeviceSelection(availableDevices);
133+
134+
const packageJson = getPackageJson(ctx.root);
135+
const preferredDevice = cacheManager.get(
136+
packageJson.name,
137+
'lastUsedIOSDeviceId',
138+
);
139+
140+
const selectedDevice = await promptForDeviceSelection(
141+
availableDevices,
142+
preferredDevice,
143+
);
144+
133145
if (!selectedDevice) {
134146
throw new CLIError(
135147
`Failed to select device, please try to run app without ${
136148
args.listDevices ? 'list-devices' : 'interactive'
137149
} command.`,
138150
);
151+
} else {
152+
if (selectedDevice.udid !== preferredDevice) {
153+
cacheManager.set(
154+
packageJson.name,
155+
'lastUsedIOSDeviceId',
156+
selectedDevice.udid,
157+
);
158+
}
139159
}
160+
140161
if (selectedDevice.type === 'simulator') {
141162
return runOnSimulator(xcodeProject, mode, scheme, args, selectedDevice);
142163
} else {

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,27 @@ export async function promptForConfigurationSelection(
3535
}
3636

3737
export async function promptForDeviceSelection(
38-
availableDevices: Device[],
38+
devices: Device[],
39+
lastUsedIOSDeviceId?: string,
3940
): Promise<Device | undefined> {
41+
const sortedDevices = [...devices];
42+
const devicesIds = sortedDevices.map(({udid}) => udid);
43+
44+
if (lastUsedIOSDeviceId) {
45+
const preferredDeviceIndex = devicesIds.indexOf(lastUsedIOSDeviceId);
46+
47+
if (preferredDeviceIndex > -1) {
48+
const [preferredDevice] = sortedDevices.splice(preferredDeviceIndex, 1);
49+
sortedDevices.unshift(preferredDevice);
50+
}
51+
}
52+
4053
const {device} = await prompt({
4154
type: 'select',
4255
name: 'device',
4356
message: 'Select the device you want to use',
44-
choices: availableDevices
45-
.filter((d) => d.type === 'device' || d.type === 'simulator')
57+
choices: sortedDevices
58+
.filter(({type}) => type === 'device' || type === 'simulator')
4659
.map((d) => {
4760
const version = d.version
4861
? ` (${d.version.match(/^(\d+\.\d+)/)?.[1]})`

packages/cli-tools/src/cacheManager.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import appDirs from 'appdirsjs';
55
import chalk from 'chalk';
66
import logger from './logger';
77

8-
type CacheKey = 'eTag' | 'lastChecked' | 'latestVersion' | 'dependencies';
8+
type CacheKey =
9+
| 'eTag'
10+
| 'lastChecked'
11+
| 'latestVersion'
12+
| 'dependencies'
13+
| 'lastUsedIOSDeviceId';
914
type Cache = {[key in CacheKey]?: string};
1015

1116
function loadCache(name: string): Cache | undefined {

0 commit comments

Comments
 (0)