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' ;
2
4
import { getOptionValue , getOptionValues } from '../utils/cli' ;
3
5
import { log } from '../utils/log' ;
4
6
import { onBeforeExit } from '../utils/process' ;
@@ -9,6 +11,8 @@ import { getInstalledAVDs } from './utils/avd';
9
11
import { installApkToDevice , selectDeviceByTarget , selectHardwareDevice , selectVirtualDevice } from './utils/run' ;
10
12
import { SDK , getSDK } from './utils/sdk' ;
11
13
14
+ const modulePrefix = 'native-run:android:run' ;
15
+
12
16
export async function run ( args : ReadonlyArray < string > ) : Promise < void > {
13
17
const sdk = await getSDK ( ) ;
14
18
const apkPath = getOptionValue ( args , '--app' ) ;
@@ -72,6 +76,8 @@ export async function run(args: ReadonlyArray<string>): Promise<void> {
72
76
}
73
77
74
78
export async function selectDevice ( sdk : SDK , args : ReadonlyArray < string > ) : Promise < Device > {
79
+ const debug = Debug ( `${ modulePrefix } :${ selectDevice . name } ` ) ;
80
+
75
81
const devices = await getDevices ( sdk ) ;
76
82
const avds = await getInstalledAVDs ( sdk ) ;
77
83
@@ -93,8 +99,26 @@ export async function selectDevice(sdk: SDK, args: ReadonlyArray<string>): Promi
93
99
94
100
if ( selectedDevice ) {
95
101
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 ) ;
96
120
}
97
121
}
98
122
99
- return selectVirtualDevice ( sdk , devices , avds ) ;
123
+ throw new RunException ( 'No targets available.' , ERR_NO_TARGET ) ;
100
124
}
0 commit comments