@blazetrails/thor 0.0.0-stage → 0.1.0-alpha0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/NOTICE +9 -0
- package/dist/actions/create-file.d.ts +23 -0
- package/dist/actions/create-file.js +69 -0
- package/dist/actions/create-file.js.map +1 -0
- package/dist/actions/create-link.d.ts +10 -0
- package/dist/actions/create-link.js +34 -0
- package/dist/actions/create-link.js.map +1 -0
- package/dist/actions/directory.d.ts +31 -0
- package/dist/actions/directory.js +76 -0
- package/dist/actions/directory.js.map +1 -0
- package/dist/actions/empty-directory.d.ts +36 -0
- package/dist/actions/empty-directory.js +95 -0
- package/dist/actions/empty-directory.js.map +1 -0
- package/dist/actions/file-manipulation.d.ts +64 -0
- package/dist/actions/file-manipulation.js +190 -0
- package/dist/actions/file-manipulation.js.map +1 -0
- package/dist/actions/inject-into-file.d.ts +31 -0
- package/dist/actions/inject-into-file.js +111 -0
- package/dist/actions/inject-into-file.js.map +1 -0
- package/dist/actions.d.ts +120 -0
- package/dist/actions.js +287 -0
- package/dist/actions.js.map +1 -0
- package/dist/base.d.ts +223 -0
- package/dist/base.js +455 -0
- package/dist/base.js.map +1 -0
- package/dist/command.d.ts +69 -0
- package/dist/command.js +169 -0
- package/dist/command.js.map +1 -0
- package/dist/constants.generated.d.ts +2 -0
- package/dist/constants.generated.js +74 -0
- package/dist/constants.generated.js.map +1 -0
- package/dist/core-ext/hash-with-indifferent-access.d.ts +32 -0
- package/dist/core-ext/hash-with-indifferent-access.js +99 -0
- package/dist/core-ext/hash-with-indifferent-access.js.map +1 -0
- package/dist/error.d.ts +62 -0
- package/dist/error.js +105 -0
- package/dist/error.js.map +1 -0
- package/dist/group.d.ts +66 -0
- package/dist/group.js +313 -0
- package/dist/group.js.map +1 -0
- package/dist/invocation.d.ts +63 -0
- package/dist/invocation.js +104 -0
- package/dist/invocation.js.map +1 -0
- package/dist/line-editor/basic.d.ts +12 -0
- package/dist/line-editor/basic.js +30 -0
- package/dist/line-editor/basic.js.map +1 -0
- package/dist/line-editor.d.ts +4 -0
- package/dist/line-editor.js +9 -0
- package/dist/line-editor.js.map +1 -0
- package/dist/nested-context.d.ts +12 -0
- package/dist/nested-context.js +28 -0
- package/dist/nested-context.js.map +1 -0
- package/dist/parser/argument.d.ts +34 -0
- package/dist/parser/argument.js +103 -0
- package/dist/parser/argument.js.map +1 -0
- package/dist/parser/arguments.d.ts +42 -0
- package/dist/parser/arguments.js +164 -0
- package/dist/parser/arguments.js.map +1 -0
- package/dist/parser/option.d.ts +45 -0
- package/dist/parser/option.js +164 -0
- package/dist/parser/option.js.map +1 -0
- package/dist/parser/options.d.ts +69 -0
- package/dist/parser/options.js +332 -0
- package/dist/parser/options.js.map +1 -0
- package/dist/shell/basic.d.ts +64 -0
- package/dist/shell/basic.js +288 -0
- package/dist/shell/basic.js.map +1 -0
- package/dist/shell/color.d.ts +29 -0
- package/dist/shell/color.js +52 -0
- package/dist/shell/color.js.map +1 -0
- package/dist/shell/column-printer.d.ts +16 -0
- package/dist/shell/column-printer.js +28 -0
- package/dist/shell/column-printer.js.map +1 -0
- package/dist/shell/table-printer.d.ts +31 -0
- package/dist/shell/table-printer.js +120 -0
- package/dist/shell/table-printer.js.map +1 -0
- package/dist/shell/terminal.d.ts +4 -0
- package/dist/shell/terminal.js +35 -0
- package/dist/shell/terminal.js.map +1 -0
- package/dist/shell/wrapped-printer.d.ts +5 -0
- package/dist/shell/wrapped-printer.js +37 -0
- package/dist/shell/wrapped-printer.js.map +1 -0
- package/dist/shell.d.ts +51 -0
- package/dist/shell.js +129 -0
- package/dist/shell.js.map +1 -0
- package/dist/thor.d.ts +174 -0
- package/dist/thor.js +640 -0
- package/dist/thor.js.map +1 -0
- package/dist/util.d.ts +16 -0
- package/dist/util.js +87 -0
- package/dist/util.js.map +1 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +2 -0
- package/dist/version.js.map +1 -0
- package/package.json +55 -3
- package/README.md +0 -3
package/dist/thor.js
ADDED
|
@@ -0,0 +1,640 @@
|
|
|
1
|
+
import { kebabCase, ArgumentError, aryDelete, arySlice, cmp, eachPair, each, include, mergeBang, puts, Range, rbCmpint, rbFArray, rbFCaller, rbFSend, rbInspect, rbObjAsString, rbObjRespondTo, constSet, rbSetClassPathString, rtest, sort, stringSplit, toS, STDOUT, rbGvGet, rbGvSet, Hash, hashDelete, isModuleIncluded, TopLevel, rbClearMethodCache, } from "@blazetrails/ruby-compat";
|
|
2
|
+
import { Base, HELP_MAPPINGS } from "./base.js";
|
|
3
|
+
import { Actions } from "./actions.js";
|
|
4
|
+
import { Command, DynamicCommand, HiddenCommand } from "./command.js";
|
|
5
|
+
import { CoreExt, HashWithIndifferentAccess } from "./core-ext/hash-with-indifferent-access.js";
|
|
6
|
+
import { AmbiguousTaskError } from "./error.js";
|
|
7
|
+
import { Group } from "./group.js";
|
|
8
|
+
import { Argument } from "./parser/argument.js";
|
|
9
|
+
import { Arguments } from "./parser/arguments.js";
|
|
10
|
+
import { Option } from "./parser/option.js";
|
|
11
|
+
import { Options } from "./parser/options.js";
|
|
12
|
+
import { thorClassesIn } from "./util.js";
|
|
13
|
+
import * as LineEditor from "./line-editor.js";
|
|
14
|
+
import * as Util from "./util.js";
|
|
15
|
+
export class Thor {
|
|
16
|
+
static {
|
|
17
|
+
if (!rtest(rbGvGet("$thor_runner")))
|
|
18
|
+
rbGvSet("$thor_runner", false);
|
|
19
|
+
}
|
|
20
|
+
static Group = Group;
|
|
21
|
+
static LineEditor = LineEditor;
|
|
22
|
+
static Util = Util;
|
|
23
|
+
/** @internal */
|
|
24
|
+
static _packageName;
|
|
25
|
+
/** @internal */
|
|
26
|
+
static _defaultCommand;
|
|
27
|
+
/** @internal */
|
|
28
|
+
static _usage;
|
|
29
|
+
/** @internal */
|
|
30
|
+
static _desc;
|
|
31
|
+
/** @internal */
|
|
32
|
+
static _hide;
|
|
33
|
+
/** @internal */
|
|
34
|
+
static _longDesc;
|
|
35
|
+
/** @internal */
|
|
36
|
+
static _longDescWrap;
|
|
37
|
+
/** @internal */
|
|
38
|
+
static _map;
|
|
39
|
+
/** @internal */
|
|
40
|
+
static _methodOptions;
|
|
41
|
+
/** @internal */
|
|
42
|
+
static _subcommands;
|
|
43
|
+
/** @internal */
|
|
44
|
+
static _subcommandClasses;
|
|
45
|
+
/** @internal */
|
|
46
|
+
static _stopOnUnknownOption;
|
|
47
|
+
/** @internal */
|
|
48
|
+
static _disableRequiredCheck;
|
|
49
|
+
/** @internal */
|
|
50
|
+
static _methodExclusiveOptionNames;
|
|
51
|
+
/** @internal */
|
|
52
|
+
static _methodAtLeastOneOptionNames;
|
|
53
|
+
static packageName(name, _ = {}) {
|
|
54
|
+
return (this._packageName = name == null || name === "" ? null : name);
|
|
55
|
+
}
|
|
56
|
+
static defaultCommand(meth = null) {
|
|
57
|
+
if (rtest(meth)) {
|
|
58
|
+
return (this._defaultCommand = meth === ":none" ? "help" : toS(meth));
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
if (!Object.hasOwn(this, "_defaultCommand") || !rtest(this._defaultCommand)) {
|
|
62
|
+
this._defaultCommand = this.fromSuperclass("defaultCommand", "help");
|
|
63
|
+
}
|
|
64
|
+
return this._defaultCommand;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
static register(klass, subcommandName, usage, description, options = {}) {
|
|
68
|
+
if (klass === Thor.Group || klass.prototype instanceof Thor.Group) {
|
|
69
|
+
this.desc(usage, description, options);
|
|
70
|
+
Object.defineProperty(this.prototype, subcommandName, {
|
|
71
|
+
value: function (...args) {
|
|
72
|
+
return this.invoke(klass, args);
|
|
73
|
+
},
|
|
74
|
+
writable: true,
|
|
75
|
+
configurable: true,
|
|
76
|
+
});
|
|
77
|
+
rbClearMethodCache();
|
|
78
|
+
this.methodAdded(subcommandName);
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
this.desc(usage, description, options);
|
|
82
|
+
this.subcommand(subcommandName, klass);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
static desc(usage, description, options = {}) {
|
|
86
|
+
if (rtest(options.for)) {
|
|
87
|
+
const command = this.findAndRefreshCommand(options.for);
|
|
88
|
+
if (rtest(usage))
|
|
89
|
+
command.usage = usage;
|
|
90
|
+
if (rtest(description))
|
|
91
|
+
command.description = description;
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
this._usage = usage;
|
|
95
|
+
this._desc = description;
|
|
96
|
+
this._hide = rtest(options.hide) ? options.hide : false;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
static longDesc(longDescription, options = {}) {
|
|
100
|
+
if (rtest(options.for)) {
|
|
101
|
+
const command = this.findAndRefreshCommand(options.for);
|
|
102
|
+
if (rtest(longDescription))
|
|
103
|
+
command.longDescription = longDescription;
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
this._longDesc = longDescription;
|
|
107
|
+
this._longDescWrap = options.wrap !== false;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
static map(mappings = null, kw = {}) {
|
|
111
|
+
if (!Object.hasOwn(this, "_map") || !rtest(this._map)) {
|
|
112
|
+
this._map = this.fromSuperclass("map", Object.create(null));
|
|
113
|
+
}
|
|
114
|
+
if (rtest(mappings) && Object.keys(kw).length !== 0) {
|
|
115
|
+
mappings = mergeBang(new Map(Object.entries(kw)), mappings);
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
mappings ||= kw;
|
|
119
|
+
}
|
|
120
|
+
if (rtest(mappings)) {
|
|
121
|
+
eachPair(mappings, (key, value) => {
|
|
122
|
+
if (rbObjRespondTo(key, "each")) {
|
|
123
|
+
each(key, (subkey) => (this._map[subkey] = value));
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
this._map[key] = value;
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
return this._map;
|
|
131
|
+
}
|
|
132
|
+
static methodOptions(options = null) {
|
|
133
|
+
if (!Object.hasOwn(this, "_methodOptions") || !rtest(this._methodOptions)) {
|
|
134
|
+
this._methodOptions = {};
|
|
135
|
+
}
|
|
136
|
+
if (rtest(options))
|
|
137
|
+
this.buildOptions(options, this._methodOptions);
|
|
138
|
+
return this._methodOptions;
|
|
139
|
+
}
|
|
140
|
+
static methodOption(name, options = {}) {
|
|
141
|
+
if (typeof name !== "string") {
|
|
142
|
+
throw new ArgumentError(`Expected a Symbol or String, got ${rbInspect(name)}`);
|
|
143
|
+
}
|
|
144
|
+
const scope = rtest(options.for)
|
|
145
|
+
? this.findAndRefreshCommand(options.for).options
|
|
146
|
+
: this.methodOptions();
|
|
147
|
+
return this.buildOption(name, options, scope);
|
|
148
|
+
}
|
|
149
|
+
static methodExclusive(...args) {
|
|
150
|
+
this.registerOptionsRelationFor("methodOptions", "methodExclusiveOptionNames", ...args);
|
|
151
|
+
}
|
|
152
|
+
static methodAtLeastOne(...args) {
|
|
153
|
+
this.registerOptionsRelationFor("methodOptions", "methodAtLeastOneOptionNames", ...args);
|
|
154
|
+
}
|
|
155
|
+
static commandHelp(shell, commandName) {
|
|
156
|
+
const meth = this.normalizeCommandName(commandName);
|
|
157
|
+
const command = this.allCommands()[meth];
|
|
158
|
+
if (!rtest(command))
|
|
159
|
+
this.handleNoCommandError(meth);
|
|
160
|
+
shell.say("Usage:");
|
|
161
|
+
shell.say(` ${stringSplit(this.banner(command), "\n").join("\n ")}`);
|
|
162
|
+
shell.say();
|
|
163
|
+
this.classOptionsHelp(shell, new Map([[null, Object.values(command.options)]]));
|
|
164
|
+
this.printExclusiveOptions(shell, command);
|
|
165
|
+
this.printAtLeastOneRequiredOptions(shell, command);
|
|
166
|
+
if (rtest(command.longDescription)) {
|
|
167
|
+
shell.say("Description:");
|
|
168
|
+
if (rtest(command.wrapLongDescription)) {
|
|
169
|
+
shell.printWrapped(command.longDescription, { indent: 2 });
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
shell.say(command.longDescription);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
shell.say(command.description);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
static async help(shell, subcommand = false) {
|
|
180
|
+
let list = this.printableCommands(true, subcommand);
|
|
181
|
+
for (const klass of thorClassesIn(this)) {
|
|
182
|
+
list = list.concat(await klass.printableCommands(false));
|
|
183
|
+
}
|
|
184
|
+
this.sortCommandsBang(list);
|
|
185
|
+
if (Object.hasOwn(this, "_packageName") && rtest(this._packageName)) {
|
|
186
|
+
shell.say(`${this._packageName} commands:`);
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
shell.say("Commands:");
|
|
190
|
+
}
|
|
191
|
+
shell.printTable(list, { indent: 2, truncate: true });
|
|
192
|
+
shell.say();
|
|
193
|
+
this.classOptionsHelp(shell);
|
|
194
|
+
this.printExclusiveOptions(shell);
|
|
195
|
+
this.printAtLeastOneRequiredOptions(shell);
|
|
196
|
+
}
|
|
197
|
+
static printableCommands(all = true, subcommand = false) {
|
|
198
|
+
return Object.entries(rtest(all) ? this.allCommands() : this.commands())
|
|
199
|
+
.map(([_, command]) => {
|
|
200
|
+
if (command.isHidden())
|
|
201
|
+
return null;
|
|
202
|
+
const item = [];
|
|
203
|
+
item.push(this.banner(command, false, subcommand));
|
|
204
|
+
item.push(rtest(command.description)
|
|
205
|
+
? `# ${command.description.replace(/[ \t\r\n\f\v]+/g, " ")}`
|
|
206
|
+
: "");
|
|
207
|
+
return item;
|
|
208
|
+
})
|
|
209
|
+
.filter((x) => x != null);
|
|
210
|
+
}
|
|
211
|
+
static subcommands() {
|
|
212
|
+
if (!Object.hasOwn(this, "_subcommands") || !rtest(this._subcommands)) {
|
|
213
|
+
this._subcommands = this.fromSuperclass("subcommands", []);
|
|
214
|
+
}
|
|
215
|
+
return this._subcommands;
|
|
216
|
+
}
|
|
217
|
+
static subcommandClasses() {
|
|
218
|
+
if (!Object.hasOwn(this, "_subcommandClasses") || !rtest(this._subcommandClasses)) {
|
|
219
|
+
this._subcommandClasses = {};
|
|
220
|
+
}
|
|
221
|
+
return this._subcommandClasses;
|
|
222
|
+
}
|
|
223
|
+
static subcommand(subcommand, subcommandClass) {
|
|
224
|
+
this.subcommands().push(rbObjAsString(subcommand));
|
|
225
|
+
subcommandClass.subcommandHelp(subcommand);
|
|
226
|
+
this.subcommandClasses()[rbObjAsString(subcommand)] = subcommandClass;
|
|
227
|
+
Object.defineProperty(this.prototype, subcommand, {
|
|
228
|
+
value: function (...args) {
|
|
229
|
+
let opts;
|
|
230
|
+
[args, opts] = Arguments.split(args);
|
|
231
|
+
const invokeArgs = [
|
|
232
|
+
args,
|
|
233
|
+
opts,
|
|
234
|
+
{ invokedViaSubcommand: true, classOptions: this.options },
|
|
235
|
+
];
|
|
236
|
+
if (rtest(aryDelete(opts, "--help")) || rtest(aryDelete(opts, "-h"))) {
|
|
237
|
+
invokeArgs.unshift("help");
|
|
238
|
+
}
|
|
239
|
+
return this.invoke(subcommandClass, ...invokeArgs);
|
|
240
|
+
},
|
|
241
|
+
writable: true,
|
|
242
|
+
configurable: true,
|
|
243
|
+
});
|
|
244
|
+
rbClearMethodCache();
|
|
245
|
+
this.methodAdded(subcommand);
|
|
246
|
+
for (const [, command] of Object.entries(subcommandClass.commands())) {
|
|
247
|
+
command.ancestorName = subcommand;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
static checkUnknownOptionsBang(options = {}) {
|
|
251
|
+
if (!Object.hasOwn(this, "_checkUnknownOptions") || !rtest(this._checkUnknownOptions)) {
|
|
252
|
+
this._checkUnknownOptions = {};
|
|
253
|
+
}
|
|
254
|
+
for (const [key, value] of Object.entries(options)) {
|
|
255
|
+
if (rtest(value)) {
|
|
256
|
+
this._checkUnknownOptions[key] = rbFArray(value);
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
delete this._checkUnknownOptions[key];
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return this._checkUnknownOptions;
|
|
263
|
+
}
|
|
264
|
+
static isCheckUnknownOptions(config) {
|
|
265
|
+
const options = this.checkUnknownOptions();
|
|
266
|
+
if (!rtest(options))
|
|
267
|
+
return false;
|
|
268
|
+
const command = config.currentCommand;
|
|
269
|
+
if (!rtest(command))
|
|
270
|
+
return true;
|
|
271
|
+
const name = command.name;
|
|
272
|
+
if (this.subcommands().includes(name)) {
|
|
273
|
+
return false;
|
|
274
|
+
}
|
|
275
|
+
else if (rtest(options.except)) {
|
|
276
|
+
return !options.except.includes(name);
|
|
277
|
+
}
|
|
278
|
+
else if (rtest(options.only)) {
|
|
279
|
+
return options.only.includes(name);
|
|
280
|
+
}
|
|
281
|
+
else {
|
|
282
|
+
return true;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
static stopOnUnknownOptionBang(...commandNames) {
|
|
286
|
+
return (this._stopOnUnknownOption = [
|
|
287
|
+
...new Set([...this.stopOnUnknownOption(), ...commandNames]),
|
|
288
|
+
]);
|
|
289
|
+
}
|
|
290
|
+
static isStopOnUnknownOption(command) {
|
|
291
|
+
return rtest(command) && this.stopOnUnknownOption().includes(command.name);
|
|
292
|
+
}
|
|
293
|
+
static disableRequiredCheckBang(...commandNames) {
|
|
294
|
+
return (this._disableRequiredCheck = [
|
|
295
|
+
...new Set([...this.disableRequiredCheck(), ...commandNames]),
|
|
296
|
+
]);
|
|
297
|
+
}
|
|
298
|
+
static isDisableRequiredCheck(command) {
|
|
299
|
+
return rtest(command) && this.disableRequiredCheck().includes(command.name);
|
|
300
|
+
}
|
|
301
|
+
static isCommandExists(commandName) {
|
|
302
|
+
return Object.keys(this.commands()).includes(this.normalizeCommandName(commandName));
|
|
303
|
+
}
|
|
304
|
+
/** @internal */
|
|
305
|
+
static methodExclusiveOptionNames() {
|
|
306
|
+
if (!Object.hasOwn(this, "_methodExclusiveOptionNames") ||
|
|
307
|
+
!rtest(this._methodExclusiveOptionNames)) {
|
|
308
|
+
this._methodExclusiveOptionNames = [];
|
|
309
|
+
}
|
|
310
|
+
return this._methodExclusiveOptionNames;
|
|
311
|
+
}
|
|
312
|
+
/** @internal */
|
|
313
|
+
static methodAtLeastOneOptionNames() {
|
|
314
|
+
if (!Object.hasOwn(this, "_methodAtLeastOneOptionNames") ||
|
|
315
|
+
!rtest(this._methodAtLeastOneOptionNames)) {
|
|
316
|
+
this._methodAtLeastOneOptionNames = [];
|
|
317
|
+
}
|
|
318
|
+
return this._methodAtLeastOneOptionNames;
|
|
319
|
+
}
|
|
320
|
+
/** @internal */
|
|
321
|
+
static stopOnUnknownOption() {
|
|
322
|
+
if (!Object.hasOwn(this, "_stopOnUnknownOption") || !rtest(this._stopOnUnknownOption)) {
|
|
323
|
+
this._stopOnUnknownOption = [];
|
|
324
|
+
}
|
|
325
|
+
return this._stopOnUnknownOption;
|
|
326
|
+
}
|
|
327
|
+
/** @internal */
|
|
328
|
+
static disableRequiredCheck() {
|
|
329
|
+
if (!Object.hasOwn(this, "_disableRequiredCheck") || !rtest(this._disableRequiredCheck)) {
|
|
330
|
+
this._disableRequiredCheck = ["help"];
|
|
331
|
+
}
|
|
332
|
+
return this._disableRequiredCheck;
|
|
333
|
+
}
|
|
334
|
+
/** @internal */
|
|
335
|
+
static printExclusiveOptions(shell, command = null) {
|
|
336
|
+
let opts = [];
|
|
337
|
+
if (!(command == null))
|
|
338
|
+
opts = command.methodExclusiveOptionNames();
|
|
339
|
+
opts = opts.concat(this.classExclusiveOptionNames());
|
|
340
|
+
if (opts.length !== 0) {
|
|
341
|
+
shell.say("Exclusive Options:");
|
|
342
|
+
shell.printTable(opts.map((ex) => ex.map((e) => `--${e}`)), { indent: 2 });
|
|
343
|
+
shell.say();
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
/** @internal */
|
|
347
|
+
static printAtLeastOneRequiredOptions(shell, command = null) {
|
|
348
|
+
let opts = [];
|
|
349
|
+
if (!(command == null))
|
|
350
|
+
opts = command.methodAtLeastOneOptionNames();
|
|
351
|
+
opts = opts.concat(this.classAtLeastOneOptionNames());
|
|
352
|
+
if (opts.length !== 0) {
|
|
353
|
+
shell.say("Required At Least One:");
|
|
354
|
+
shell.printTable(opts.map((ex) => ex.map((e) => `--${e}`)), { indent: 2 });
|
|
355
|
+
shell.say();
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
/** @internal */
|
|
359
|
+
static async dispatch(meth, givenArgs, givenOpts, config, block) {
|
|
360
|
+
if (!rtest(meth))
|
|
361
|
+
meth = this.retrieveCommandName(givenArgs);
|
|
362
|
+
let command = this.allCommands()[this.normalizeCommandName(meth)];
|
|
363
|
+
if (!rtest(command) && rtest(config.invokedViaSubcommand)) {
|
|
364
|
+
givenArgs.unshift(meth);
|
|
365
|
+
command = this.allCommands()[this.normalizeCommandName(this.defaultCommand())];
|
|
366
|
+
}
|
|
367
|
+
let args;
|
|
368
|
+
let opts;
|
|
369
|
+
if (rtest(command)) {
|
|
370
|
+
[args, opts] = Options.split(givenArgs);
|
|
371
|
+
if (rtest(this.isStopOnUnknownOption(command)) && args.length !== 0) {
|
|
372
|
+
opts.forEach((opt) => args.push(opt));
|
|
373
|
+
opts.length = 0;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
else {
|
|
377
|
+
args = givenArgs;
|
|
378
|
+
opts = null;
|
|
379
|
+
command = new (this.dynamicCommandClass())(meth);
|
|
380
|
+
}
|
|
381
|
+
opts = givenOpts || opts || [];
|
|
382
|
+
config.currentCommand = command;
|
|
383
|
+
config.commandOptions = command.options;
|
|
384
|
+
const instance = new this(args, opts, config);
|
|
385
|
+
if (block !== undefined)
|
|
386
|
+
block(instance);
|
|
387
|
+
args = instance.args;
|
|
388
|
+
const trailing = arySlice(args, new Range(this.arguments().length, -1));
|
|
389
|
+
return await instance.invokeCommand(command, trailing || []);
|
|
390
|
+
}
|
|
391
|
+
/** @internal */
|
|
392
|
+
static banner(command, namespace = null, subcommand = false) {
|
|
393
|
+
return stringSplit(command.formattedUsage(this, rbGvGet("$thor_runner"), subcommand), "\n")
|
|
394
|
+
.map((formattedUsage) => `${this.basename() ?? ""} ${formattedUsage}`)
|
|
395
|
+
.join("\n");
|
|
396
|
+
}
|
|
397
|
+
/** @internal */
|
|
398
|
+
static baseclass() {
|
|
399
|
+
return Thor;
|
|
400
|
+
}
|
|
401
|
+
/** @internal */
|
|
402
|
+
static dynamicCommandClass() {
|
|
403
|
+
return DynamicCommand;
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* @internal
|
|
407
|
+
* @inventedArm kebabCase — PERMANENT
|
|
408
|
+
*/
|
|
409
|
+
static createCommand(meth) {
|
|
410
|
+
if (!Object.hasOwn(this, "_usage") || !rtest(this._usage))
|
|
411
|
+
this._usage = null;
|
|
412
|
+
if (!Object.hasOwn(this, "_desc") || !rtest(this._desc))
|
|
413
|
+
this._desc = null;
|
|
414
|
+
if (!Object.hasOwn(this, "_longDesc") || !rtest(this._longDesc))
|
|
415
|
+
this._longDesc = null;
|
|
416
|
+
if (!Object.hasOwn(this, "_longDescWrap") || !rtest(this._longDescWrap)) {
|
|
417
|
+
this._longDescWrap = null;
|
|
418
|
+
}
|
|
419
|
+
if (!Object.hasOwn(this, "_hide") || !rtest(this._hide))
|
|
420
|
+
this._hide = null;
|
|
421
|
+
if (rtest(this._usage) && rtest(this._desc)) {
|
|
422
|
+
const baseClass = rtest(this._hide) ? HiddenCommand : Command;
|
|
423
|
+
const relations = {
|
|
424
|
+
exclusiveOptionNames: this.methodExclusiveOptionNames(),
|
|
425
|
+
atLeastOneOptionNames: this.methodAtLeastOneOptionNames(),
|
|
426
|
+
};
|
|
427
|
+
this.commands()[kebabCase(meth)] = new baseClass(meth, this._desc, this._longDesc, this._longDescWrap, this._usage, this.methodOptions(), relations);
|
|
428
|
+
this._usage = this._desc = this._longDesc = this._longDescWrap = null;
|
|
429
|
+
this._methodOptions = this._hide = null;
|
|
430
|
+
this._methodExclusiveOptionNames = this._methodAtLeastOneOptionNames = null;
|
|
431
|
+
return true;
|
|
432
|
+
}
|
|
433
|
+
else if (rtest(this.allCommands()[kebabCase(meth)]) || meth === "method_missing") {
|
|
434
|
+
return true;
|
|
435
|
+
}
|
|
436
|
+
else {
|
|
437
|
+
puts.call(STDOUT, `[WARNING] Attempted to create command ${rbInspect(meth)} without usage or description. ` +
|
|
438
|
+
"Call desc if you want this method to be available as command or declare it inside a " +
|
|
439
|
+
`no_commands{} block. Invoked from ${rbInspect(rbFCaller()[1])}.`);
|
|
440
|
+
return false;
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
/** @internal */
|
|
444
|
+
static initializeAdded() {
|
|
445
|
+
Object.assign(this.classOptions(), this.methodOptions());
|
|
446
|
+
this._methodOptions = null;
|
|
447
|
+
}
|
|
448
|
+
/** @internal */
|
|
449
|
+
static retrieveCommandName(args) {
|
|
450
|
+
const meth = args.length !== 0 ? toS(args[0] ?? null) : null;
|
|
451
|
+
if (rtest(meth) && (rtest(this.map()[meth]) || !/^-/m.test(meth))) {
|
|
452
|
+
return args.shift();
|
|
453
|
+
}
|
|
454
|
+
return null;
|
|
455
|
+
}
|
|
456
|
+
/**
|
|
457
|
+
* @internal
|
|
458
|
+
* @inventedArm kebabCase — PERMANENT
|
|
459
|
+
*/
|
|
460
|
+
static normalizeCommandName(meth) {
|
|
461
|
+
if (!rtest(meth))
|
|
462
|
+
return kebabCase(toS(this.defaultCommand()));
|
|
463
|
+
const possibilities = this.findCommandPossibilities(meth);
|
|
464
|
+
if (possibilities.length > 1) {
|
|
465
|
+
throw new AmbiguousTaskError(`Ambiguous command ${meth} matches [${possibilities.join(", ")}]`);
|
|
466
|
+
}
|
|
467
|
+
if (possibilities.length === 0) {
|
|
468
|
+
meth ??= this.defaultCommand();
|
|
469
|
+
}
|
|
470
|
+
else if (rtest(this.map()[meth])) {
|
|
471
|
+
meth = this.map()[meth];
|
|
472
|
+
}
|
|
473
|
+
else {
|
|
474
|
+
meth = possibilities[0] ?? null;
|
|
475
|
+
}
|
|
476
|
+
return kebabCase(toS(meth));
|
|
477
|
+
}
|
|
478
|
+
/**
|
|
479
|
+
* @internal
|
|
480
|
+
* @inventedArm replaceAll — PERMANENT
|
|
481
|
+
*/
|
|
482
|
+
static findCommandPossibilities(meth) {
|
|
483
|
+
const len = toS(meth).length;
|
|
484
|
+
const folded = toS(meth).replaceAll("_", "-");
|
|
485
|
+
const possibilities = sort(Object.keys({ ...this.allCommands(), ...this.map() }).filter((n) => meth === n.slice(0, len) || folded === n.slice(0, len)));
|
|
486
|
+
const uniquePossibilities = [
|
|
487
|
+
...new Set(possibilities.map((k) => (rtest(this.map()[k]) ? this.map()[k] : k))),
|
|
488
|
+
];
|
|
489
|
+
if (possibilities.includes(meth)) {
|
|
490
|
+
return [meth];
|
|
491
|
+
}
|
|
492
|
+
else if (possibilities.includes(folded)) {
|
|
493
|
+
return [folded];
|
|
494
|
+
}
|
|
495
|
+
else if (uniquePossibilities.length === 1) {
|
|
496
|
+
return uniquePossibilities;
|
|
497
|
+
}
|
|
498
|
+
else {
|
|
499
|
+
return possibilities;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
/** @internal */
|
|
503
|
+
static subcommandHelp(cmd) {
|
|
504
|
+
this.desc("help [COMMAND]", "Describe subcommands or one specific subcommand");
|
|
505
|
+
const prototype = this.prototype;
|
|
506
|
+
Object.defineProperty(prototype, "help", {
|
|
507
|
+
value: function (command = null, subcommand = true) {
|
|
508
|
+
return Object.getPrototypeOf(prototype).help.call(this, command, subcommand);
|
|
509
|
+
},
|
|
510
|
+
writable: true,
|
|
511
|
+
configurable: true,
|
|
512
|
+
});
|
|
513
|
+
rbClearMethodCache();
|
|
514
|
+
this.methodAdded("help");
|
|
515
|
+
}
|
|
516
|
+
/** @internal */
|
|
517
|
+
static sortCommandsBang(list) {
|
|
518
|
+
return list.sort((a, b) => rbCmpint(cmp(a[0], b[0]), a[0], b[0]));
|
|
519
|
+
}
|
|
520
|
+
/**
|
|
521
|
+
* @inlinedFrom Thor::Shell#initialize thor/v1.4.0/lib/thor/shell.rb:44-48
|
|
522
|
+
* @inlinedFrom Thor::Invocation#initialize thor/v1.4.0/lib/thor/invocation.rb:23-27
|
|
523
|
+
* @inlinedFrom Thor::Base#initialize thor/v1.4.0/lib/thor/base.rb:53-113
|
|
524
|
+
* @inventedArm if — PERMANENT
|
|
525
|
+
*/
|
|
526
|
+
constructor(args = [], localOptions = {}, config = {}) {
|
|
527
|
+
if (isModuleIncluded(new.target, Actions)) {
|
|
528
|
+
this.behavior = (() => {
|
|
529
|
+
switch (toS(config.behavior)) {
|
|
530
|
+
case "force":
|
|
531
|
+
case "skip":
|
|
532
|
+
this._cleanupOptionsAndSet(localOptions, config.behavior);
|
|
533
|
+
return "invoke";
|
|
534
|
+
case "revoke":
|
|
535
|
+
return "revoke";
|
|
536
|
+
default:
|
|
537
|
+
return "invoke";
|
|
538
|
+
}
|
|
539
|
+
})();
|
|
540
|
+
}
|
|
541
|
+
this._invocations =
|
|
542
|
+
config.invocations ||
|
|
543
|
+
new Hash((h, k) => {
|
|
544
|
+
const v = [];
|
|
545
|
+
h.set(k, v);
|
|
546
|
+
return v;
|
|
547
|
+
});
|
|
548
|
+
this._initializer = [args, localOptions, config];
|
|
549
|
+
const klass = this.constructor;
|
|
550
|
+
let parseOptions = klass.classOptions();
|
|
551
|
+
const commandOptions = hashDelete(config, "commandOptions");
|
|
552
|
+
if (rtest(commandOptions))
|
|
553
|
+
parseOptions = { ...parseOptions, ...commandOptions };
|
|
554
|
+
let arrayOptions;
|
|
555
|
+
let hashOptions;
|
|
556
|
+
if (Array.isArray(localOptions)) {
|
|
557
|
+
arrayOptions = localOptions;
|
|
558
|
+
hashOptions = {};
|
|
559
|
+
}
|
|
560
|
+
else {
|
|
561
|
+
arrayOptions = [];
|
|
562
|
+
hashOptions = localOptions;
|
|
563
|
+
}
|
|
564
|
+
const currentCommand = config.currentCommand ?? null;
|
|
565
|
+
const stopOnUnknown = klass.isStopOnUnknownOption(currentCommand);
|
|
566
|
+
const relations = currentCommand == null
|
|
567
|
+
? { exclusiveOptionNames: [], atLeastOneOptionNames: [] }
|
|
568
|
+
: currentCommand.optionsRelation;
|
|
569
|
+
klass.classExclusiveOptionNames().map((n) => relations.exclusiveOptionNames.push(n));
|
|
570
|
+
klass.classAtLeastOneOptionNames().map((n) => relations.atLeastOneOptionNames.push(n));
|
|
571
|
+
const disableRequiredCheck = klass.isDisableRequiredCheck(currentCommand);
|
|
572
|
+
const opts = new Options(parseOptions, hashOptions, stopOnUnknown, disableRequiredCheck, relations);
|
|
573
|
+
this.options = opts.parse(arrayOptions);
|
|
574
|
+
if (rtest(config.classOptions)) {
|
|
575
|
+
this.options = config.classOptions.merge(this.options);
|
|
576
|
+
}
|
|
577
|
+
if (klass.isCheckUnknownOptions(config))
|
|
578
|
+
opts.checkUnknownBang();
|
|
579
|
+
let toParse = args;
|
|
580
|
+
if (!klass["strictArgsPosition?"](config))
|
|
581
|
+
toParse = toParse.concat(opts.remaining());
|
|
582
|
+
const thorArgs = new Arguments(klass.arguments());
|
|
583
|
+
for (const [k, v] of Object.entries(thorArgs.parse(toParse)))
|
|
584
|
+
rbFSend(this, `${k}=`, v);
|
|
585
|
+
this._args = thorArgs.remaining();
|
|
586
|
+
this.shell = config.shell;
|
|
587
|
+
if (rbObjRespondTo(this.shell, "base"))
|
|
588
|
+
this.shell.base ||= this;
|
|
589
|
+
if (isModuleIncluded(new.target, Actions))
|
|
590
|
+
this.destinationRoot = config.destinationRoot;
|
|
591
|
+
}
|
|
592
|
+
async help(command = null, subcommand = false) {
|
|
593
|
+
const klass = this.constructor;
|
|
594
|
+
const shell = this.shell;
|
|
595
|
+
if (rtest(command)) {
|
|
596
|
+
if (klass.subcommands().includes(command)) {
|
|
597
|
+
await klass.subcommandClasses()[command].help(shell, true);
|
|
598
|
+
}
|
|
599
|
+
else {
|
|
600
|
+
klass.commandHelp(shell, command);
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
else {
|
|
604
|
+
await klass.help(shell, subcommand);
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
rbSetClassPathString(Argument, Thor, "Argument");
|
|
609
|
+
rbSetClassPathString(Arguments, Thor, "Arguments");
|
|
610
|
+
rbSetClassPathString(Option, Thor, "Option");
|
|
611
|
+
rbSetClassPathString(Options, Thor, "Options");
|
|
612
|
+
constSet(Thor, "Command", Command);
|
|
613
|
+
constSet(Thor, "Task", Command);
|
|
614
|
+
constSet(Thor, "HiddenCommand", HiddenCommand);
|
|
615
|
+
constSet(Thor, "HiddenTask", HiddenCommand);
|
|
616
|
+
constSet(Thor, "DynamicCommand", DynamicCommand);
|
|
617
|
+
constSet(Thor, "DynamicTask", DynamicCommand);
|
|
618
|
+
constSet(Thor, "CoreExt", CoreExt);
|
|
619
|
+
constSet(CoreExt, "HashWithIndifferentAccess", HashWithIndifferentAccess);
|
|
620
|
+
Thor.defaultTask = Thor.defaultCommand;
|
|
621
|
+
Thor.options = Thor.methodOptions;
|
|
622
|
+
Thor.option = Thor.methodOption;
|
|
623
|
+
Thor.exclusive = Thor.methodExclusive;
|
|
624
|
+
Thor.atLeastOne = Thor.methodAtLeastOne;
|
|
625
|
+
Thor.subtasks = Thor.subcommands;
|
|
626
|
+
Thor.subtask = Thor.subcommand;
|
|
627
|
+
Thor.createTask = Thor.createCommand;
|
|
628
|
+
Thor.subtaskHelp = Thor.subcommandHelp;
|
|
629
|
+
Thor.taskHelp = Thor.commandHelp;
|
|
630
|
+
Thor.printableTasks = Thor.printableCommands;
|
|
631
|
+
Thor.retrieveTaskName = Thor.retrieveCommandName;
|
|
632
|
+
Thor.normalizeTaskName = Thor.normalizeCommandName;
|
|
633
|
+
Thor.findTaskPossibilities = Thor.findCommandPossibilities;
|
|
634
|
+
include(Thor, Base);
|
|
635
|
+
Thor.map(new Map([[HELP_MAPPINGS, ":help"]]));
|
|
636
|
+
Thor.desc("help [COMMAND]", "Describe available commands or one specific command");
|
|
637
|
+
Thor.methodAdded("help");
|
|
638
|
+
import "./constants.generated.js";
|
|
639
|
+
TopLevel.Thor = Thor;
|
|
640
|
+
//# sourceMappingURL=thor.js.map
|