1- import { CLIException , ERR_BAD_INPUT , ERR_TARGET_NOT_FOUND , RunException } from '../errors' ;
1+ import * as Debug from 'debug' ;
2+
3+ import { AVDException , CLIException , ERR_BAD_INPUT , ERR_NO_DEVICE , ERR_NO_TARGET , ERR_TARGET_NOT_FOUND , ERR_UNSUITABLE_API_INSTALLATION , RunException } from '../errors' ;
24import { getOptionValue , getOptionValues } from '../utils/cli' ;
35import { log } from '../utils/log' ;
46import { onBeforeExit } from '../utils/process' ;
@@ -9,6 +11,8 @@ import { getInstalledAVDs } from './utils/avd';
911import { installApkToDevice , selectDeviceByTarget , selectHardwareDevice , selectVirtualDevice } from './utils/run' ;
1012import { SDK , getSDK } from './utils/sdk' ;
1113
14+ const modulePrefix = 'native-run:android:run' ;
15+
1216export async function run ( args : ReadonlyArray < string > ) : Promise < void > {
1317 const sdk = await getSDK ( ) ;
1418 const apkPath = getOptionValue ( args , '--app' ) ;
@@ -72,6 +76,8 @@ export async function run(args: ReadonlyArray<string>): Promise<void> {
7276}
7377
7478export async function selectDevice ( sdk : SDK , args : ReadonlyArray < string > ) : Promise < Device > {
79+ const debug = Debug ( `${ modulePrefix } :${ selectDevice . name } ` ) ;
80+
7581 const devices = await getDevices ( sdk ) ;
7682 const avds = await getInstalledAVDs ( sdk ) ;
7783
@@ -93,8 +99,26 @@ export async function selectDevice(sdk: SDK, args: ReadonlyArray<string>): Promi
9399
94100 if ( selectedDevice ) {
95101 return selectedDevice ;
102+ } else if ( args . includes ( '--device' ) ) {
103+ throw new RunException ( `No hardware devices found. Not attempting emulator because --device was specified.` , ERR_NO_DEVICE ) ;
104+ } else {
105+ log ( 'No hardare devices found, attempting emulator...\n' ) ;
106+ }
107+ }
108+
109+ try {
110+ return await selectVirtualDevice ( sdk , devices , avds ) ;
111+ } catch ( e ) {
112+ if ( ! ( e instanceof AVDException ) ) {
113+ throw e ;
114+ }
115+
116+ debug ( 'Issue with AVDs: %s' , e . message ) ;
117+
118+ if ( e . code === ERR_UNSUITABLE_API_INSTALLATION ) {
119+ throw new RunException ( 'No targets available. Cannot create AVD because there is no suitable API installation. Use --sdk-info to reveal missing packages and other issues.' , ERR_NO_TARGET ) ;
96120 }
97121 }
98122
99- return selectVirtualDevice ( sdk , devices , avds ) ;
123+ throw new RunException ( 'No targets available.' , ERR_NO_TARGET ) ;
100124}
0 commit comments