From c87885f38f21698e3f589a3636253afe3834e163 Mon Sep 17 00:00:00 2001 From: Nameer Haider Date: Sun, 18 Feb 2024 02:25:57 +0300 Subject: [PATCH] adding CommandRunner --- src/commanders/command-runner.ts | 36 ++++++++++++++++++++++++++++++++ src/commanders/commander.ts | 12 +++++------ src/commanders/index.ts | 5 +++++ src/main.ts | 9 ++++++-- 4 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 src/commanders/command-runner.ts diff --git a/src/commanders/command-runner.ts b/src/commanders/command-runner.ts new file mode 100644 index 0000000..868e794 --- /dev/null +++ b/src/commanders/command-runner.ts @@ -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 { + for (const command of this.commands) { + const args = this.getArgs(); + const isExecuted = await command.run(args); + if (isExecuted) { + return true; + } + } + return false; + } +} diff --git a/src/commanders/commander.ts b/src/commanders/commander.ts index 523b04f..e3c042f 100644 --- a/src/commanders/commander.ts +++ b/src/commanders/commander.ts @@ -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) => Promise; - 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) => Promise; + public readonly isDefault: boolean = false; async run(args: string[]): Promise { if (this.extractCommand(args, this.command) || this.isDefault) { diff --git a/src/commanders/index.ts b/src/commanders/index.ts index f4d5ed2..19061ee 100644 --- a/src/commanders/index.ts +++ b/src/commanders/index.ts @@ -3,6 +3,11 @@ */ export * from './commands/package.command'; +/** + * Export Command Runner: + */ +export * from './command-runner'; + /** * Export SubCommands: */ diff --git a/src/main.ts b/src/main.ts index 2afd3b1..508e7a3 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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. @@ -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();