import { Command , flags } from '@oclif/command' ; import AVDManager from '../avdmanager' ; import AVDLaunchOptions from '../avdlaunchoptions' ; import * as inquirer from 'inquirer' ; function getRandomAVDs ( avds ) { const randomIndex = Math . floor ( Math . random ( ) * avds . length ) ; return [ avds [ randomIndex ] ] ; } async function getUserSelectedAVDs ( avds ) { const answer = await inquirer . prompt ( [ selectDevicesQuestion ( avds ) ] ) ; return answer . avds ; } function createLaunchOptions ( flags ) { return { noSnapshotLoad : flags . cold , } ; } function selectDevicesQuestion ( avds ) { return { type : 'checkbox' , name : 'avds' , message : 'Select a device' , choices : avds , } ; } export default class Launch extends Command { static description = 'Launch an Android Virtual Device.' ; static flags = { cold : flags . boolean ( { char : 'c' , description : 'Performs a cold boot and saves the emulator state on exit.' } ) , random : flags . boolean ( { char : 'r' , description : 'Launches a random AVD.' } ) , version : flags . version ( { char : 'v' } ) , help : flags . help ( { char : 'h' } ) , } ; async run ( ) { const { flags } = this . parse ( Launch ) ; const avdManager = new AVDManager ( ) ; const availableAvds = await avdManager . getAll ( ) ; let avdsToLaunch ; if ( flags . random ) { avdsToLaunch = getRandomAVDs ( availableAvds ) ; } else { avdsToLaunch = await getUserSelectedAVDs ( availableAvds ) ; } avdsToLaunch . forEach ( async ( avd ) => { console . log ( `Launching ${ avd }...` ) ; await avdManager . launch ( avd , createLaunchOptions ( flags ) ) ; console . log ( 'done.' ) ; } ) ; } }