Skip to content

Commit

Permalink
fix(android): emulator instance required to select device
Browse files Browse the repository at this point in the history
release-npm
  • Loading branch information
tobua committed Oct 6, 2024
1 parent f5b6d10 commit 4e306d5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
4 changes: 2 additions & 2 deletions script/android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
21 changes: 17 additions & 4 deletions script/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ const getAndroidEmulators = () => {
(match) => match[0],
)

// Running emulator name functions as a device.
const emulatorNameToDevice: Record<string, string> = {}

const runningEmulatorNames = runningEmulators
.map((runningEmulator) => {
let detailsOutput = ''
Expand All @@ -124,13 +127,18 @@ const getAndroidEmulators = () => {
const match = detailsOutput.match(avdNameRegex)
const avdName = match ? match[1] : null

if (avdName) {
emulatorNameToDevice[avdName] = runningEmulator
}

return avdName
})
.filter(Boolean)

const allDevices = emulators.map((emulator) => ({
name: emulator,
state: runningEmulatorNames.includes(emulator) ? 'Booted' : 'Shutdown',
device: emulatorNameToDevice[emulator],
}))

return allDevices
Expand Down Expand Up @@ -197,7 +205,6 @@ export const prompt = async () => {
],
})

let deviceId: string | undefined
let simulator: string | undefined
let emulator: string | undefined
let isEmulatorRunning = false
Expand Down Expand Up @@ -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',
Expand All @@ -293,7 +307,7 @@ export const prompt = async () => {
log('No attached devices found', 'error')
}

deviceId = (
device = (
await prompts({
type: 'select',
name: 'device',
Expand Down Expand Up @@ -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 })
}
}

Expand Down
3 changes: 1 addition & 2 deletions types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 4e306d5

Please sign in to comment.