|
| 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 | +} |
0 commit comments