Skip to content

Commit 675cdc4

Browse files
fix: grep issue in avd creation (#71)
Co-authored-by: Priyansh Garg <priyanshgarg30@gmail.com>
1 parent 97d94f8 commit 675cdc4

File tree

1 file changed

+32
-16
lines changed
  • src/commands/android/subcommands/install

1 file changed

+32
-16
lines changed

src/commands/android/subcommands/install/avd.ts

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@ import {getBinaryLocation} from '../../utils/common';
77
import {execBinaryAsync, execBinarySync} from '../../utils/sdk';
88
import {getInstalledSystemImages, showMissingBinaryHelp} from '../common';
99

10-
type DeviceType = 'Nexus' | 'Pixel' | 'Wear OS' | 'Android TV' | 'Desktop' | 'Others';
11-
12-
const deviceTypesToGrepCommand: Record<DeviceType, string> = {
13-
'Nexus': 'Nexus',
14-
'Pixel': 'pixel',
15-
'Wear OS': 'wear',
16-
'Android TV': 'tv',
17-
'Desktop': 'desktop',
18-
'Others': '-Ev "wear|Nexus|pixel|tv|desktop"'
19-
};
10+
const DEVICE_TYPES = [
11+
{name: 'Nexus', value: 'Nexus'},
12+
{name: 'Pixel', value: 'pixel'},
13+
{name: 'Wear OS', value: 'wear'},
14+
{name: 'Android TV', value: 'tv'},
15+
{name: 'Desktop', value: 'desktop'},
16+
{name: 'Others', value: 'Others'}
17+
];
2018

2119
export async function createAvd(sdkRoot: string, platform: Platform): Promise<boolean> {
2220
try {
@@ -64,24 +62,42 @@ export async function createAvd(sdkRoot: string, platform: Platform): Promise<bo
6462
type: 'list',
6563
name: 'deviceType',
6664
message: 'Select the device type for AVD:',
67-
choices: Object.keys(deviceTypesToGrepCommand)
65+
choices: DEVICE_TYPES
6866
});
6967
const deviceType = deviceTypeAnswer.deviceType;
7068

71-
let cmd = `list devices -c | grep ${deviceTypesToGrepCommand[deviceType as DeviceType]}`;
69+
let cmd = 'list devices -c';
7270
const availableDeviceProfiles = execBinarySync(avdmanagerLocation, 'avdmanager', platform, cmd);
73-
7471
if (!availableDeviceProfiles) {
75-
Logger.log(`${colors.red(`No potential device profile found for device type ${deviceType}.`)} Please try again.`);
72+
Logger.log(colors.red('\nSomething went wrong while retrieving available device profiles.'), 'Please try again.');
73+
74+
return false;
75+
}
76+
77+
const deviceTypeValues = DEVICE_TYPES.map(deviceType => deviceType.value);
78+
const matchingDeviceProfiles = availableDeviceProfiles
79+
.split('\n')
80+
.filter(line => line !== '')
81+
.filter(deviceProfile => {
82+
if (deviceType === 'Others') {
83+
return !deviceTypeValues.some(deviceTypeValue => deviceProfile.includes(deviceTypeValue));
84+
} else {
85+
return deviceProfile.includes(deviceType);
86+
}
87+
});
88+
89+
90+
if (!matchingDeviceProfiles.length) {
91+
Logger.log(colors.red(`\nNo potential device profile found for device type "${deviceType}".`), 'Please try again.');
7692

7793
return false;
7894
}
79-
const availableDeviceProfilesList = availableDeviceProfiles.split('\n').filter(deviceProfile => deviceProfile !== '');
95+
8096
const deviceAnswer = await inquirer.prompt({
8197
type: 'list',
8298
name: 'deviceProfile',
8399
message: 'Select the device profile for AVD:',
84-
choices: availableDeviceProfilesList
100+
choices: matchingDeviceProfiles
85101
});
86102
const deviceProfile = deviceAnswer.deviceProfile;
87103

0 commit comments

Comments
 (0)