Skip to content

Commit

Permalink
feat: uninstall avd (#60)
Browse files Browse the repository at this point in the history
Co-authored-by: Priyansh Garg <[email protected]>
  • Loading branch information
itsspriyansh and garg3133 authored Aug 23, 2024
1 parent 8234c7c commit d7d9a81
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/commands/android/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ export const AVAILABLE_SUBCOMMANDS: AvailableSubcommands = {
]
}
]
},
uninstall: {
description: 'todo item',
flags: [
{name: 'avd', description: 'todo item'},
]
}
};

Expand Down
7 changes: 4 additions & 3 deletions src/commands/android/subcommands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {connect} from './connect';
import {showHelp} from './help';
import {install} from './install';
import {list} from './list';
import {uninstall} from './uninstall';

export class AndroidSubcommand {
sdkRoot: string;
Expand Down Expand Up @@ -61,9 +62,7 @@ export class AndroidSubcommand {
}
this.sdkRoot = sdkRootEnv;

this.executeSubcommand();

return false;
return await this.executeSubcommand();
}

loadEnvFromDotEnv(): void {
Expand All @@ -79,6 +78,8 @@ export class AndroidSubcommand {
return await install(this.options, this.sdkRoot, this.platform);
} else if (this.subcommand === 'list') {
return await list(this.options, this.sdkRoot, this.platform);
} else if (this.subcommand === 'uninstall') {
return await uninstall(this.options, this.sdkRoot, this.platform);
}

return false;
Expand Down
63 changes: 63 additions & 0 deletions src/commands/android/subcommands/uninstall/avd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import colors from 'ansi-colors';
import inquirer from 'inquirer';

import Logger from '../../../../logger';
import {Platform} from '../../interfaces';
import {getBinaryLocation} from '../../utils/common';
import {execBinarySync, execBinaryAsync} from '../../utils/sdk';
import {showMissingBinaryHelp} from '../common';

export async function deleteAvd(sdkRoot: string, platform: Platform): Promise<boolean> {
try {
const avdmanagerLocation = getBinaryLocation(sdkRoot, platform, 'avdmanager', true);
if (!avdmanagerLocation) {
showMissingBinaryHelp('avdmanager');

return false;
}

const installedAvds = execBinarySync(avdmanagerLocation, 'avdmanager', platform, 'list avd -c');
if (installedAvds === null) {
Logger.log(`${colors.red('\nFailed to fetch installed AVDs.')} Please try again.\n`);

return false;
} else if (installedAvds === '') {
Logger.log(`${colors.yellow('No installed AVD found.')}\n`);
Logger.log('To see the list of installed AVDs, run the following command:');
Logger.log(colors.cyan(' npx @nightwatch/mobile-helper android list --avd\n'));

return false;
}

const avdAnswer = await inquirer.prompt({
type: 'list',
name: 'avdName',
message: 'Select the AVD to delete:',
choices: installedAvds.split('\n').filter(avd => avd !== '')
});
const avdName = avdAnswer.avdName;

Logger.log();
Logger.log(`Deleting ${colors.cyan(avdName)}...\n`);

const deleteStatus = await execBinaryAsync(avdmanagerLocation, 'avdmanager', platform, `delete avd --name '${avdName}'`);

if (deleteStatus?.includes('deleted')) {
Logger.log(colors.green('AVD deleted successfully!\n'));

return true;
}

Logger.log(colors.red('Something went wrong while deleting AVD.'));
Logger.log('Command output:', deleteStatus);
Logger.log(`To verify if the AVD was deleted, run: ${colors.cyan('npx @nightwatch/mobile-helper android list --avd')}`);
Logger.log('If the AVD is still present, try deleting it again.\n');

return false;
} catch (error) {
Logger.log(colors.red('\nError occurred while deleting AVD.'));
console.error(error);

return false;
}
}
11 changes: 11 additions & 0 deletions src/commands/android/subcommands/uninstall/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {Options, Platform} from '../../interfaces';
import {deleteAvd} from './avd';

export async function uninstall(options: Options, sdkRoot: string, platform: Platform): Promise<boolean> {
if (options.avd) {
return await deleteAvd(sdkRoot, platform);
}

return false;
}

0 comments on commit d7d9a81

Please sign in to comment.