-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.js
More file actions
250 lines (220 loc) · 8.77 KB
/
command.js
File metadata and controls
250 lines (220 loc) · 8.77 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
"use strict";
export const commands = {};
const aliases = {};
/**
* Registers a command's name and aliases for use in the terminal.
* @param {String} name The primary name to register the command with.
* @param {Command} command The command object.
*/
export function registerCommand(name, command) {
commands[name] = command;
commands[name].name = name;
for (const alias of command.aliases)
aliases[alias] = command;
}
/**
* Gets the command object associated with the given name.
* @param {String} name The name of a command, or one of a command's aliases.
* @returns {Command} The command.
*/
export function getCommand(name) {
if (commands.hasOwnProperty(name))
return commands[name];
else if (aliases.hasOwnProperty(name))
return aliases[name];
else
throw new Error(`Command \"${name}\" does not exist.`);
}
export class CommandFlag {
/**
*
* @param {String} name The name of the flag.
* @param {String} description A description of what the flag represents, for help purposes.
*/
constructor(name, description) {
this.name = name;
this.description = description;
}
}
export class CommandParam {
/**
*
* @param {String} name The name of the parameter.
* @param {String} description A description of what the parameter represents, for help purposes.
* @param {Boolean} optional Whether this parameter shouldn't be required. Defaults to false.
* @param {Boolean} variadic Whether this parameter consists of an arbitrarily long sequence of tokens rather than a single token. Defaults to false.
* @param {(input: String | String[]) => any} parser The function responsible for parsing the string value of the parameter to the desired final type.
* Takes in an array of string values if the parameter is variadic.
*/
constructor(name, description, optional = false, variadic = false, parser = (input) => { return input; }) {
this.name = name;
this.description = description;
this.optional = optional;
this.variadic = variadic;
this.parser = parser;
}
}
export class CommandOverload {
/**
* Creates a new overload for a terminal command.
* @param {String} helpText Text describing what this overload does.
* @param {CommandParam[]} params The parameters this overload expects.
* @param {CommandFlag[]} flags The flags this overload expects.
* @param {(params: object, flags: object) => void} executor The code that executes this overload's functionality. Receives parameters and flags as objects.
*/
constructor(helpText, params = [], flags = [], executor = () => { }) {
this.helpText = helpText;
this.params = params;
this.flags = flags;
this.executor = executor;
}
}
export class Command {
/**
* Creates a new terminal command.
* @param {CommandOverload[]} overloads The overloads (implementations) of the command.
* @param {String[]} aliases Aliases to use for command lookup in addition to the command's registered name.
* @param {boolean} hidden Whether this command should be hidden from the help menu.
*/
constructor(overloads = [], aliases = [], hidden = false) {
this.overloads = overloads;
this.aliases = aliases;
this.hidden = hidden;
}
}
/**
* Tokenizes text for command processing.
* @param {String} text The text to tokenize.
*/
export function tokenize(text) {
const tokens = [];
let inString = false;
let isEscaped = false;
let buffer = [];
for (const c of text) {
// If we're not escaped and we encounter a quote, we need to start/end a string token
if (!isEscaped && c == '"') {
// If string flag set, we're ending a string; push token we've built so far excluding the quotes
if (inString) {
tokens.push(buffer.join(''));
buffer = [];
}
// In either case, flip the string flag and continue to next char after quote
inString = !inString;
continue;
}
// If we're not already escaped, and we encounter a backslash, set escaped flag and continue to next char
if (!isEscaped && c == '\\') {
isEscaped = true;
continue;
}
// Don't tokenize whitespace unless we're in a string -- instead, finish any in-progress tokens
const isWhiteSpace = /\s/.test(c);
if (!inString && isWhiteSpace) {
if (buffer.length != 0) {
tokens.push(buffer.join(''));
buffer = [];
}
continue;
}
// Add current character to token builder
buffer.push(c);
// Reset escaped flag at end of char processing if set
if (isEscaped)
isEscaped = false;
}
// Add final token to tokens if in progress
if (buffer.length != 0)
tokens.push(buffer.join(''));
return tokens;
}
/**
* Executes the provided command given both raw and tokenized text from terminal input.
* @param {Command} command The command to be executed.
* @param {string} rawText The raw terminal input. Passed to the command as __rawText.
* @param {string[]} tokens The tokenized input. Passed to the command as __tokens.
*/
export function executeCommand(command, rawText, tokens) {
const allTokens = tokens.slice(1); // Remove command name
// Preprocess named and positional parameters from tokens for later parsing
const named = {};
const flags = {};
const positional = [];
for (let i = 0; i < allTokens.length; i++) {
const token = allTokens[i];
if (token.startsWith("--")) {
const name = token.slice(2);
// Grab all tokens between this named param and the next as values in case it ends up being a variadic param later
let values = [];
let j = i + 1;
while (j < allTokens.length && !allTokens[j].startsWith("--")) {
values.push(allTokens[j]);
j++;
}
named[name] = values;
} else if (token.startsWith("-")) {
flags[token.slice(1)] = true;
}
else {
positional.push(token);
}
}
let matchingOverload = null;
let parsedParams = {};
let parsedFlags = {};
// Try each overload
for (const overload of command.overloads) {
// Set the flags
for (const flag of overload.flags)
parsedFlags[flag.name] = flags.hasOwnProperty(flag.name);
// Build list of expected params based on matching precedence, keeping them in listed order if possible
const expectedParams = [];
// Explicitly named params first
expectedParams.push(...overload.params.filter((p) => named.hasOwnProperty(p.name)));
// Then required params
expectedParams.push(...overload.params.filter((p) => !p.optional && !expectedParams.includes(p)));
// Then optional non-variadic params
expectedParams.push(...overload.params.filter((p) => !p.variadic && !expectedParams.includes(p)));
// Then optional variadic params
expectedParams.push(...overload.params.filter((p) => !expectedParams.includes(p)));
const paramMap = {};
let success = true;
let posCursor = 0;
for (const param of expectedParams) {
const name = param.name;
let rawValue = undefined;
if (named.hasOwnProperty(name)) {
rawValue = param.variadic ? named[name] : named[name][0];
posCursor += (param.variadic ? rawValue.length : 1);
} else if (param.variadic) {
rawValue = positional.slice(posCursor);
posCursor = positional.length;
} else if (posCursor < positional.length) {
rawValue = positional[posCursor++];
} else if (!param.optional) {
success = false;
break;
}
if (rawValue !== undefined) {
try {
paramMap[name] = param.parser(rawValue);
} catch (e) {
success = false;
break;
}
}
}
// Overload matches only if all required params parsed and no leftover positional tokens
if (success && posCursor === positional.length) {
matchingOverload = overload;
parsedParams = paramMap;
break;
}
}
if (matchingOverload == null) {
throw new Error(`No overload of command "${tokens[0]}" matches the provided parameters. Try running <b>help ${tokens[0]}</b>.`);
}
parsedParams.__rawText = rawText;
parsedParams.__tokens = tokens;
matchingOverload.executor(parsedParams, parsedFlags);
}