Skip to content

Commit

Permalink
refactor dotcmd handling
Browse files Browse the repository at this point in the history
  • Loading branch information
itsspriyansh committed Aug 18, 2024
1 parent 0d23ba8 commit 79a913a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
41 changes: 25 additions & 16 deletions src/commands/android/dotcommands.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,56 @@
import path from 'path';
import colors from 'ansi-colors';
import * as dotenv from 'dotenv';
import path from 'path';

import colors from 'ansi-colors';
import {ANDROID_DOTCOMMANDS} from '../../constants';
import Logger from '../../logger';
import {execBinarySync} from './utils/sdk';
import {getPlatformName} from '../../utils';
import {Platform, SdkBinary} from './interfaces';
import {checkJavaInstallation, getBinaryLocation, getSdkRootFromEnv} from './utils/common';
import {execBinarySync} from './utils/sdk';

export class AndroidDotCommand {
binary: SdkBinary;
dotcmd: string;
command: string;
sdkRoot: string;
rootDir: string;
platform: Platform;
androidHomeInGlobalEnv: boolean;

constructor(binary: SdkBinary, command: string, rootDir = process.cwd()) {
this.binary = binary;
this.command = command;
constructor(dotcmd: string, cmdArgs: string[], rootDir = process.cwd()) {
this.dotcmd = dotcmd;
this.command = cmdArgs.slice(1).join(' ');
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`));

return false;
}

const javaInstalled = checkJavaInstallation(this.rootDir);

if (!javaInstalled) {
return false;
}

this.loadEnvFromDotEnv();
const sdkRootEnv = getSdkRootFromEnv(this.androidHomeInGlobalEnv, this.rootDir);
const sdkRootEnv = getSdkRootFromEnv(this.rootDir, this.androidHomeInGlobalEnv);

if (!sdkRootEnv) {
Logger.log(`Run: ${colors.cyan('npx @nightwatch/mobile-helper android --standalone')} to setup Android SDK`);
Logger.log(' or provide the path to Android SDK if already installed.');
Logger.log(`(Remove the ${colors.gray('--standalone')} flag from the above command if setting up for testing.)\n`);
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;

this.executeCommand();
this.executeDotCommand();

return false;
}
Expand All @@ -54,14 +60,17 @@ export class AndroidDotCommand {
dotenv.config({path: path.join(this.rootDir, '.env')});
}

executeCommand(): boolean {
Logger.log(`Running: ${colors.cyan(this.binary)} ${colors.gray(this.command)}\n`);
executeDotCommand(): boolean {
const binaryName = this.dotcmd.split('.')[1] as SdkBinary;

Logger.log(`Running: ${colors.cyan(binaryName)} ${colors.gray(this.command)}\n`);

const binaryLocation = getBinaryLocation(this.sdkRoot, this.platform, this.binary, true);
const commandOutput = execBinarySync(binaryLocation, this.binary, this.platform, this.command);
const binaryLocation = getBinaryLocation(this.sdkRoot, this.platform, binaryName, true);
const commandOutput = execBinarySync(binaryLocation, binaryName, this.platform, this.command);

console.log(commandOutput);

return true;
}
}

9 changes: 7 additions & 2 deletions src/commands/android/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import {AndroidSetup} from './androidSetup';
import {Options} from './interfaces';
import {AndroidSubcommand} from './subcommands';
import {getSubcommandHelp} from './utils/common';
import {AndroidDotCommand} from './dotcommands';

export function handleAndroidCommand(args: string[], options: Options): void {
if (args.length === 1) {
export function handleAndroidCommand(args: string[], options: Options, argv: string[]): void {
if (args[0].includes('.')) {
// Here args[0] represents the android dot command
const androidDotCommand = new AndroidDotCommand(args[0], argv, process.cwd());
androidDotCommand.run();
} else if (args.length === 1) {
const androidSetup = new AndroidSetup(options);
androidSetup.run();
} else if (args.length === 2) {
Expand Down
3 changes: 1 addition & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export const AVAILABLE_COMMANDS = ['android', 'ios'];
export const AVAILABLE_DOT_COMMANDS = ['android'];
export const ANDROID_DOT_COMMANDS = ['adb', 'emulator', 'sdkmanager', 'avdmanager'];
export const ANDROID_DOTCOMMANDS = ['android.emulator', 'android.avdmanager', 'android.sdkmanager', 'android.adb'];
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export const run = () => {
console.log(`${colors.red('No command passed.')}\n`);
}
showHelp();
} else if (args[0] === 'android') {
handleAndroidCommand(args, options);
} else if (args[0].split('.')[0] === 'android') {
handleAndroidCommand(args, options, argv);
} else if (args[0] === 'ios') {
if (args.length > 1) {
// ios command does not accept subcommands.
Expand Down

0 comments on commit 79a913a

Please sign in to comment.