From 1c5410ef2b62cde5fb9132e255a00cf38ad00229 Mon Sep 17 00:00:00 2001 From: Priyansh Prajapati <88396544+itsspriyansh@users.noreply.github.com> Date: Thu, 22 Aug 2024 01:11:42 +0530 Subject: [PATCH] Add default flow for the `install` subcommand (#67) --- src/commands/android/constants.ts | 27 ++++++++++++++ .../android/subcommands/install/app.ts | 2 +- .../android/subcommands/install/index.ts | 35 +++++++++++++++++-- 3 files changed, 61 insertions(+), 3 deletions(-) diff --git a/src/commands/android/constants.ts b/src/commands/android/constants.ts index b086e61..4c30706 100644 --- a/src/commands/android/constants.ts +++ b/src/commands/android/constants.ts @@ -41,6 +41,33 @@ export const AVAILABLE_SUBCOMMANDS: AvailableSubcommands = { description: 'Connect a real device wirelessly' } ] + }, + install: { + description: 'Install APK or AVD on a device', + flags: [ + { + name: 'avd', + description: 'Create an Android Virtual Device' + }, + { + name: 'app', + description: 'Install an APK on the device', + cliConfigs: [ + { + name: 'path', + alias: ['p'], + description: 'Path to the APK file', + usageHelp: 'path_to_apk' + }, + { + name: 'deviceId', + alias: ['s'], + description: 'Id of the device to install the APK', + usageHelp: 'device_id' + } + ] + } + ] } }; diff --git a/src/commands/android/subcommands/install/app.ts b/src/commands/android/subcommands/install/app.ts index 8231980..607d90d 100644 --- a/src/commands/android/subcommands/install/app.ts +++ b/src/commands/android/subcommands/install/app.ts @@ -95,7 +95,7 @@ const handleError = (consoleOutput: any) => { let errorMessage = consoleOutput; if (consoleOutput.includes('INSTALL_FAILED_ALREADY_EXISTS')) { errorMessage = 'APK with the same package name already exists on the device.\n'; - errorMessage += colors.reset(`\nPlease uninstall the app first from the device and then install again.\n`); + errorMessage += colors.reset('\nPlease uninstall the app first from the device and then install again.\n'); errorMessage += colors.reset(`To uninstall, use: ${colors.cyan('npx @nightwatch/mobile-helper android uninstall --app')}\n`); } else if (consoleOutput.includes('INSTALL_FAILED_OLDER_SDK')) { errorMessage = 'Target installation location (AVD/Real device) has older SDK version than the minimum requirement of the APK.\n'; diff --git a/src/commands/android/subcommands/install/index.ts b/src/commands/android/subcommands/install/index.ts index 8c3b0c1..6387629 100644 --- a/src/commands/android/subcommands/install/index.ts +++ b/src/commands/android/subcommands/install/index.ts @@ -1,14 +1,45 @@ +import inquirer from 'inquirer'; + +import Logger from '../../../../logger'; import {Options, Platform} from '../../interfaces'; +import {verifyOptions} from '../common'; import {installApp} from './app'; import {createAvd} from './avd'; export async function install(options: Options, sdkRoot: string, platform: Platform): Promise { - if (options.app) { + const optionsVerified = verifyOptions('install', options); + if (!optionsVerified) { + return false; + } + + let subcommandFlag = optionsVerified.subcommandFlag; + if (subcommandFlag === '') { + subcommandFlag = await promptForFlag(); + } + + if (subcommandFlag === 'app') { return await installApp(options, sdkRoot, platform); - } else if (options.avd) { + } else if (subcommandFlag === 'avd') { return await createAvd(sdkRoot, platform); } return false; } +async function promptForFlag(): Promise { + const flagAnswer = await inquirer.prompt({ + type: 'list', + name: 'flag', + message: 'Select what do you want to install:', + choices: ['APK', 'AVD'] + }); + Logger.log(); + + const flag = flagAnswer.flag; + if (flag === 'APK') { + return 'app'; + } + + return 'avd'; +} +