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 app #59

Merged
merged 5 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
117 changes: 117 additions & 0 deletions src/commands/android/subcommands/uninstall/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import colors from 'ansi-colors';
import inquirer from 'inquirer';

import Logger from '../../../../logger';
import {Options, Platform} from '../../interfaces';
import ADB from '../../utils/appium-adb';
import {getBinaryLocation} from '../../utils/common';
import {execBinaryAsync, execBinarySync} from '../../utils/sdk';
import {showMissingBinaryHelp} from '../common';

export async function uninstallApp(options: Options, sdkRoot: string, platform: Platform): Promise<boolean> {
try {
const adbLocation = getBinaryLocation(sdkRoot, platform, 'adb', true);
if (!adbLocation) {
showMissingBinaryHelp('adb');

return false;
}

const adb = await ADB.createADB({allowOfflineDevices: true});
const devices = await adb.getConnectedDevices();

if (!devices.length) {
Logger.log(`${colors.red('No device found running.')} Please connect the device to uninstall the APK.\n`);

return true;
}

if (options.deviceId) {
// If device id is passed then check if the id is valid. If not then prompt user to select a device.
const deviceConnected = devices.find(device => device.udid === options.deviceId);
if (!deviceConnected) {
Logger.log(colors.yellow(`No connected device found with deviceId '${options.deviceId}'.\n`));

options.deviceId = '';
}
}

if (!options.deviceId) {
// if device id not found, or invalid device id is found, then prompt the user
// to select a device from the list of running devices.
const deviceAnswer = await inquirer.prompt({
type: 'list',
name: 'device',
message: 'Select the device to uninstall the APK:',
choices: devices.map(device => device.udid)
});
options.deviceId = deviceAnswer.device;
}

const appNameAnswer = await inquirer.prompt({
type: 'input',
name: 'appName',
message: 'Enter the name of the App to uninstall:'
});

const packageNames = execBinarySync(adbLocation, 'adb', platform, `-s ${options.deviceId} shell pm list packages '${appNameAnswer.appName}'`);
if (!packageNames) {
Logger.log();
Logger.log(`${colors.red('App not found!')} Please try again.\n`);

return false;
}

const packagesList: string[] = [];
// Name of a package is in the format 'package:com.example.app'
packageNames.split('\n').forEach(line => {
if (line.includes('package:')) {
packagesList.push(line.split(':')[1].trim());
}
});

let packageName = packagesList[0];

if (packagesList.length > 1) {
const packageNameAnswer = await inquirer.prompt({
type: 'list',
name: 'packageName',
message: 'Select the package you want to uninstall:',
choices: packagesList
});

packageName = packageNameAnswer.packageName;
}

const UninstallationAnswer = await inquirer.prompt({
type: 'confirm',
name: 'confirm',
message: `Are you sure you want to uninstall ${colors.cyan(packageName)}`
});

Logger.log();

if (!UninstallationAnswer.confirm) {
Logger.log('Uninstallation cancelled.\n');

return false;
}

Logger.log(`Uninstalling ${colors.cyan(packageName)}...\n`);

const uninstallationStatus = await execBinaryAsync(adbLocation, 'adb', platform, `-s ${options.deviceId} uninstall ${packageName}`);
if (uninstallationStatus?.includes('Success')) {
Logger.log(`${colors.green('App uninstalled successfully!')}\n`);

return true;
}
itsspriyansh marked this conversation as resolved.
Show resolved Hide resolved

return false;
} catch (error) {
Logger.log(colors.red('Error occured while uninstalling App.'));
console.error(error);

return false;
}
}

6 changes: 4 additions & 2 deletions src/commands/android/subcommands/uninstall/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {Options, Platform} from '../../interfaces';
import {uninstallApp} from './app';
import {deleteAvd} from './avd';

export async function uninstall(options: Options, sdkRoot: string, platform: Platform): Promise<boolean> {
if (options.avd) {
if (options.app) {
return await uninstallApp(options, sdkRoot, platform);
} else if (options.avd) {
itsspriyansh marked this conversation as resolved.
Show resolved Hide resolved
return await deleteAvd(sdkRoot, platform);
}

return false;
}

Loading