-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
446 lines (399 loc) · 11.4 KB
/
index.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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
const cyan = (t: string) => `\x1b[36m${t}\x1b[0m`;
const gray = (t: string) => `\x1b[90m${t}\x1b[0m`;
const grayBold = (t: string) => `\x1b[90;1m${t}\x1b[0m`;
const white = (t: string) => `\x1b[97m${t}\x1b[0m`;
const whiteBold = (t: string) => `\x1b[97;1m${t}\x1b[0m`;
type CLIOptionType = "number" | "text" | "boolean";
interface Opt {
demandArgument?: boolean;
}
interface CLIOptionConstructorOptions {
alias?: string[] | string;
type?: CLIOptionType;
description?: string;
}
class CLIOption {
constructor(
name: string,
options?: CLIOptionConstructorOptions,
isRoot?: boolean,
isDefaultOption?: boolean
) {
this.name = name;
this.alias = (<string[]>[]).concat(options?.alias || []);
this.type = options?.type || "text";
this.description = options?.description || "No description provided";
this.isRoot = isRoot || false;
// For i18n reasons, see usage later in the code:
this.isDefaultOption = isDefaultOption || false;
}
name: string;
alias: string[];
type: CLIOptionType;
description: string;
isRoot: boolean;
isDefaultOption: boolean;
}
interface Lang {
"Usage:": string;
"Commands:": string;
"Root Options:": string;
"Options:": string;
"Display this help message": string;
}
interface CommandOptions {
/**
* When this is set, tauris does not, by default, add a "-h" option.
* Note that if the option "-h" is passed, tauris will still show a
* help message unless you also call `.noHelp()` on this command
* instance later
*/
noDefaultHelpOption?: boolean;
/**
* Translations of the default UI text
*/
language?: Lang;
}
export class UsageError extends Error {}
export class Command {
constructor(
name: string,
/**
* @deprecated use `.language()` and `.noHelp` instead
*/
opts?: CommandOptions
) {
this.name = name;
this.usageString = `${this.name} [...options]`;
this.opts = opts || {};
if (opts?.noDefaultHelpOption) this.options = [];
else
this.options = [
new CLIOption(
"h",
{
alias: ["help"],
type: "boolean",
description: this._translate("Display this help message"),
},
false,
true
),
];
}
clone(): Command {
const c = Object.assign(Object.create(Object.getPrototypeOf(this)), this);
return c;
}
private _translate(english: keyof Lang): string {
return this.opts?.language?.[english] || english;
}
language(lang: Lang) {
this.opts.language = lang;
return this;
}
/**
* Add a description
* @param message description
*/
describe(message: string) {
this.description = message;
return this;
}
/**
* Disable the help message
*/
noHelp() {
this.help = false;
this.options = this.options.filter(
(x) => !(x.isDefaultOption && x.name === "h")
);
return this;
}
/**
* Override the default usage string
* @param usageString Usage String
*/
usage(usageString: string) {
this.usageString = usageString;
return this;
}
/**
* Add an option
* @param name Option names
* @param alias Optionally any amount of aliases
*/
option(name: string, options?: CLIOptionConstructorOptions) {
this.options.push(new CLIOption(name, options));
return this;
}
/**
* Add an option that is automatically carried through to all subcommands and
* their subcommands, etc.
* @param name Option name
* @param options Configuration for option
* @returns this
*/
rootOption(name: string, options?: CLIOptionConstructorOptions) {
const opt = new CLIOption(name, options, true);
this.options.push(opt);
return this;
}
/**
* Delete a specific root option, list of root options or (if no argument is
* given) all root options for this command and all its children
* @param names Name(s) of the root options to delete
*/
clearRoot(name?: string): this;
/**
* Delete a specific root option, list of root options or (if no argument is
* given) all root options for this command and all its children
* @param names Name(s) of the root options to delete
*/
clearRoot(names?: string[]): this;
/**
* Delete a specific root option, list of root options or (if no argument is
* given) all root options for this command and all its children
* @param names Name(s) of the root options to delete
*/
clearRoot(names?: string | string[]) {
this.options = this.options.filter(
(x) =>
!(
x.isRoot &&
(names ? (<string[]>[]).concat(names).includes(x.name) : true)
)
);
this.clearedRoots = this.clearedRoots.concat(names || []);
return this;
}
/**
* Parse arguments into an object. If a command is called, returns a promise so you can await the command.
* @param argv Raw process arguments, without binary and file location (e.g. `process.argv.slice(2)`)
*/
parse(
argv: string[],
options?: {
/**
* Throw an error instead of exiting
*/
noExit?: boolean;
noPromise?: boolean;
}
): { [key: string]: any } | false | Promise<void> {
var res: { [key: string]: any } = {};
var index = 0;
while (index < argv.length) {
let isOption = false;
let stripped: string = argv[index];
while (stripped.startsWith("-")) {
isOption = true;
stripped = stripped.slice(1);
}
if (isOption) {
let done = false;
this.options.forEach((option) => {
if (option.name === stripped || option.alias.includes(stripped)) {
done = true;
if (option.name === "parameters") return;
if (option.type === "boolean") {
res[option.name] = true;
} else if (option.type === "text") {
res[option.name] = argv[++index];
} else if (option.type === "number") {
index++;
if (isNaN(parseFloat(argv[index]))) {
this.renderHelp();
if (options?.noExit)
throw new UsageError(
`Expected a number for option ${
option.name.length > 1 ? "-" : "--"
}${option.name}`
);
process.exit();
} else {
res[option.name] = argv[index];
}
}
}
});
if (!done) {
res[stripped] = argv[++index] || true;
}
} else {
const promise = this.subcommands
.map((cmd) => {
if (cmd.name === stripped) {
return cmd.callHandler(
cmd.parse(argv.slice(index + 1), {
noPromise: options?.noPromise,
noExit: options?.noExit,
})
);
}
return null;
})
.find((x) => !!x)
?.then((x) => (options?.noExit ? x : process.exit(x || 0)));
if (promise) return options?.noPromise ? false : promise;
else
Array.isArray(res.parameters)
? res.parameters.push(stripped)
: (res.parameters = [stripped]);
}
index++;
}
if (
this.help &&
(res.h || (this.opt.demandArgument && Object.keys(res).length === 0))
) {
this.renderHelp();
return false;
}
return res;
}
/**
* A message to display above the help message
* @param message Message
*/
header(message: string) {
this.helpHeader = message;
return this;
}
/**
* Show help and exit when no option or command is provided
*/
demandArgument(bool?: boolean) {
this.opt.demandArgument = bool || true;
return this;
}
/**
* Render the help message to the screen
*/
private renderHelp() {
console.log();
if (this.helpHeader) {
console.log(this.helpHeader);
console.log();
}
console.log(
`${whiteBold(this._translate("Usage:"))}\n\n ${grayBold("$")} ${cyan(
this.usageString
)}\n`
);
console.log(`${whiteBold(this._translate("Options:"))}\n`);
const optionToString = (option: CLIOption) => {
return [
(option.name.length === 1 ? "-" : "--") + option.name,
option.alias.map(
(alias: string) => (alias.length === 1 ? " -" : "--") + alias
),
].join(", ");
};
var longest =
this.options.map(optionToString).sort((a, b) => b.length - a.length)[0]
?.length + 5;
if (
this.subcommands
.map((cmd) => cmd.name)
.sort((a, b) => b.length - a.length)[0]?.length +
5 >
longest
) {
longest =
this.subcommands
.map((cmd) => cmd.name)
.sort((a, b) => b.length - a.length)[0]?.length + 5;
}
this.options
.filter((x) => !x.isRoot)
.forEach((option) => {
console.log(
` ${cyan(optionToString(option))} ${gray(".").repeat(
longest - optionToString(option).length
)} ${option.description}`
);
});
console.log();
if (this.options.filter((x) => x.isRoot).length) {
console.log(`${whiteBold(this._translate("Root Options:"))}\n`);
this.options
.filter((x) => x.isRoot)
.forEach((option) => {
console.log(
` ${cyan(optionToString(option))} ${gray(".").repeat(
longest - optionToString(option).length
)} ${option.description}`
);
});
console.log();
}
if (this.subcommands.length > 0) {
console.log(`${whiteBold(this._translate("Commands:"))}\n`);
this.subcommands.forEach((cmd) => {
console.log(
` ${cyan(cmd.name)} ${gray(".").repeat(longest - cmd.name.length)} ${
cmd.description
}`
);
});
console.log();
}
}
/**
* Called when the command is invoked as a subcommand
* @param callback Callback
*/
handler(
callback: (
this: Command,
argv: { [key: string]: any }
) => number | void | Promise<number | void>
) {
this._handler = callback;
return this;
}
/**
* Attach a subcommand
* @param cmd Subcommand to attach
*/
command(cmd: Command) {
cmd.parent = this;
cmd.options = cmd.options.concat(
this.options.filter((x) => x.isRoot && !cmd.clearedRoots.includes(x.name))
);
if (!cmd.opts?.language) {
cmd.opts = { ...(cmd.opts || {}), language: this.opts?.language };
cmd.options.forEach((x) => {
if (!x.isDefaultOption) return;
x.description = cmd._translate(x.description as keyof Lang);
});
}
this.subcommands.push(cmd);
return this;
}
root(): Command {
return this.parent ? this.parent.root() : this;
}
description: string = "No description provided";
private usageString: string;
private options: CLIOption[];
protected name: string;
private clearedRoots: string[] = [];
public parent: Command | undefined;
private help: boolean = true;
private helpHeader: string = "";
private opt: Opt = {};
private subcommands: Command[] = [];
protected opts: CommandOptions;
protected async callHandler(
argv: { [key: string]: any } | false | Promise<void>
): Promise<number | void> {
const args = await argv;
if (args && typeof args !== "number") return await this._handler(args);
}
protected _handler: (
this: Command,
argv: { [key: string]: any }
) => number | void | Promise<number | void> = () => {};
}