Skip to content

Commit

Permalink
Handle all 'android' logic in a separate file. (#63)
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 authored Aug 14, 2024
1 parent 290c862 commit ca9e51d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
23 changes: 23 additions & 0 deletions src/commands/android/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import colors from 'ansi-colors';

import {AndroidSetup} from './androidSetup';
import {Options} from './interfaces';
import {AndroidSubcommand} from './subcommands';
import {getSubcommandHelp} from './utils/common';

export function handleAndroidCommand(args: string[], options: Options): void {
if (args.length === 1) {
const androidSetup = new AndroidSetup(options);
androidSetup.run();
} else if (args.length === 2) {
// Here args[1] represents the android subcommand.
const androidSubcommand = new AndroidSubcommand(args[1], options);
androidSubcommand.run();
} else {
// android command doesn't accept more than one argument.
console.log(`${colors.red(`Too many arguments passed for 'android' command: ${args.slice(1).join(', ')}`)}\n`);

console.log(getSubcommandHelp());
}
}

32 changes: 12 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import colors from 'ansi-colors';
import minimist from 'minimist';

import {AndroidSetup} from './commands/android/androidSetup';
import {AndroidSubcommand} from './commands/android/subcommands';
import {AVAILABLE_COMMANDS} from './constants';
import {IosSetup} from './commands/ios';
import {handleAndroidCommand} from './commands/android';

export const run = () => {
try {
Expand All @@ -25,28 +25,20 @@ export const run = () => {
console.log(`${colors.red('No command passed.')}\n`);
}
showHelp();
} else if (!AVAILABLE_COMMANDS.includes(args[0])) {
console.log(`${colors.red(`Unknown command passed: ${args[0]}`)}\n`);
showHelp();
} else if (args.length > 2 || (args[0] === 'ios' && args.length > 1)) {
// android command can accept only one subcommand.
// ios command does not accept subcommands.
console.log(`${colors.red(`Too many arguments passed: ${args.slice(1).join(', ')}`)}\n`);
showHelp();
} else if (args[0] === 'android') {
if (args[1]) {
// args[1] represents the android subcommand.
// If subcommand is present then proceed to run the subcommand.
const androidSubcommand = new AndroidSubcommand(args[1], options);
androidSubcommand.run();
handleAndroidCommand(args, options);
} else if (args[0] === 'ios') {
if (args.length > 1) {
// ios command does not accept subcommands.
console.log(`${colors.red(`Too many arguments passed for 'ios' command: ${args.slice(1).join(', ')}`)}\n`);
console.log(`Run: ${colors.cyan('npx @nightwatch/mobile-helper ios --help')} to get help for 'ios' command.`);
} else {
// If no subcommand is present then proceed to run the main android setup.
const androidSetup = new AndroidSetup(options);
androidSetup.run();
const iOSSetup = new IosSetup(options);
iOSSetup.run();
}
} else if (args[0] === 'ios') {
const iOSSetup = new IosSetup(options);
iOSSetup.run();
} else {
console.log(`${colors.red(`Unknown command passed: ${args[0]}`)}\n`);
showHelp();
}
} catch (err) {
console.error(err as string);
Expand Down

0 comments on commit ca9e51d

Please sign in to comment.