UNPKG

2.47 kBPlain TextView Raw
1#!/usr/bin/env node
2
3import * as fs from "fs";
4import * as path from "path";
5import * as minimist from "minimist";
6import * as chalk from "chalk";
7
8import { DataHelper } from "./helpers/data.helper";
9import { HelpHelper } from "./helpers/help.helper";
10
11const pckg = JSON.parse(
12 fs.readFileSync(path.dirname(__dirname) + "/package.json").toString()
13);
14const CommandFiles = fs.readdirSync(__dirname + "/commands/");
15
16const args = minimist(process.argv.slice(2));
17const RequestedCommand = args._[0];
18const Commands: {
19 name: string;
20 arguments: string[];
21 description: string;
22 handle: Function;
23}[] = [];
24CommandFiles.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
55DataHelper.Commands = Commands;
56
57if (
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
69Commands.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});