#!/usr/bin/env node import * as fs from "fs"; import * as path from "path"; import * as minimist from "minimist"; import * as chalk from "chalk"; import { DataHelper } from "./helpers/data.helper"; import { HelpHelper } from "./helpers/help.helper"; const pckg = JSON.parse( fs.readFileSync(path.dirname(__dirname) + "/package.json").toString() ); const CommandFiles = fs.readdirSync(__dirname + "/commands/"); const args = minimist(process.argv.slice(2)); const RequestedCommand = args._[0]; const Commands: { name: string; arguments: string[]; description: string; handle: Function; }[] = []; CommandFiles.forEach(CommandFile => { if ( CommandFile.split(".").includes("ts") || CommandFile.split(".").includes("js") ) { const CurrentCommand = require(`./commands/${CommandFile}`).default; if ( !CurrentCommand.Signature().includes("<") || !CurrentCommand.Signature().includes(">") ) { throw new Error( `Cannot find the start, and or the end of the parameters in the Signature method of the command called "${CurrentCommand.Signature()}".` ); } Commands.push({ name: CurrentCommand.Signature() .split("<")[0] .trim(), arguments: CurrentCommand.Signature() .split("<")[1] .split(">")[0] .trim() .split(","), description: CurrentCommand.Description(), handle: CurrentCommand.HandleCommand }); } }); DataHelper.Commands = Commands; if ( Commands.filter(Command => typeof RequestedCommand === "undefined" ? true : Command.name.toLowerCase() === RequestedCommand.toLowerCase() ).length === 0 ) { console.error( `Cannot find a command called ${RequestedCommand.toLowerCase()}!` ); } Commands.forEach(Command => { if ( typeof RequestedCommand !== "undefined" && RequestedCommand.toLowerCase() === Command.name.toLowerCase() ) { const CommandArguments: string[] = args._.slice(1); const MissingArguments: string[] = Command.arguments.filter(Argument => Argument.endsWith("?") ? false : Argument === "" ? false : !CommandArguments.includes(Argument) ); if (MissingArguments.length > 0) { console.error(`Missing parameter ${MissingArguments[0]}!`); } Command.handle(args); } else { HelpHelper.OutputHelp(); } });