Skip to content

Commit d7d9a81

Browse files
feat: uninstall avd (#60)
Co-authored-by: Priyansh Garg <[email protected]>
1 parent 8234c7c commit d7d9a81

File tree

4 files changed

+84
-3
lines changed

4 files changed

+84
-3
lines changed

src/commands/android/constants.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ export const AVAILABLE_SUBCOMMANDS: AvailableSubcommands = {
7979
]
8080
}
8181
]
82+
},
83+
uninstall: {
84+
description: 'todo item',
85+
flags: [
86+
{name: 'avd', description: 'todo item'},
87+
]
8288
}
8389
};
8490

src/commands/android/subcommands/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {connect} from './connect';
1111
import {showHelp} from './help';
1212
import {install} from './install';
1313
import {list} from './list';
14+
import {uninstall} from './uninstall';
1415

1516
export class AndroidSubcommand {
1617
sdkRoot: string;
@@ -61,9 +62,7 @@ export class AndroidSubcommand {
6162
}
6263
this.sdkRoot = sdkRootEnv;
6364

64-
this.executeSubcommand();
65-
66-
return false;
65+
return await this.executeSubcommand();
6766
}
6867

6968
loadEnvFromDotEnv(): void {
@@ -79,6 +78,8 @@ export class AndroidSubcommand {
7978
return await install(this.options, this.sdkRoot, this.platform);
8079
} else if (this.subcommand === 'list') {
8180
return await list(this.options, this.sdkRoot, this.platform);
81+
} else if (this.subcommand === 'uninstall') {
82+
return await uninstall(this.options, this.sdkRoot, this.platform);
8283
}
8384

8485
return false;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import colors from 'ansi-colors';
2+
import inquirer from 'inquirer';
3+
4+
import Logger from '../../../../logger';
5+
import {Platform} from '../../interfaces';
6+
import {getBinaryLocation} from '../../utils/common';
7+
import {execBinarySync, execBinaryAsync} from '../../utils/sdk';
8+
import {showMissingBinaryHelp} from '../common';
9+
10+
export async function deleteAvd(sdkRoot: string, platform: Platform): Promise<boolean> {
11+
try {
12+
const avdmanagerLocation = getBinaryLocation(sdkRoot, platform, 'avdmanager', true);
13+
if (!avdmanagerLocation) {
14+
showMissingBinaryHelp('avdmanager');
15+
16+
return false;
17+
}
18+
19+
const installedAvds = execBinarySync(avdmanagerLocation, 'avdmanager', platform, 'list avd -c');
20+
if (installedAvds === null) {
21+
Logger.log(`${colors.red('\nFailed to fetch installed AVDs.')} Please try again.\n`);
22+
23+
return false;
24+
} else if (installedAvds === '') {
25+
Logger.log(`${colors.yellow('No installed AVD found.')}\n`);
26+
Logger.log('To see the list of installed AVDs, run the following command:');
27+
Logger.log(colors.cyan(' npx @nightwatch/mobile-helper android list --avd\n'));
28+
29+
return false;
30+
}
31+
32+
const avdAnswer = await inquirer.prompt({
33+
type: 'list',
34+
name: 'avdName',
35+
message: 'Select the AVD to delete:',
36+
choices: installedAvds.split('\n').filter(avd => avd !== '')
37+
});
38+
const avdName = avdAnswer.avdName;
39+
40+
Logger.log();
41+
Logger.log(`Deleting ${colors.cyan(avdName)}...\n`);
42+
43+
const deleteStatus = await execBinaryAsync(avdmanagerLocation, 'avdmanager', platform, `delete avd --name '${avdName}'`);
44+
45+
if (deleteStatus?.includes('deleted')) {
46+
Logger.log(colors.green('AVD deleted successfully!\n'));
47+
48+
return true;
49+
}
50+
51+
Logger.log(colors.red('Something went wrong while deleting AVD.'));
52+
Logger.log('Command output:', deleteStatus);
53+
Logger.log(`To verify if the AVD was deleted, run: ${colors.cyan('npx @nightwatch/mobile-helper android list --avd')}`);
54+
Logger.log('If the AVD is still present, try deleting it again.\n');
55+
56+
return false;
57+
} catch (error) {
58+
Logger.log(colors.red('\nError occurred while deleting AVD.'));
59+
console.error(error);
60+
61+
return false;
62+
}
63+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {Options, Platform} from '../../interfaces';
2+
import {deleteAvd} from './avd';
3+
4+
export async function uninstall(options: Options, sdkRoot: string, platform: Platform): Promise<boolean> {
5+
if (options.avd) {
6+
return await deleteAvd(sdkRoot, platform);
7+
}
8+
9+
return false;
10+
}
11+

0 commit comments

Comments
 (0)