Skip to content

Commit

Permalink
refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
itsspriyansh committed Aug 25, 2024
1 parent 7a94c31 commit 916b619
Show file tree
Hide file tree
Showing 25 changed files with 3,952 additions and 1,103 deletions.
1,026 changes: 1,026 additions & 0 deletions src/commands/android/androidSetup.ts

Large diffs are not rendered by default.

57 changes: 54 additions & 3 deletions src/commands/android/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import inquirer from 'inquirer';
import os from 'os';
import path from 'path';

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

export const AVAILABLE_OPTIONS: AvailableOptions = {
help: {
Expand Down Expand Up @@ -34,7 +35,7 @@ export const AVAILABLE_OPTIONS: AvailableOptions = {
export const AVAILABLE_SUBCOMMANDS: AvailableSubcommands = {
connect: {
description: 'Connect to a device',
options: [
flags: [
{
name: 'wireless',
description: 'Connect a real device wirelessly'
Expand All @@ -43,7 +44,57 @@ export const AVAILABLE_SUBCOMMANDS: AvailableSubcommands = {
},
disconnect: {
description: 'Disconnect a real device or emulator',
options: []
cliConfigs: [{
name: 'deviceId',
alias: ['s'],
description: 'Id of the device to disconnect',
usageHelp: 'device_id'
}],
flags: []
},
list: {
description: 'List connected devices or installed AVDs',
flags: [{
name: 'device',
description: 'List connected devices (real devices and AVDs)'
},
{
name: 'avd',
description: 'List installed AVDs'
}]
},
install: {
description: 'Install APK or AVD on a device',
flags: [
{
name: 'avd',
description: 'Create an Android Virtual Device'
},
{
name: 'app',
description: 'Install an APK on the device',
cliConfigs: [
{
name: 'path',
alias: ['p'],
description: 'Path to the APK file',
usageHelp: 'path_to_apk'
},
{
name: 'deviceId',
alias: ['s'],
description: 'Id of the device to install the APK',
usageHelp: 'device_id'
}
]
}
]
},
uninstall: {
description: 'todo item',
flags: [
{name: 'avd', description: 'todo item'},
]
}
};

Expand Down
96 changes: 96 additions & 0 deletions src/commands/android/dotcommands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import colors from 'ansi-colors';
import {spawnSync} from 'child_process';
import * as dotenv from 'dotenv';
import path from 'path';

import {ANDROID_DOTCOMMANDS} from '../../constants';
import Logger from '../../logger';
import {getPlatformName} from '../../utils';
import {Platform, SdkBinary} from './interfaces';
import {checkJavaInstallation, getBinaryLocation, getBinaryNameForOS, getSdkRootFromEnv} from './utils/common';

export class AndroidDotCommand {
dotcmd: string;
args: string[];
sdkRoot: string;
rootDir: string;
platform: Platform;
androidHomeInGlobalEnv: boolean;

constructor(dotcmd: string, argv: string[], rootDir = process.cwd()) {
this.dotcmd = dotcmd;
this.args = argv.slice(1);
this.sdkRoot = '';
this.rootDir = rootDir;
this.platform = getPlatformName();
this.androidHomeInGlobalEnv = false;
}

async run(): Promise<boolean> {
if (!ANDROID_DOTCOMMANDS.includes(this.dotcmd)) {
Logger.log(colors.red(`Unknown dot command passed: ${this.dotcmd}\n`));

Logger.log('Run Android SDK command line tools using the following command:');
Logger.log(colors.cyan('npx @nightwatch/mobile-helper <DOTCMD> [options|args]\n'));

Logger.log(`Available Dot Commands: ${colors.magenta(ANDROID_DOTCOMMANDS.join(', '))}`);
Logger.log(`(Example command: ${colors.gray('npx @nightwatch/mobile-helper android.emulator @nightwatch-android-11')})\n`);

return false;
}

const javaInstalled = checkJavaInstallation(this.rootDir);
if (!javaInstalled) {
return false;
}

this.loadEnvFromDotEnv();

const sdkRootEnv = getSdkRootFromEnv(this.rootDir, this.androidHomeInGlobalEnv);
if (!sdkRootEnv) {
Logger.log(`Run: ${colors.cyan('npx @nightwatch/mobile-helper android --standalone')} to fix this issue.`);
Logger.log(`(Remove the ${colors.gray('--standalone')} flag from the above command if using the tool for testing.)\n`);

return false;
}
this.sdkRoot = sdkRootEnv;

return this.executeDotCommand();
}

loadEnvFromDotEnv(): void {
this.androidHomeInGlobalEnv = 'ANDROID_HOME' in process.env;
dotenv.config({path: path.join(this.rootDir, '.env')});
}

buildCommand(): string {
const binaryName = this.dotcmd.split('.')[1] as SdkBinary;
const binaryLocation = getBinaryLocation(this.sdkRoot, this.platform, binaryName, true);

let cmd: string;
if (binaryLocation === 'PATH') {
const binaryFullName = getBinaryNameForOS(this.platform, binaryName);
cmd = `${binaryFullName}`;
} else {
const binaryFullName = path.basename(binaryLocation);
const binaryDirPath = path.dirname(binaryLocation);
cmd = path.join(binaryDirPath, binaryFullName);
}

return cmd;
}

executeDotCommand(): boolean {
const cmd = this.buildCommand();
const result = spawnSync(cmd, this.args, {stdio: 'inherit'});

if (result.error) {
console.error(result.error);

return false;
}

return result.status === 0;
}
}

Loading

0 comments on commit 916b619

Please sign in to comment.