Skip to content

Commit

Permalink
Add --standalone flag for Android setup (#27)
Browse files Browse the repository at this point in the history
Co-authored-by: Priyansh Garg <priyanshgarg30@gmail.com>
  • Loading branch information
itsspriyansh and garg3133 committed May 23, 2024
1 parent ebfb259 commit 7053c19
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/commands/android/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export const AVAILABLE_OPTIONS: AvailableOptions = {
standalone: {
alias: [],
description: 'Do standalone setup for Android Emulator (no Nightwatch-related requirements will be downloaded).'
},
wireless: {
alias: [],
description: 'Connect ADB with wireless connection.'
}
};

Expand Down
5 changes: 5 additions & 0 deletions src/commands/android/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
} from './utils/sdk';

import DOWNLOADS from './downloads.json';
import {connectWirelessAdb} from './utils/adbWirelessConnect';


export class AndroidSetup {
Expand Down Expand Up @@ -107,6 +108,10 @@ export class AndroidSetup {
this.sdkRoot = sdkRootEnv || await this.getSdkRootFromUser();
process.env.ANDROID_HOME = this.sdkRoot;

if (this.options.wireless) {
return await connectWirelessAdb(this.sdkRoot, this.platform);
}

let result = true;

const setupConfigs: SetupConfigs = await this.getSetupConfigs(this.options);
Expand Down
95 changes: 95 additions & 0 deletions src/commands/android/utils/adbWirelessConnect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import {Platform} from '../interfaces';
import {execBinarySync} from './sdk';
import Logger from '../../../logger';
import inquirer from 'inquirer';
import colors from 'ansi-colors';
import { getBinaryLocation } from './common';

export async function connectWirelessAdb(sdkRoot: string, platform: Platform): Promise<boolean> {
try {
const adbLocation = getBinaryLocation(sdkRoot, platform, 'adb', true);
if (adbLocation === '') {
Logger.log(`${colors.red('ADB not found!')} Use ${colors.magenta('--setup')} flag with the main command to setup missing requirements.`);

return false;
}

Logger.log(`${colors.yellow('Note: This feature is available only for Android 11 and above.\n')}`);

Logger.log(`${colors.bold('Follow the below steps to connect to your device wirelessly:')}\n`);

Logger.log(`1.Connect your device to the same network as your computer.`)
Logger.log(`${colors.grey('You may connect your device to your computer\'s hotspot')}\n`);

Logger.log(`2.Enable developer options on your device by going to:`);
Logger.log(`${colors.cyan('Settings > About Phone > Build Number')}`);
Logger.log(`Tap on build number 7 times until you see the message ${colors.bold('You are now a developer!')}\n`);

Logger.log(`3.Enable wireless debugging on your device by searching ${colors.bold('wireless debugging')} in the search bar or by going to:`);
Logger.log(`${colors.cyan('Settings > Developer Options > Wireless Debugging')}\n`);

Logger.log(`4.Find the IP address and port number of your device on the wireless debugging screen`);
Logger.log(`${colors.grey('IP address and port number are separated by \':\' in the format <ip_address:port>\nwhere IP address comes before \':\' and port number comes after \':\'')}\n`);

const deviceIPAnswer = await inquirer.prompt({
type: 'input',
name: 'deviceIP',
message: 'Enter the IP address of your device:'
});
const deviceIP = deviceIPAnswer.deviceIP;

const portAnswer = await inquirer.prompt({
type: 'input',
name: 'port',
message: 'Enter the port number:'
});
const port = portAnswer.port;

Logger.log();
Logger.log(`5.Now find your device's pairing code and pairing port number by going to:`);
Logger.log(`${colors.cyan('Settings > Wireless Debugging > Pair device with pairing code')}`);
Logger.log(`${colors.grey('Here you will find a pairing code and a')} ${colors.magenta('IP address:port')} ${colors.grey('combination.\nThe port number associated with the IP address is the required pairing port number.\n')}`);

const pairingCodeAnswer = await inquirer.prompt({
type: 'input',
name: 'pairingCode',
message: 'Enter the pairing code displayed on your device:'
});
const pairingCode = pairingCodeAnswer.pairingCode;

const pairingPortAnswer = await inquirer.prompt({
type: 'input',
name: 'pairingPort',
message: 'Enter the pairing port number displayed on your device:'
});
const pairingPort = pairingPortAnswer.pairingPort;

Logger.log();
Logger.log('Pairing your device with your computer...\n');

const pairing = execBinarySync(adbLocation, 'adb', platform, `pair ${deviceIP}:${pairingPort} ${pairingCode}`);
if (pairing) {
Logger.log(`${colors.green('Pairing successful!')} Now connecting to device wirelessly...\n`);
} else {
Logger.log(`${colors.red('Pairing failed!')} Please try again.`);

return false;
}

const connecting = execBinarySync(adbLocation, 'adb', platform, `connect ${deviceIP}:${port}`);
if (connecting) {
Logger.log(colors.green('Connected to device wirelessly.'));
} else {
Logger.log(`${colors.red('Failed to connect!')} Please try again.`);

return false;
}

return true;
} catch (error) {
Logger.log('Error connecting to wifi ADB');
console.error('Error:', error);

return false;
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const run = () => {
try {
const argv = process.argv.slice(2);
const {_: args, ...options} = minimist(argv, {
boolean: ['install', 'setup', 'help', 'appium', 'standalone'],
boolean: ['install', 'setup', 'help', 'appium', 'standalone', 'wireless'],
alias: {
help: 'h',
mode: 'm',
Expand Down

0 comments on commit 7053c19

Please sign in to comment.