From 1a0483bcdb44bbdd9bad9114daef5f6b3b7d2e83 Mon Sep 17 00:00:00 2001 From: szymonrybczak Date: Tue, 7 Nov 2023 17:14:21 +0100 Subject: [PATCH 1/4] feat(ios): save last used device --- .../src/commands/runIOS/index.ts | 23 +++++++++++++++++-- .../cli-platform-ios/src/tools/prompts.ts | 19 ++++++++++++--- packages/cli-tools/src/cacheManager.ts | 7 +++++- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/packages/cli-platform-ios/src/commands/runIOS/index.ts b/packages/cli-platform-ios/src/commands/runIOS/index.ts index cff9905c7..c0b819003 100644 --- a/packages/cli-platform-ios/src/commands/runIOS/index.ts +++ b/packages/cli-platform-ios/src/commands/runIOS/index.ts @@ -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'; @@ -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'; @@ -129,14 +130,32 @@ async function runIOS(_: Array, 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, + 'lastUsedDeviceId', + ); + + 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 { + cacheManager.set( + packageJson.name, + 'lastUsedDeviceId', + selectedDevice.udid, + ); } + if (selectedDevice.type === 'simulator') { return runOnSimulator(xcodeProject, mode, scheme, args, selectedDevice); } else { diff --git a/packages/cli-platform-ios/src/tools/prompts.ts b/packages/cli-platform-ios/src/tools/prompts.ts index 0f69317a6..c23704c8f 100644 --- a/packages/cli-platform-ios/src/tools/prompts.ts +++ b/packages/cli-platform-ios/src/tools/prompts.ts @@ -35,14 +35,27 @@ export async function promptForConfigurationSelection( } export async function promptForDeviceSelection( - availableDevices: Device[], + devices: Device[], + lastUsedDeviceId?: string, ): Promise { + const sortedDevices = devices; + const devicesIds = sortedDevices.map(({udid}) => udid); + + if (lastUsedDeviceId) { + const preferredDeviceIndex = devicesIds.indexOf(lastUsedDeviceId); + + 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]})` diff --git a/packages/cli-tools/src/cacheManager.ts b/packages/cli-tools/src/cacheManager.ts index 06f21d25e..c1c0eea30 100644 --- a/packages/cli-tools/src/cacheManager.ts +++ b/packages/cli-tools/src/cacheManager.ts @@ -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' + | 'lastUsedDeviceId'; type Cache = {[key in CacheKey]?: string}; function loadCache(name: string): Cache | undefined { From 94e82cfad1079b809f538a6f7a5df2278c8387ed Mon Sep 17 00:00:00 2001 From: szymonrybczak Date: Tue, 7 Nov 2023 17:20:10 +0100 Subject: [PATCH 2/4] fix: do not save cache when same device was chosen --- .../cli-platform-ios/src/commands/runIOS/index.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/cli-platform-ios/src/commands/runIOS/index.ts b/packages/cli-platform-ios/src/commands/runIOS/index.ts index c0b819003..d02dd8796 100644 --- a/packages/cli-platform-ios/src/commands/runIOS/index.ts +++ b/packages/cli-platform-ios/src/commands/runIOS/index.ts @@ -149,11 +149,13 @@ async function runIOS(_: Array, ctx: Config, args: FlagsT) { } command.`, ); } else { - cacheManager.set( - packageJson.name, - 'lastUsedDeviceId', - selectedDevice.udid, - ); + if (selectedDevice.udid !== preferredDevice) { + cacheManager.set( + packageJson.name, + 'lastUsedDeviceId', + selectedDevice.udid, + ); + } } if (selectedDevice.type === 'simulator') { From fad9763b552854a8e003bd44f46a63c8211e5c51 Mon Sep 17 00:00:00 2001 From: szymonrybczak Date: Mon, 11 Dec 2023 15:50:26 +0100 Subject: [PATCH 3/4] fix: create iOS specific options --- packages/cli-platform-ios/src/commands/runIOS/index.ts | 4 ++-- packages/cli-platform-ios/src/tools/prompts.ts | 6 +++--- packages/cli-tools/src/cacheManager.ts | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/cli-platform-ios/src/commands/runIOS/index.ts b/packages/cli-platform-ios/src/commands/runIOS/index.ts index d02dd8796..bd31639b9 100644 --- a/packages/cli-platform-ios/src/commands/runIOS/index.ts +++ b/packages/cli-platform-ios/src/commands/runIOS/index.ts @@ -134,7 +134,7 @@ async function runIOS(_: Array, ctx: Config, args: FlagsT) { const packageJson = getPackageJson(ctx.root); const preferredDevice = cacheManager.get( packageJson.name, - 'lastUsedDeviceId', + 'lastUsedIOSDeviceId', ); const selectedDevice = await promptForDeviceSelection( @@ -152,7 +152,7 @@ async function runIOS(_: Array, ctx: Config, args: FlagsT) { if (selectedDevice.udid !== preferredDevice) { cacheManager.set( packageJson.name, - 'lastUsedDeviceId', + 'lastUsedIOSDeviceId', selectedDevice.udid, ); } diff --git a/packages/cli-platform-ios/src/tools/prompts.ts b/packages/cli-platform-ios/src/tools/prompts.ts index c23704c8f..25d327fe0 100644 --- a/packages/cli-platform-ios/src/tools/prompts.ts +++ b/packages/cli-platform-ios/src/tools/prompts.ts @@ -36,13 +36,13 @@ export async function promptForConfigurationSelection( export async function promptForDeviceSelection( devices: Device[], - lastUsedDeviceId?: string, + lastUsedIOSDeviceId?: string, ): Promise { const sortedDevices = devices; const devicesIds = sortedDevices.map(({udid}) => udid); - if (lastUsedDeviceId) { - const preferredDeviceIndex = devicesIds.indexOf(lastUsedDeviceId); + if (lastUsedIOSDeviceId) { + const preferredDeviceIndex = devicesIds.indexOf(lastUsedIOSDeviceId); if (preferredDeviceIndex > -1) { const [preferredDevice] = sortedDevices.splice(preferredDeviceIndex, 1); diff --git a/packages/cli-tools/src/cacheManager.ts b/packages/cli-tools/src/cacheManager.ts index c1c0eea30..227eb674a 100644 --- a/packages/cli-tools/src/cacheManager.ts +++ b/packages/cli-tools/src/cacheManager.ts @@ -10,7 +10,7 @@ type CacheKey = | 'lastChecked' | 'latestVersion' | 'dependencies' - | 'lastUsedDeviceId'; + | 'lastUsedIOSDeviceId'; type Cache = {[key in CacheKey]?: string}; function loadCache(name: string): Cache | undefined { From c1fe6aeb9fb9d63bed7e8c63f66e2827b7389510 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Mon, 11 Dec 2023 16:56:05 +0100 Subject: [PATCH 4/4] Update packages/cli-platform-ios/src/tools/prompts.ts Co-authored-by: Szymon Rybczak --- packages/cli-platform-ios/src/tools/prompts.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli-platform-ios/src/tools/prompts.ts b/packages/cli-platform-ios/src/tools/prompts.ts index 25d327fe0..2aa343b5c 100644 --- a/packages/cli-platform-ios/src/tools/prompts.ts +++ b/packages/cli-platform-ios/src/tools/prompts.ts @@ -38,7 +38,7 @@ export async function promptForDeviceSelection( devices: Device[], lastUsedIOSDeviceId?: string, ): Promise { - const sortedDevices = devices; + const sortedDevices = [...devices]; const devicesIds = sortedDevices.map(({udid}) => udid); if (lastUsedIOSDeviceId) {