1 | #!/usr/bin/env node
|
2 |
|
3 | import * as fs from "fs";
|
4 | import * as path from "path";
|
5 | import * as minimist from "minimist";
|
6 | import * as chalk from "chalk";
|
7 |
|
8 | import { DataHelper } from "./helpers/data.helper";
|
9 | import { HelpHelper } from "./helpers/help.helper";
|
10 |
|
11 | const pckg = JSON.parse(
|
12 | fs.readFileSync(path.dirname(__dirname) + "/package.json").toString()
|
13 | );
|
14 | const CommandFiles = fs.readdirSync(__dirname + "/commands/");
|
15 |
|
16 | const args = minimist(process.argv.slice(2));
|
17 | const RequestedCommand = args._[0];
|
18 | const Commands: {
|
19 | name: string;
|
20 | arguments: string[];
|
21 | description: string;
|
22 | handle: Function;
|
23 | }[] = [];
|
24 | CommandFiles.forEach(CommandFile => {
|
25 | if (
|
26 | CommandFile.split(".").includes("ts") ||
|
27 | CommandFile.split(".").includes("js")
|
28 | ) {
|
29 | const CurrentCommand = require(`./commands/${CommandFile}`).default;
|
30 |
|
31 | if (
|
32 | !CurrentCommand.Signature().includes("<") ||
|
33 | !CurrentCommand.Signature().includes(">")
|
34 | ) {
|
35 | throw new Error(
|
36 | `Cannot find the start, and or the end of the parameters in the Signature method of the command called "${CurrentCommand.Signature()}".`
|
37 | );
|
38 | }
|
39 |
|
40 | Commands.push({
|
41 | name: CurrentCommand.Signature()
|
42 | .split("<")[0]
|
43 | .trim(),
|
44 | arguments: CurrentCommand.Signature()
|
45 | .split("<")[1]
|
46 | .split(">")[0]
|
47 | .trim()
|
48 | .split(","),
|
49 | description: CurrentCommand.Description(),
|
50 | handle: CurrentCommand.HandleCommand
|
51 | });
|
52 | }
|
53 | });
|
54 |
|
55 | DataHelper.Commands = Commands;
|
56 |
|
57 | if (
|
58 | Commands.filter(Command =>
|
59 | typeof RequestedCommand === "undefined"
|
60 | ? true
|
61 | : Command.name.toLowerCase() === RequestedCommand.toLowerCase()
|
62 | ).length === 0
|
63 | ) {
|
64 | console.error(
|
65 | `Cannot find a command called ${RequestedCommand.toLowerCase()}!`
|
66 | );
|
67 | }
|
68 |
|
69 | Commands.forEach(Command => {
|
70 | if (
|
71 | typeof RequestedCommand !== "undefined" &&
|
72 | RequestedCommand.toLowerCase() === Command.name.toLowerCase()
|
73 | ) {
|
74 | const CommandArguments: string[] = args._.slice(1);
|
75 | const MissingArguments: string[] = Command.arguments.filter(Argument =>
|
76 | Argument.endsWith("?")
|
77 | ? false
|
78 | : Argument === ""
|
79 | ? false
|
80 | : !CommandArguments.includes(Argument)
|
81 | );
|
82 | if (MissingArguments.length > 0) {
|
83 | console.error(`Missing parameter ${MissingArguments[0]}!`);
|
84 | }
|
85 | Command.handle(args);
|
86 | } else {
|
87 | HelpHelper.OutputHelp();
|
88 | }
|
89 | });
|