Skip to content

Commit

Permalink
refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
itsspriyansh committed Sep 4, 2024
1 parent a977011 commit 6a9d746
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 50 deletions.
38 changes: 6 additions & 32 deletions src/commands/android/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import inquirer from 'inquirer';
import os from 'os';
import path from 'path';

import {ApiLevelNames, AvailableOptions, SdkBinary} from './interfaces';
import {AvailableOptions, SdkBinary} from './interfaces';
import {AvailableSubcommands} from './subcommands/interfaces';

export const AVAILABLE_OPTIONS: AvailableOptions = {
Expand Down Expand Up @@ -85,9 +85,12 @@ export const AVAILABLE_SUBCOMMANDS: AvailableSubcommands = {
]
},
uninstall: {
description: 'todo item',
description: 'Uninstall system images, AVDs, or apps from a device',
flags: [
{name: 'avd', description: 'todo item'},
{
name: 'avd',
description: 'Delete an Android Virtual Device'
},
{
name: 'app',
description: 'Uninstall an APK from a device',
Expand Down Expand Up @@ -167,32 +170,3 @@ export const BINARY_TO_PACKAGE_NAME: Record<SdkBinary | typeof NIGHTWATCH_AVD, s
[NIGHTWATCH_AVD]: `system-images;android-30;google_apis;${ABI}`
};

export const APILevelNames: ApiLevelNames = {
'android-10': 'Gingerbread (v2.3.3)',
'android-11': 'Honeycomb (v3.0)',
'android-12': 'Honeycomb (v3.1)',
'android-13': 'Honeycomb (v3.2)',
'android-14': 'Ice Cream Sandwich (v4.0)',
'android-15': 'Ice Cream Sandwich (v4.0.3)',
'android-16': 'Jelly Bean (v4.1)',
'android-17': 'Jelly Bean (v4.2)',
'android-18': 'Jelly Bean (v4.3)',
'android-19': 'KitKat (v4.4)',
'android-20': 'KitKat Watch (v4.4W)',
'android-21': 'Lollipop (v5.0)',
'android-22': 'Lollipop (v5.1)',
'android-23': 'Marshmallow (v6.0)',
'android-24': 'Nougat (v7.0)',
'android-25': 'Nougat (v7.1)',
'android-26': 'Oreo (v8.0)',
'android-27': 'Oreo (v8.1)',
'android-28': 'Pie (v9.0)',
'android-29': 'Android 10',
'android-30': 'Android 11',
'android-31': 'Android 12',
'android-32': 'Android 12L',
'android-33': 'Android 13',
'android-34': 'Android 14',
'android-35': 'Android 15'
};

5 changes: 0 additions & 5 deletions src/commands/android/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,3 @@ export interface SetupConfigs {
}

export type SdkBinary = 'sdkmanager' | 'adb' | 'emulator' | 'avdmanager';

export interface ApiLevelNames {
[apiLevel: string]: string
}

29 changes: 29 additions & 0 deletions src/commands/android/subcommands/apiLevelNames.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"android-10": "Gingerbread (v2.3.3)",
"android-11": "Honeycomb (v3.0)",
"android-12": "Honeycomb (v3.1)",
"android-13": "Honeycomb (v3.2)",
"android-14": "Ice Cream Sandwich (v4.0)",
"android-15": "Ice Cream Sandwich (v4.0.3)",
"android-16": "Jelly Bean (v4.1)",
"android-17": "Jelly Bean (v4.2)",
"android-18": "Jelly Bean (v4.3)",
"android-19": "KitKat (v4.4)",
"android-20": "KitKat Watch (v4.4W)",
"android-21": "Lollipop (v5.0)",
"android-22": "Lollipop (v5.1)",
"android-23": "Marshmallow (v6.0)",
"android-24": "Nougat (v7.0)",
"android-25": "Nougat (v7.1)",
"android-26": "Oreo (v8.0)",
"android-27": "Oreo (v8.1)",
"android-28": "Pie (v9.0)",
"android-29": "Android 10",
"android-30": "Android 11",
"android-31": "Android 12",
"android-32": "Android 12L",
"android-33": "Android 13",
"android-34": "Android 14",
"android-35": "Android 15"
}

25 changes: 15 additions & 10 deletions src/commands/android/subcommands/install/system-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import colors from 'ansi-colors';
import inquirer from 'inquirer';

import Logger from '../../../../logger';
import {APILevelNames} from '../../constants';
import {Platform} from '../../interfaces';
import {getBinaryLocation} from '../../utils/common';
import {execBinarySync, spawnCommandSync} from '../../utils/sdk';
import apiLevelNames from '../apiLevelNames.json';
import {showMissingBinaryHelp} from '../common';
import {AvailableSystemImages} from '../interfaces';
import {ApiLevelNames, AvailableSystemImages} from '../interfaces';

export async function installSystemImage(sdkRoot: string, platform: Platform): Promise<boolean> {
try {
Expand All @@ -18,9 +18,9 @@ export async function installSystemImage(sdkRoot: string, platform: Platform): P
return false;
}

const stdout = execBinarySync(sdkmanagerLocation, 'sdkmanager', platform, '--list | grep "system-images;"');
const stdout = execBinarySync(sdkmanagerLocation, 'sdkmanager', platform, '--list');
if (!stdout) {
Logger.log(`${colors.red('Failed to fetch system images!')} Please try again.`);
Logger.log(`${colors.red('Failed to fetch available system images!')} Please try again.`);

return false;
}
Expand All @@ -33,6 +33,9 @@ export async function installSystemImage(sdkRoot: string, platform: Platform): P
const lines = stdout.split('\n').sort();

lines.forEach(line => {
if (!line.includes('system-images;')) {
return;
}
const image = line.split('|')[0].trim();
images.add(image);
});
Expand Down Expand Up @@ -66,11 +69,12 @@ export async function installSystemImage(sdkRoot: string, platform: Platform): P
});

const apiLevelsWithNames = Object.keys(availableSystemImages).map(apiLevel => {
if (APILevelNames[apiLevel]) {
return `${apiLevel}: ${APILevelNames[apiLevel]}`;
let name = apiLevel;
if ((apiLevelNames as ApiLevelNames)[apiLevel]) {
name = `${apiLevel}: ${(apiLevelNames as ApiLevelNames)[apiLevel]}`;
}

return apiLevel;
return {name, value: apiLevel};
});

const androidVersionAnswer = await inquirer.prompt({
Expand All @@ -79,7 +83,7 @@ export async function installSystemImage(sdkRoot: string, platform: Platform): P
message: 'Select the API level for system image:',
choices: apiLevelsWithNames
});
const apiLevel = androidVersionAnswer.androidVersion.split(':')[0];
const apiLevel = androidVersionAnswer.androidVersion;

const systemImageTypeAnswer = await inquirer.prompt({
type: 'list',
Expand All @@ -100,6 +104,7 @@ export async function installSystemImage(sdkRoot: string, platform: Platform): P
const systemImageName = `system-images;${apiLevel};${type};${arch}`;

Logger.log();
Logger.log(`Installing system image: ${colors.cyan(systemImageName)}\n`);

const installationStatus = spawnCommandSync(sdkmanagerLocation, 'sdkmanager', platform, [systemImageName]);
if (installationStatus) {
Expand All @@ -108,8 +113,8 @@ export async function installSystemImage(sdkRoot: string, platform: Platform): P
return true;
}

Logger.log(colors.red('Something went wrong while installing system image'));
Logger.log(`Please run ${colors.cyan('npx @nightwatch/mobile-helper android.sdkmanager --list')} to verify if the system image was installed.`);
Logger.log(colors.red('Something went wrong while installing system image.'));
Logger.log(`Please run ${colors.cyan('npx @nightwatch/mobile-helper android.sdkmanager --list_installed')} to verify if the system image was installed.`);
Logger.log('If the system image was not installed, please try installing again.\n');

return false;
Expand Down
5 changes: 5 additions & 0 deletions src/commands/android/subcommands/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,8 @@ export interface AvailableSystemImages {
archs: string[];
}[]
}

export interface ApiLevelNames {
[apiLevel: string]: string
}

4 changes: 1 addition & 3 deletions src/commands/android/utils/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,7 @@ export const spawnCommandSync = (binaryLocation: string, binaryName: string, pla
const binaryFullName = getBinaryNameForOS(platform, binaryName);
cmd = `${binaryFullName}`;
} else {
const binaryFullName = path.basename(binaryLocation);
const binaryDirPath = path.dirname(binaryLocation);
cmd = path.join(binaryDirPath, binaryFullName);
cmd = binaryLocation;
}

const result = spawnSync(cmd, args, {stdio: 'inherit'});
Expand Down

0 comments on commit 6a9d746

Please sign in to comment.