|
| 1 | +import colors from 'ansi-colors'; |
| 2 | +import {existsSync} from 'fs'; |
| 3 | +import inquirer from 'inquirer'; |
| 4 | +import path from 'path'; |
| 5 | + |
| 6 | +import Logger from '../../../../logger'; |
| 7 | +import {symbols} from '../../../../utils'; |
| 8 | +import {Options, Platform} from '../../interfaces'; |
| 9 | +import ADB from '../../utils/appium-adb'; |
| 10 | +import {getBinaryLocation} from '../../utils/common'; |
| 11 | +import {execBinaryAsync} from '../../utils/sdk'; |
| 12 | +import {showMissingRequirementsHelp} from '../common'; |
| 13 | + |
| 14 | +export async function installApp(options: Options, sdkRoot: string, platform: Platform): Promise<boolean> { |
| 15 | + try { |
| 16 | + const adbLocation = getBinaryLocation(sdkRoot, platform, 'adb', true); |
| 17 | + if (!adbLocation) { |
| 18 | + Logger.log(` ${colors.red(symbols().fail)} ${colors.cyan('adb')} binary not found.\n`); |
| 19 | + showMissingRequirementsHelp(); |
| 20 | + |
| 21 | + return false; |
| 22 | + } |
| 23 | + |
| 24 | + const adb = await ADB.createADB({allowOfflineDevices: true}); |
| 25 | + const devices = await adb.getConnectedDevices(); |
| 26 | + |
| 27 | + if (!devices.length) { |
| 28 | + Logger.log(`${colors.red('No device found running.')} Please connect a device to install the APK.`); |
| 29 | + Logger.log(`Use ${colors.cyan('npx @nightwatch/mobile-helper android connect')} to connect to a device.\n`); |
| 30 | + |
| 31 | + return true; |
| 32 | + } |
| 33 | + |
| 34 | + if (options.deviceId) { |
| 35 | + // If device id is passed then check if the id is valid. If not then prompt user to select a device. |
| 36 | + const deviceConnected = devices.find(device => device.udid === options.deviceId); |
| 37 | + if (!deviceConnected) { |
| 38 | + Logger.log(colors.yellow(`No connected device found with deviceId '${options.deviceId}'.\n`)); |
| 39 | + |
| 40 | + options.deviceId = ''; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + if (!options.deviceId) { |
| 45 | + // if device id not found, or invalid device id is found, then prompt the user |
| 46 | + // to select a device from the list of running devices. |
| 47 | + const deviceAnswer = await inquirer.prompt({ |
| 48 | + type: 'list', |
| 49 | + name: 'device', |
| 50 | + message: 'Select the device to install the APK:', |
| 51 | + choices: devices.map(device => device.udid) |
| 52 | + }); |
| 53 | + options.deviceId = deviceAnswer.device; |
| 54 | + } |
| 55 | + |
| 56 | + if (!options.path) { |
| 57 | + // if path to APK is not provided, then prompt the user to enter the path. |
| 58 | + const apkPathAnswer = await inquirer.prompt({ |
| 59 | + type: 'input', |
| 60 | + name: 'apkPath', |
| 61 | + message: 'Enter the path to the APK file:' |
| 62 | + }); |
| 63 | + options.path = apkPathAnswer.apkPath; |
| 64 | + } |
| 65 | + |
| 66 | + Logger.log(); |
| 67 | + |
| 68 | + options.path = path.resolve(process.cwd(), options.path as string); |
| 69 | + if (!existsSync(options.path)) { |
| 70 | + Logger.log(`${colors.red('No APK file found at: ' + options.path)}\nPlease provide a valid path to the APK file.\n`); |
| 71 | + |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + Logger.log('Installing APK...'); |
| 76 | + |
| 77 | + const installationStatus = await execBinaryAsync(adbLocation, 'adb', platform, `-s ${options.deviceId} install ${options.path}`); |
| 78 | + if (installationStatus?.includes('Success')) { |
| 79 | + Logger.log(colors.green('APK installed successfully!\n')); |
| 80 | + |
| 81 | + return true; |
| 82 | + } |
| 83 | + |
| 84 | + handleError(installationStatus); |
| 85 | + |
| 86 | + return false; |
| 87 | + } catch (err) { |
| 88 | + handleError(err); |
| 89 | + |
| 90 | + return false; |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +const handleError = (consoleOutput: any) => { |
| 95 | + Logger.log(colors.red('\nError while installing APK:')); |
| 96 | + |
| 97 | + let errorMessage = consoleOutput; |
| 98 | + if (consoleOutput.includes('INSTALL_FAILED_ALREADY_EXISTS')) { |
| 99 | + errorMessage = 'APK with the same package name already exists on the device.\n'; |
| 100 | + errorMessage += colors.reset(`\nPlease uninstall the app first from the device and then install again.\n`); |
| 101 | + errorMessage += colors.reset(`To uninstall, use: ${colors.cyan('npx @nightwatch/mobile-helper android uninstall --app')}\n`); |
| 102 | + } else if (consoleOutput.includes('INSTALL_FAILED_OLDER_SDK')) { |
| 103 | + errorMessage = 'Target installation location (AVD/Real device) has older SDK version than the minimum requirement of the APK.\n'; |
| 104 | + } |
| 105 | + |
| 106 | + Logger.log(colors.red(errorMessage)); |
| 107 | +}; |
0 commit comments