From 4e306d588f6c1a8b19cdf70fdb1d1703a1fb1320 Mon Sep 17 00:00:00 2001 From: Matthias Giger Date: Sun, 6 Oct 2024 11:53:08 +0200 Subject: [PATCH] fix(android): emulator instance required to select device release-npm --- script/android.ts | 4 ++-- script/prompt.ts | 21 +++++++++++++++++---- types.ts | 3 +-- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/script/android.ts b/script/android.ts index 3575e36..7b2fee7 100644 --- a/script/android.ts +++ b/script/android.ts @@ -37,8 +37,8 @@ export const android = async (inputs: RunInputs) => { typeof inputs.location === 'number' ) { runInputArguments += ` --mode=${inputs.mode === RunMode.debug ? 'debug' : 'release'}` - if (inputs.deviceId) { - runInputArguments += ` --deviceId=${inputs.deviceId}` + if (inputs.device) { + runInputArguments += ` --device=${inputs.device}` } if (inputs.emulator) { runInputArguments += ' --active-arch-only' // Speeds up build. diff --git a/script/prompt.ts b/script/prompt.ts index 021ae8f..4067c9b 100644 --- a/script/prompt.ts +++ b/script/prompt.ts @@ -107,6 +107,9 @@ const getAndroidEmulators = () => { (match) => match[0], ) + // Running emulator name functions as a device. + const emulatorNameToDevice: Record = {} + const runningEmulatorNames = runningEmulators .map((runningEmulator) => { let detailsOutput = '' @@ -124,6 +127,10 @@ const getAndroidEmulators = () => { const match = detailsOutput.match(avdNameRegex) const avdName = match ? match[1] : null + if (avdName) { + emulatorNameToDevice[avdName] = runningEmulator + } + return avdName }) .filter(Boolean) @@ -131,6 +138,7 @@ const getAndroidEmulators = () => { const allDevices = emulators.map((emulator) => ({ name: emulator, state: runningEmulatorNames.includes(emulator) ? 'Booted' : 'Shutdown', + device: emulatorNameToDevice[emulator], })) return allDevices @@ -197,7 +205,6 @@ export const prompt = async () => { ], }) - let deviceId: string | undefined let simulator: string | undefined let emulator: string | undefined let isEmulatorRunning = false @@ -273,6 +280,13 @@ export const prompt = async () => { }) ).emulator + // Device is required to select the emulator in the RN CLI. + emulators.forEach((currentEmulator) => { + if (currentEmulator.name === emulator) { + device = currentEmulator.device + } + }) + isEmulatorRunning = emulators.some( (currentEmulator) => currentEmulator.name === emulator && currentEmulator.state === 'Booted', @@ -293,7 +307,7 @@ export const prompt = async () => { log('No attached devices found', 'error') } - deviceId = ( + device = ( await prompts({ type: 'select', name: 'device', @@ -345,9 +359,8 @@ export const prompt = async () => { log(`Launching emulator ${emulator} in the background`) spawn('emulator', ['-avd', emulator], { shell: true, detached: true }) } - deviceId = emulator - android({ location, mode, deviceId, simulator, emulator }) + android({ location, mode, device, simulator, emulator }) } } diff --git a/types.ts b/types.ts index 58c0e39..f143e41 100644 --- a/types.ts +++ b/types.ts @@ -47,8 +47,7 @@ export enum RunMode { export type RunInputs = { location: RunLocation mode: RunMode - device?: string // iOS device. - deviceId?: string // Android device. + device?: string // iOS or Android device. simulator?: string emulator?: string }