-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
patchCommander.ts
64 lines (54 loc) 路 1.7 KB
/
patchCommander.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import chalk from "chalk";
import {Command} from "commander";
function at(n: number) {
// ToInteger() abstract op
n = Math.trunc(n) || 0;
// Allow negative indexing from the end
if (n < 0) n += this.length;
// OOB access is guaranteed to return undefined
if (n < 0 || n >= this.length) return undefined;
// Otherwise, this is just normal property access
return this[n];
}
const TypedArray = Reflect.getPrototypeOf(Int8Array);
for (const C of [Array, String, TypedArray]) {
if (C) {
Object.defineProperty((C as any).prototype, "at", {value: at, writable: true, enumerable: false, configurable: true});
}
}
const helpInformation = Command.prototype.helpInformation;
function colorizeSection(str: any, section: string) {
// eslint-disable-next-line prefer-const
let [before, after] = str.split(section);
after = after
.split("\n")
.map((line: string) => {
if (!line.match(/(\w+):$/)) {
const [cmd, ...rest] = line.trim().split(" ");
if (!cmd.startsWith("-")) {
return [" ", chalk.bold(chalk.blue(cmd)), ...rest].join(" ");
}
}
return line;
})
.join("\n");
return [before, after].join(chalk.green(section));
}
Command.prototype.helpInformation = function help() {
let str = helpInformation.call(this);
if (str.includes("Commands:")) {
str = colorizeSection(str, "Commands:");
}
if (str.includes("Arguments:")) {
str = colorizeSection(str, "Arguments:");
}
return (
"\n" +
str
.replace("Usage:", chalk.green("Usage:"))
.replace("Options:", chalk.green("Options:"))
.replace(/<(\w+)>/gi, `<${chalk.yellow("$1")}>`)
.replace(/\[(\w+)]/gi, `[${chalk.cyan("$1")}]`) +
"\n"
);
};