Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: uninstall avd #60

Merged
merged 7 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}

Loading