1
1
import { Footnote , MetadataGroup } from '@ionic/cli-framework' ;
2
2
import { onBeforeExit , sleepForever } from '@ionic/utils-process' ;
3
+ import * as Debug from 'debug' ;
3
4
import * as url from 'url' ;
4
5
5
6
import { CommandInstanceInfo , CommandLineInputs , CommandLineOptions , CommandMetadata , CommandMetadataOption , CommandPreRun , IShellRunOptions } from '../../definitions' ;
@@ -9,12 +10,14 @@ import { FatalException } from '../../lib/errors';
9
10
import { loadConfigXml } from '../../lib/integrations/cordova/config' ;
10
11
import { getPackagePath } from '../../lib/integrations/cordova/project' ;
11
12
import { filterArgumentsForCordova , generateOptionsForCordovaBuild } from '../../lib/integrations/cordova/utils' ;
12
- import { SUPPORTED_PLATFORMS , checkNativeRun , createNativeRunArgs , createNativeRunListArgs , runNativeRun } from '../../lib/native-run' ;
13
+ import { SUPPORTED_PLATFORMS , checkNativeRun , createNativeRunArgs , createNativeRunListArgs , getNativeTargets , runNativeRun } from '../../lib/native-run' ;
13
14
import { COMMON_SERVE_COMMAND_OPTIONS , LOCAL_ADDRESSES , serve } from '../../lib/serve' ;
14
15
import { createPrefixedWriteStream } from '../../lib/utils/logger' ;
15
16
16
17
import { CORDOVA_BUILD_EXAMPLE_COMMANDS , CORDOVA_RUN_OPTIONS , CordovaCommand } from './base' ;
17
18
19
+ const debug = Debug ( 'ionic:commands:run' ) ;
20
+
18
21
const NATIVE_RUN_OPTIONS : readonly CommandMetadataOption [ ] = [
19
22
{
20
23
name : 'native-run' ,
@@ -209,6 +212,21 @@ Just like with ${input('ionic cordova build')}, you can pass additional options
209
212
options [ 'native-run' ] = false ;
210
213
}
211
214
215
+ // If we're using native-run, and if --device and --emulator are not used,
216
+ // we can detect if hardware devices are plugged in and prefer them over
217
+ // any virtual devices the host has.
218
+ if ( options [ 'native-run' ] && ! options [ 'device' ] && ! options [ 'emulator' ] && platform ) {
219
+ const platformTargets = await getNativeTargets ( this . env , platform ) ;
220
+ const { devices } = platformTargets ;
221
+
222
+ debug ( `Native platform devices: %O` , devices ) ;
223
+
224
+ if ( devices . length > 0 ) {
225
+ this . env . log . info ( `Hardware device(s) found for ${ input ( platform ) } . Using ${ input ( '--device' ) } .` ) ;
226
+ options [ 'device' ] = true ;
227
+ }
228
+ }
229
+
212
230
await this . checkForPlatformInstallation ( platform ) ;
213
231
}
214
232
@@ -221,13 +239,13 @@ Just like with ${input('ionic cordova build')}, you can pass additional options
221
239
}
222
240
223
241
protected async runServeDeploy ( inputs : CommandLineInputs , options : CommandLineOptions ) {
224
- if ( ! this . project ) {
225
- throw new FatalException ( `Cannot run ${ input ( 'ionic cordova run/emulate' ) } outside a project directory.` ) ;
226
- }
227
-
228
242
const conf = await loadConfigXml ( this . integration ) ;
229
243
const metadata = await this . getMetadata ( ) ;
230
244
245
+ if ( ! this . project ) {
246
+ throw new FatalException ( `Cannot run ${ input ( `ionic cordova ${ metadata . name } ` ) } outside a project directory.` ) ;
247
+ }
248
+
231
249
let livereloadUrl = options [ 'livereload-url' ] ? String ( options [ 'livereload-url' ] ) : undefined ;
232
250
233
251
if ( ! livereloadUrl ) {
@@ -254,7 +272,7 @@ Just like with ${input('ionic cordova build')}, you can pass additional options
254
272
255
273
if ( options [ 'native-run' ] ) {
256
274
const [ platform ] = inputs ;
257
- const packagePath = await getPackagePath ( conf . getProjectInfo ( ) . name , platform , options [ 'emulator' ] as boolean ) ;
275
+ const packagePath = await getPackagePath ( conf . getProjectInfo ( ) . name , platform , ! options [ 'device' ] ) ;
258
276
const { port : portForward } = url . parse ( livereloadUrl ) ;
259
277
260
278
const buildOpts : IShellRunOptions = { stream : cordovalogws } ;
@@ -272,21 +290,21 @@ Just like with ${input('ionic cordova build')}, you can pass additional options
272
290
}
273
291
274
292
protected async runBuildDeploy ( inputs : CommandLineInputs , options : CommandLineOptions ) {
275
- if ( ! this . project ) {
276
- throw new FatalException ( `Cannot run ${ input ( 'ionic cordova run/emulate' ) } outside a project directory.` ) ;
277
- }
278
-
279
293
const conf = await loadConfigXml ( this . integration ) ;
280
294
const metadata = await this . getMetadata ( ) ;
281
295
296
+ if ( ! this . project ) {
297
+ throw new FatalException ( `Cannot run ${ input ( `ionic cordova ${ metadata . name } ` ) } outside a project directory.` ) ;
298
+ }
299
+
282
300
if ( options . build ) {
283
301
// TODO: use runner directly
284
302
await build ( { config : this . env . config , log : this . env . log , shell : this . env . shell , prompt : this . env . prompt , project : this . project } , inputs , generateOptionsForCordovaBuild ( metadata , inputs , options ) ) ;
285
303
}
286
304
287
305
if ( options [ 'native-run' ] ) {
288
306
const [ platform ] = inputs ;
289
- const packagePath = await getPackagePath ( conf . getProjectInfo ( ) . name , platform , options [ 'emulator' ] as boolean ) ;
307
+ const packagePath = await getPackagePath ( conf . getProjectInfo ( ) . name , platform , ! options [ 'device' ] ) ;
290
308
291
309
await this . runCordova ( filterArgumentsForCordova ( { ...metadata , name : 'build' } , options ) ) ;
292
310
await this . runNativeRun ( createNativeRunArgs ( { packagePath, platform } , { ...options , connect : false } ) ) ;
0 commit comments