Skip to content

Commit

Permalink
Add default flow for the install subcommand (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
itsspriyansh authored Aug 21, 2024
1 parent e84c677 commit 1c5410e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
27 changes: 27 additions & 0 deletions src/commands/android/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
]
}
]
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/commands/android/subcommands/install/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
35 changes: 33 additions & 2 deletions src/commands/android/subcommands/install/index.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
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<string> {
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';
}

0 comments on commit 1c5410e

Please sign in to comment.