Skip to content

Commit

Permalink
adding CommandRunner
Browse files Browse the repository at this point in the history
  • Loading branch information
NAMEER242 committed Feb 17, 2024
1 parent c8ae767 commit c87885f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
36 changes: 36 additions & 0 deletions src/commanders/command-runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Commander } from './commander';

export class CommandRunner {
constructor(private readonly commands: Commander[]) {
this.checkCommands(commands);
}

private checkCommands(commands: Commander[]): void {
for (const command of commands) {
const isDuplicated =
commands.filter((c) => {
return c.command === command.command;
}).length > 1;
if (isDuplicated) {
const error = `Command "${command.command}" is duplicated`;
global._logger.error(error);
throw new Error(error);
}
}
}

private getArgs(): string[] {
return process.argv.slice(2);
}

async run(): Promise<boolean> {
for (const command of this.commands) {
const args = this.getArgs();
const isExecuted = await command.run(args);
if (isExecuted) {
return true;
}
}
return false;
}
}
12 changes: 6 additions & 6 deletions src/commanders/commander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ export class Commander {
this.isDefault = props.isDefault ?? false;
}

private readonly command: string;
private readonly description: string;
private readonly commandOptions: CommandOptions[];
private readonly subCommands: Commander[];
private readonly script: (options: Record<string, any>) => Promise<void>;
private readonly isDefault: boolean = false;
public readonly command: string;
public readonly description: string;
public readonly commandOptions: CommandOptions[];
public readonly subCommands: Commander[];
public readonly script: (options: Record<string, any>) => Promise<void>;
public readonly isDefault: boolean = false;

async run(args: string[]): Promise<boolean> {
if (this.extractCommand(args, this.command) || this.isDefault) {
Expand Down
5 changes: 5 additions & 0 deletions src/commanders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
*/
export * from './commands/package.command';

/**
* Export Command Runner:
*/
export * from './command-runner';

/**
* Export SubCommands:
*/
Expand Down
9 changes: 7 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node
import { dirname } from 'path';
import { packageCommand } from './commanders';
import { CommandRunner, packageCommand } from './commanders';
import { Logger } from './commons/utils';

// setting global variable to the parent directory.
Expand All @@ -15,4 +15,9 @@ global.configs = {
backupDir: `${global._projectDir}/package.backup`,
};

packageCommand.run(process.argv.slice(2));
async function bootstrap() {
const commandApp = new CommandRunner([packageCommand]);
await commandApp.run();
}

bootstrap();

0 comments on commit c87885f

Please sign in to comment.