From 119b3c4dadc51ac576defce05bb74fbd50ddcdfd Mon Sep 17 00:00:00 2001 From: Apostolos Siokas Date: Thu, 29 Jul 2021 14:13:03 +0300 Subject: [PATCH] Clean up the project --- .lizard_installer.ts | 3 - bootstrap.ts | 7 - docs.ts | 27 +-- lizard.ts | 30 --- lizard_installer.sh | 6 - mod.ts | 6 +- src/Arguments.ts | 6 +- src/Command.ts | 33 ++-- src/Container.ts | 18 -- src/CustomOption.ts | 14 +- src/Denomander.ts | 14 +- src/Executor.ts | 43 ++-- src/Helper.ts | 42 ---- src/Kernel.ts | 29 +-- src/Lizard.ts | 113 ----------- src/Logger.ts | 41 ---- src/Option.ts | 48 ++--- src/Util.ts | 362 ---------------------------------- src/Validator.ts | 51 ++--- src/{ => types}/interfaces.ts | 2 +- src/{ => types}/types.ts | 6 +- src/utils/detect.ts | 169 ++++++++++++++++ src/utils/find.ts | 71 +++++++ src/utils/print.ts | 109 ++++++++++ src/utils/remove.ts | 42 ++++ src/utils/set.ts | 11 ++ src/utils/utils.ts | 16 ++ tests/command.test.ts | 2 +- tests/denomander.test.ts | 2 +- tests/helpers.test.ts | 17 -- tests/utils.test.ts | 40 ++-- tests/validations.test.ts | 8 +- 32 files changed, 599 insertions(+), 789 deletions(-) delete mode 100755 .lizard_installer.ts delete mode 100644 bootstrap.ts delete mode 100644 lizard.ts delete mode 100755 lizard_installer.sh delete mode 100644 src/Container.ts delete mode 100644 src/Helper.ts delete mode 100644 src/Lizard.ts delete mode 100644 src/Logger.ts delete mode 100644 src/Util.ts rename src/{ => types}/interfaces.ts (94%) rename src/{ => types}/types.ts (95%) create mode 100644 src/utils/detect.ts create mode 100644 src/utils/find.ts create mode 100644 src/utils/print.ts create mode 100644 src/utils/remove.ts create mode 100644 src/utils/set.ts create mode 100644 src/utils/utils.ts delete mode 100644 tests/helpers.test.ts diff --git a/.lizard_installer.ts b/.lizard_installer.ts deleted file mode 100755 index c1929dd..0000000 --- a/.lizard_installer.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { Lizard } from "./lizard.ts"; - -Lizard.parse(); diff --git a/bootstrap.ts b/bootstrap.ts deleted file mode 100644 index dcfa3da..0000000 --- a/bootstrap.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { Denomander } from "./src/Denomander.ts"; -import { Lizard as Lizy } from "./src/Lizard.ts"; - -const program = new Denomander(); -const Lizard = new Lizy(program); - -export { Lizard, program }; diff --git a/docs.ts b/docs.ts index cad8bcc..29634cd 100644 --- a/docs.ts +++ b/docs.ts @@ -1,14 +1,15 @@ /** Export every available functionality to generate the docs from "https://doc.deno.land" */ -export { Arguments } from "./src/Arguments.ts"; -export { Command } from "./src/Command.ts"; -export { Denomander } from "./src/Denomander.ts"; -export { Executor } from "./src/Executor.ts"; -export { Helper } from "./src/Helper.ts"; -export { Kernel } from "./src/Kernel.ts"; -export { Util } from "./src/Util.ts"; -export { Validator } from "./src/Validator.ts"; -export { Option } from "./src/Option.ts"; -export { Container } from "./src/Container.ts"; -export { Lizard } from "./src/Lizard.ts"; -export * from "./src/interfaces.ts"; -export * from "./src/types.ts"; +export * as Command from "./src/Command.ts"; +export * as Arguments from "./src/Arguments.ts"; +export * as Denomander from "./src/Denomander.ts"; +export * as Executor from "./src/Executor.ts"; +export * as Kernel from "./src/Kernel.ts"; +export * as Validator from "./src/Validator.ts"; +export * as Option from "./src/Option.ts"; +export * from "./src/types/interfaces.ts"; +export * from "./src/types/types.ts"; +export * from "./src/utils/detect.ts"; +export * from "./src/utils/find.ts"; +export * from "./src/utils/print.ts"; +export * from "./src/utils/set.ts"; +export * from "./src/utils/utils.ts"; diff --git a/lizard.ts b/lizard.ts deleted file mode 100644 index 937d149..0000000 --- a/lizard.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { Lizard, program } from "https://deno.land/x/denomander/mod.ts"; - -Lizard.appDetails({ - app_name: "Lizy", - app_description: "A new Lizard App", - app_version: "1.0.0", -}); - -Lizard.command("clone [url]", clone) - .option("-c, --color", "the color") - .option("-d, --dcolor", "the color") - .describe( - "this is a description", - ); - -Lizard.command("pull [repo]", ({ repo }: any) => { - if (program.force) { - console.log(`pull from ${repo} with force`); - } else { - console.log("Just pull from " + repo); - } -}).requiredOption("-f --force", "With force").describe( - "This is a pull command", -); - -function clone({ url }: any) { - console.log("clone from ..." + url); -} - -export { Lizard }; diff --git a/lizard_installer.sh b/lizard_installer.sh deleted file mode 100755 index aafed6d..0000000 --- a/lizard_installer.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/sh - -curl -sSf "https://deno.land/x/denomander/lizard.ts" -o ./lizard.ts -curl -sSf "https://deno.land/x/denomander/.lizard_installer.ts" -o ./.lizard_installer.ts - -deno "install" "-f" "-n" "lizard" ".lizard_installer.ts" \ No newline at end of file diff --git a/mod.ts b/mod.ts index 0598c0a..05d08a2 100644 --- a/mod.ts +++ b/mod.ts @@ -1,5 +1,5 @@ -import { Denomander } from "./src/Denomander.ts"; -export { CustomOption as Option } from "./src/CustomOption.ts"; -export { error_log, success_log, warning_log } from "./src/Logger.ts"; +import Denomander from "./src/Denomander.ts"; +export * as Option from "./src/CustomOption.ts"; +export { error_log, success_log, warning_log } from "./src/utils/print.ts"; export default Denomander; diff --git a/src/Arguments.ts b/src/Arguments.ts index f2bfaf8..a4a49f1 100644 --- a/src/Arguments.ts +++ b/src/Arguments.ts @@ -1,9 +1,9 @@ import { parse } from "../deps.ts"; -import { ArgumentsContract } from "./interfaces.ts"; -import { CustomArgs } from "./types.ts"; +import { ArgumentsContract } from "./types/interfaces.ts"; +import { CustomArgs } from "./types/types.ts"; /** It parses the arguments and splits them into commands and options */ -export class Arguments implements ArgumentsContract { +export default class Arguments implements ArgumentsContract { /** Aruments from Deno.args (unparsed) */ protected unparsed_args: Array; diff --git a/src/Command.ts b/src/Command.ts index 6433f6b..449ead2 100644 --- a/src/Command.ts +++ b/src/Command.ts @@ -1,15 +1,24 @@ -import { Helper } from "./Helper.ts"; +import { + removeBrackets, + removeDashes, + removeQuestionMark, +} from "./utils/remove.ts"; +import { + itContainsBrackets, + itContainsCurlyBrackets, + itContainsQuestionMark, +} from "./utils/detect.ts"; import { CommandArgument, CommandOption, CommandParams, OptionParameters, -} from "./types.ts"; -import { Option } from "./Option.ts"; -import { CustomOption } from "./CustomOption.ts"; +} from "./types/types.ts"; +import Option from "./Option.ts"; +import CustomOption from "./CustomOption.ts"; /* Command class */ -export class Command { +export default class Command { public declaration = ""; /** If the command has a required value to be passed from the user*/ public require_command_value = false; @@ -133,26 +142,26 @@ export class Command { const splitedValue = this.params.value.split(" "); if (splitedValue.length == 1) { - this._word_command = Helper.stripDashes(splitedValue[0]); + this._word_command = removeDashes(splitedValue[0]); } else { - this._word_command = Helper.stripDashes(splitedValue[0]); + this._word_command = removeDashes(splitedValue[0]); splitedValue.splice(0, 1); splitedValue.forEach((value) => { - if (Helper.containsBrackets(value)) { + if (itContainsBrackets(value)) { // Command Here - if (Helper.containsQuestionMark(value)) { + if (itContainsQuestionMark(value)) { this.command_arguments.push({ - argument: Helper.stripBrackets(Helper.stripQuestionMark(value)), + argument: removeBrackets(removeQuestionMark(value)), isRequired: false, }); } else { this.command_arguments.push({ - argument: Helper.stripBrackets(value), + argument: removeBrackets(value), isRequired: true, }); } } - if (Helper.containsCurlyBrackets(value)) { + if (itContainsCurlyBrackets(value)) { // Options Here } }); diff --git a/src/Container.ts b/src/Container.ts deleted file mode 100644 index c157e34..0000000 --- a/src/Container.ts +++ /dev/null @@ -1,18 +0,0 @@ -export class Container { - private static _instance: Container; - public bindings: any = []; - - public bind(abstract: string, concrete: any) { - if (this.bindings[abstract] === undefined) { - this.bindings[abstract] = concrete; - } - } - - public resolve(abstract: string) { - return this.bindings[abstract]; - } - - public static get Instance() { - return this._instance || (this._instance = new this()); - } -} diff --git a/src/CustomOption.ts b/src/CustomOption.ts index b97823e..9beaa47 100644 --- a/src/CustomOption.ts +++ b/src/CustomOption.ts @@ -1,13 +1,13 @@ -import { CommandOption } from "./types.ts"; +import { CommandOption } from "./types/types.ts"; /** - * Class that only handles the custom option form the user - * (it is not used but it reasign the proper Option object) - * - * @export - * @class CustomOption + * Class that only handles the custom option form the user + * (it is not used but it reasign the proper Option object) + * + * @export + * @class CustomOption */ -export class CustomOption { +export default class CustomOption { /** Holds the flags as defined (unparsed) */ public _flags: string; diff --git a/src/Denomander.ts b/src/Denomander.ts index 458e2ec..8d74024 100644 --- a/src/Denomander.ts +++ b/src/Denomander.ts @@ -1,12 +1,12 @@ -import { Arguments } from "./Arguments.ts"; -import { Command } from "./Command.ts"; -import { Kernel } from "./Kernel.ts"; -import { PublicAPI } from "./interfaces.ts"; -import { CommandOption, VersionType } from "./types.ts"; -import { CustomOption } from "./CustomOption.ts"; +import Arguments from "./Arguments.ts"; +import Command from "./Command.ts"; +import Kernel from "./Kernel.ts"; +import CustomOption from "./CustomOption.ts"; +import { PublicAPI } from "./types/interfaces.ts"; +import { CommandOption, VersionType } from "./types/types.ts"; /** The main class */ -export class Denomander extends Kernel implements PublicAPI { +export default class Denomander extends Kernel implements PublicAPI { /** Parses the args*/ public parse(args: Array) { this.args = new Arguments(args); diff --git a/src/Executor.ts b/src/Executor.ts index 39fae51..ab3ac78 100644 --- a/src/Executor.ts +++ b/src/Executor.ts @@ -1,14 +1,17 @@ -import { Arguments } from "./Arguments.ts"; -import { Kernel } from "./Kernel.ts"; -import { Command } from "./Command.ts"; -import { Util } from "./Util.ts"; -import { CommandArgument, ValidationRules } from "./types.ts"; -import { Helper } from "./Helper.ts"; -import { Option } from "./Option.ts"; -import { Validator } from "./Validator.ts"; +import Arguments from "./Arguments.ts"; +import Kernel from "./Kernel.ts"; +import Command from "./Command.ts"; +import Option from "./Option.ts"; +import Validator from "./Validator.ts"; +import { CommandArgument, ValidationRules } from "./types/types.ts"; +import { isCommandInArgs, isOptionInArgs } from "./utils/detect.ts"; +import { findCommandFromArgs, findOptionFromArgs } from "./utils/find.ts"; +import { printCommandHelp } from "./utils/print.ts"; +import { setOptionValue } from "./utils/set.ts"; +import { trimDashesAndSpaces } from "./utils/remove.ts"; /** It is responsible for generating the app variables and running the necessary callback functions */ -export class Executor { +export default class Executor { /** User have the option to throw the errors */ public throw_errors: boolean; @@ -29,15 +32,15 @@ export class Executor { public defaultCommands(): Executor { if (this.args) { this.args.commands.forEach((argCommand) => { - const command = Util.findCommandFromArgs(this.app.commands, argCommand); + const command = findCommandFromArgs(this.app.commands, argCommand); if (command) { command.options.forEach((option: Option) => { - option.value = Util.setOptionValue(option, this.args!); + option.value = setOptionValue(option, this.args!); - if (Util.optionIsInArgs(option, this.args!)) { + if (isOptionInArgs(option, this.args!)) { if (option.word_option == "help") { - Util.printCommandHelp(command!); + printCommandHelp(command!); Deno.exit(0); } } @@ -59,7 +62,7 @@ export class Executor { }).validate(); this.args.commands.forEach((arg: string, key: number) => { - const command: Command | undefined = Util.findCommandFromArgs( + const command: Command | undefined = findCommandFromArgs( this.app.commands, arg, ); @@ -100,7 +103,7 @@ export class Executor { throw_errors: this.throw_errors, }).validate(); for (const key in this.args.options) { - const command: Command | undefined = Util.findCommandFromArgs( + const command: Command | undefined = findCommandFromArgs( this.app.commands, this.args.commands[0], ); @@ -149,14 +152,14 @@ export class Executor { }).validate(); this.app.on_commands.forEach((onCommand) => { - const command: Command | undefined = Util.findCommandFromArgs( + const command: Command | undefined = findCommandFromArgs( this.app.commands, onCommand.arg, ); - const option: Option | undefined = Util.findOptionFromArgs( + const option: Option | undefined = findOptionFromArgs( this.app.BASE_COMMAND.options, - Helper.noDashesTrimSpaces(onCommand.arg), + trimDashesAndSpaces(onCommand.arg), ); if (command) { @@ -164,7 +167,7 @@ export class Executor { this.app.available_actions.push(command); } - if (option && Util.optionIsInArgs(option, this.args)) { + if (option && isOptionInArgs(option, this.args)) { if (this.args.options[option.word_option]) { onCommand.callback(this.args.options[option.word_option]); } @@ -182,7 +185,7 @@ export class Executor { public actionCommands(): Executor { this.app.available_actions.forEach((command: Command) => { if (this.args) { - if (Util.isCommandInArgs(command, this.args)) { + if (isCommandInArgs(command, this.args)) { if (command.command_arguments.length == 0) { command.action(this.app); } else { diff --git a/src/Helper.ts b/src/Helper.ts deleted file mode 100644 index eefa6e2..0000000 --- a/src/Helper.ts +++ /dev/null @@ -1,42 +0,0 @@ -/* General helper functions */ -export class Helper { - /** It removes dashes from a string */ - public static stripDashes(text: string): string { - return text.substr(0, 2).replace(/-/g, "") + - text.substr(2, text.length - 1); - } - - /** It removes dashes from a string */ - public static stripBrackets(text: string): string { - return text.replace(/\[/g, "").replace(/\]/g, ""); - } - - /** It removes dashes from a string */ - public static stripQuestionMark(text: string): string { - return text.replace(/\?/g, ""); - } - - /** Detects if a strign contains brackets */ - public static containsBrackets(text: string): boolean { - return text.match(/\[(.*?)\]/) ? true : false; - } - - /** Detects if a strign contains curly brackets */ - public static containsCurlyBrackets(text: string): boolean { - return text.match(/\{(.*?)\}/) ? true : false; - } - - /** Detects if a strign contains curly brackets */ - public static containsQuestionMark(text: string): boolean { - return text.match(/\?/) ? true : false; - } - - /** It trims of the empty spaces from the given string */ - public static trimString(text: string): string { - return text.replace(/\s/g, ""); - } - - public static noDashesTrimSpaces(text: string) { - return Helper.stripDashes(Helper.trimString(text)); - } -} diff --git a/src/Kernel.ts b/src/Kernel.ts index dd85787..88474a9 100644 --- a/src/Kernel.ts +++ b/src/Kernel.ts @@ -1,26 +1,27 @@ -import { Command } from "./Command.ts"; -import { Validator } from "./Validator.ts"; -import { Arguments } from "./Arguments.ts"; -import { Executor } from "./Executor.ts"; -import { Util } from "./Util.ts"; +import Command from "./Command.ts"; +import Validator from "./Validator.ts"; +import Arguments from "./Arguments.ts"; +import Executor from "./Executor.ts"; +import Option from "./Option.ts"; import { AliasCommandBuilder, AppDetails, - CommandArgument, CustomArgs, DenomanderErrors, KernelAppDetails, OnCommand, OptionBuilder, ValidationRules, -} from "./types.ts"; -import { Option } from "./Option.ts"; +} from "./types/types.ts"; +import { isEmptyArgs, isOptionInArgs } from "./utils/detect.ts"; +import { print_help } from "./utils/print.ts"; +import { setOptionValue } from "./utils/set.ts"; /** * It is the core of the app. It is responsible for almost everything. * The executeProgram() is the starting point of the app. */ -export abstract class Kernel { +abstract class Kernel { /** * Multiple variables that will be defined during runtime, * holding the values of the commands passed from the user @@ -170,12 +171,12 @@ export abstract class Kernel { app_version: this.app_version, }; - Util.print_help(app_details, this.commands, this.BASE_COMMAND); + print_help(app_details, this.commands, this.BASE_COMMAND); } /** Detects if there are no args and prints the help screen */ protected detectEmptyArgs(): Kernel { - if (this.args && Util.emptyArgs(this.args)) { + if (this.args && isEmptyArgs(this.args)) { this.printDefaultHelp(); Deno.exit(0); } @@ -186,7 +187,7 @@ export abstract class Kernel { protected detectDefaultOptions(): Kernel { if (this.args && this.args.commands.length == 0) { this.BASE_COMMAND.options.forEach((option) => { - if (Util.optionIsInArgs(option, this.args!)) { + if (isOptionInArgs(option, this.args!)) { if (option.word_option == "help") { this.printDefaultHelp(); Deno.exit(0); @@ -213,7 +214,7 @@ export abstract class Kernel { }).validate(); this.BASE_COMMAND.options.forEach((option) => { - option.value = Util.setOptionValue(option, this.args!); + option.value = setOptionValue(option, this.args!); this[option.word_option] = option.value; }); @@ -240,3 +241,5 @@ export abstract class Kernel { return this.setup().execute(); } } + +export default Kernel; diff --git a/src/Lizard.ts b/src/Lizard.ts deleted file mode 100644 index 0cd3a53..0000000 --- a/src/Lizard.ts +++ /dev/null @@ -1,113 +0,0 @@ -import { Kernel } from "./Kernel.ts"; -import { Command } from "./Command.ts"; -import { AppDetails } from "./types.ts"; - -/** Lizard class is a wraper arround Denomander app */ -export class Lizard { - /** Holds all the commands defined by the user */ - public allCommands: Array = []; - /** Holds the main Kernel instance passed in constructor */ - private app: Kernel; - - /** Constructor of the Lizard object */ - constructor(app: Kernel) { - this.app = app; - } - - /** Setup all the details of the app at once (name, description, version) */ - public appDetails(app_details: AppDetails): Lizard { - this.app.app_name = app_details.app_name; - this.app.app_description = app_details.app_description; - this.app.app_version = app_details.app_version; - - return this; - } - - /** Setup the name of the app */ - public appName(name: string): Lizard { - this.app.app_name = name; - - return this; - } - - /** Setup the description of the app */ - public appDescription(description: string): Lizard { - this.app.app_description = description; - - return this; - } - - /** Setup the version of the app */ - public appVersion(version: string): Lizard { - this.app.app_version = version; - - return this; - } - - /** Define a command */ - public command(name: string, callback: Function): Lizard { - this.app.command(name).action(callback); - this.allCommands.push(name); - return this; - } - - /** Define a description for the previous defined command */ - public describe(text: string): Lizard { - const command: Command = this.app.commands.slice(-1)[0]; - if (command) { - command.description = text; - } - return this; - } - - /** Define an option that belongs to the latest defined command */ - public option( - flags: string, - description: string, - callback?: Function, - ): Lizard { - const command: Command = this.app.commands.slice(-1)[0]; - - if (command) { - if (callback) { - command.addOption({ flags, description, callback }); - } else { - command.addOption({ flags, description }); - } - } - return this; - } - - /** Define a required option that belongs to the latest defined command */ - public requiredOption( - flags: string, - description: string, - callback?: Function, - ): Lizard { - const command: Command = this.app.commands.slice(-1)[0]; - - if (command) { - if (callback) { - command.addOption({ flags, description, callback, isRequired: true }); - } else { - command.addOption({ flags, description, isRequired: true }); - } - } - return this; - } - - public alias(...aliases: Array): Lizard { - const command: Command = this.app.commands.slice(-1)[0]; - - if (command) { - aliases.forEach((alias) => command.addAlias(alias)); - } - - return this; - } - - /** It is the starting point of the app. It parses the args and executes the code */ - public parse() { - this.app.parse(Deno.args); - } -} diff --git a/src/Logger.ts b/src/Logger.ts deleted file mode 100644 index 4c2b43d..0000000 --- a/src/Logger.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { blue, green, red, yellow } from "../deps.ts"; - -/** Prints success message */ -export function success_log(text: string): void { - return colored_output(`✅ ${text}`, "green"); -} - -/** Prints warning message */ -export function warning_log(text: string): void { - return colored_output(`⚠️ ${text}`, "yellow"); -} - -/** Prints error message */ -export function error_log(text: string): void { - return colored_output(`❌ ${text}`, "red"); -} - -/** Handles the output color */ -function colored_output(text: string, color: string = "normal") { - switch (color) { - case "red": - console.log(red(text)); - break; - - case "green": - console.log(green(text)); - break; - - case "yellow": - console.log(yellow(text)); - break; - - case "blue": - console.log(blue(text)); - break; - - default: - console.log(text); - break; - } -} diff --git a/src/Option.ts b/src/Option.ts index ad65995..f37a212 100644 --- a/src/Option.ts +++ b/src/Option.ts @@ -1,15 +1,15 @@ -import { Helper } from "./Helper.ts"; -import { Util } from "./Util.ts"; -import { Command } from "./Command.ts"; -import { OptionParameters } from "./types.ts"; +import Command from "./Command.ts"; +import { OptionParameters } from "./types/types.ts"; +import { splitValue } from "./utils/utils.ts"; +import { removeDashes, trimString } from "./utils/remove.ts"; /** - * Option class - * - * @export - * @class Option + * Option class + * + * @export + * @class Option */ -export class Option { +export default class Option { /** Holds the flags as defined (unparsed) */ public flags: string; @@ -78,36 +78,26 @@ export class Option { * and stores the word_option. */ private splitFlags() { - const splitedValue: Array = Util.splitValue(this.flags); + const splitedValue: Array = splitValue(this.flags); switch (splitedValue.length) { case 1: - if (Helper.stripDashes(splitedValue[0]).length === 1) { - this._letter_option = Helper.stripDashes(splitedValue[0]); + if (removeDashes(splitedValue[0]).length === 1) { + this._letter_option = removeDashes(splitedValue[0]); } else { - this._word_option = Helper.stripDashes(splitedValue[0]); + this._word_option = removeDashes(splitedValue[0]); } break; case 2: - if ( - Helper.stripDashes(Helper.trimString(splitedValue[0])).length === 1 - ) { - this._letter_option = Helper.stripDashes( - Helper.trimString(splitedValue[0]), - ); - - if ( - Helper.stripDashes(Helper.trimString(splitedValue[1])).length > 1 - ) { - this._word_option = Helper.stripDashes( - Helper.trimString(splitedValue[1]), - ); + if (removeDashes(trimString(splitedValue[0])).length === 1) { + this._letter_option = removeDashes(trimString(splitedValue[0])); + + if (removeDashes(trimString(splitedValue[1])).length > 1) { + this._word_option = removeDashes(trimString(splitedValue[1])); } } else { - this._word_option = Helper.stripDashes( - Helper.trimString(splitedValue[0]), - ); + this._word_option = removeDashes(trimString(splitedValue[0])); } break; diff --git a/src/Util.ts b/src/Util.ts deleted file mode 100644 index 504c155..0000000 --- a/src/Util.ts +++ /dev/null @@ -1,362 +0,0 @@ -import { bold, green, red, yellow } from "../deps.ts"; -import { Command } from "./Command.ts"; -import { Helper } from "./Helper.ts"; -import { Arguments } from "./Arguments.ts"; -import { Kernel } from "./Kernel.ts"; -import { AppDetails, CommandArgument, CustomArgs } from "./types.ts"; -import { Option } from "./Option.ts"; - -/** Specific functionality */ -export class Util { - /** It prints out the help doc */ - public static print_help( - app_details: AppDetails, - commands: Array, - BASE_COMMAND: Command, - ) { - console.log(); - console.log(green(bold(app_details.app_name))); - console.log(red(bold("v" + app_details.app_version))); - console.log(); - console.log(yellow(bold("Description:"))); - console.log(app_details.app_description); - console.log(); - - console.log(yellow(bold("Options:"))); - BASE_COMMAND.options.forEach((option) => { - console.log(option.flags + " \t " + option.description); - }); - - console.log(); - - console.log(yellow(bold("Commands:"))); - commands.forEach((command) => { - console.log(command.value + " \t " + command.description); - }); - console.log(); - } - - /** Print the help screen for a specific command */ - public static printCommandHelp(command: Command) { - console.log(); - if (command.description) { - console.log(yellow(bold("Description:"))); - console.log(command.description); - console.log(); - } - console.log(yellow(bold("Command Usage:"))); - if (command.hasOptions()) { - console.log(command.usage + " {Options}"); - } else { - console.log(command.usage); - } - console.log(); - - if (command.command_arguments.length > 0) { - console.log(yellow(bold("Arguments:"))); - command.command_arguments.forEach((commandArg: CommandArgument) => { - console.log( - green(`${commandArg.argument}${commandArg.isRequired ? "" : "?"}`), - ); - }); - console.log(); - } - if (command.hasRequiredOptions()) { - console.log(yellow(bold("Required Options:"))); - command.requiredOptions.forEach((option) => { - let helpText = green(option.flags) + " \t " + option.description + - " \t "; - - if (option.hasDefaultValue()) { - helpText = helpText + `(default: ${option.defaultValue})`; - } - - if (option.choices) { - helpText = helpText + `(choices: ${option.choices.toString()})`; - } - console.log(helpText); - }); - console.log(); - } - console.log(yellow(bold("Options:"))); - command.options.forEach((option) => { - if (!option.isRequired) { - let helpText = green(option.flags) + " \t " + option.description + - " \t "; - - if (option.hasDefaultValue()) { - helpText = helpText + `(default: ${option.defaultValue})`; - } - - if (option.choices) { - helpText = helpText + `(choices: ${option.choices.toString()})`; - } - console.log(helpText); - } - }); - console.log(); - if (command.hasAlias()) { - console.log(yellow(bold("Aliases:"))); - command.aliases.forEach((alias) => console.log(alias)); - } - } - - /** Detects if option is in args */ - public static optionIsInArgs(option: Option, args: Arguments) { - let found = false; - - for (const key in args.options) { - if (key == option.word_option || key == option.letter_option) { - found = true; - } - } - - return found; - } - - /** Sets the option value */ - public static setOptionValue(option: Option, args: Arguments) { - for (const key in args.options) { - if (key == option.word_option || key == option.letter_option) { - return args.options[key]; - } - } - } - - /** It returns the command instance if founded in given arguments */ - public static findCommandFromArgs( - array: Array, - arg: string | number, - ): Command | undefined { - return array.find((command: Command) => { - if (typeof arg === "string") { - if (command.word_command === Helper.stripDashes(arg)) { - return command; - } - if (command.hasAlias()) { - const aliasFound = command.aliases.find((alias) => { - if (alias === Helper.stripDashes(arg)) { - return alias; - } - }); - if (aliasFound) { - return command; - } - } - } - }); - } - - /** It returns the command instance if founded in given arguments */ - public static findOptionFromArgs( - array: Array