ya-git-jira 1.2.0 → 1.3.0
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/README.md +22 -3
- package/bin/git-bump.ts +2 -1
- package/bin/git-jira-issue.ts +6 -3
- package/bin/git-jira-issues.ts +2 -1
- package/bin/git-jira-start.ts +2 -1
- package/bin/git-jira.ts +9 -12
- package/bin/git-lab-projects.ts +38 -0
- package/bin/git-lab-whoami.ts +34 -0
- package/bin/git-lab.ts +22 -0
- package/bin/gitj.ts +16 -13
- package/build.ts +17 -0
- package/bun.lockb +0 -0
- package/dist/bin/git-bump.js +79 -110
- package/dist/bin/git-jira-issue.js +55 -75
- package/dist/bin/git-jira-issues.js +74 -95
- package/dist/bin/git-jira-start.js +56 -78
- package/dist/bin/git-jira.js +82 -146
- package/dist/bin/git-lab-mergetrain.js +1906 -0
- package/dist/bin/git-lab-projects.js +1935 -0
- package/dist/bin/git-lab-whoami.js +1932 -0
- package/dist/bin/git-lab.js +1969 -0
- package/dist/bin/gitj.js +306 -93
- package/dist/index.js +106 -47
- package/index.ts +2 -0
- package/lib/git.ts +4 -13
- package/lib/gitlab.ts +86 -0
- package/lib/jira.ts +24 -15
- package/lib/json.ts +6 -0
- package/lib/spawn.ts +30 -4
- package/package.json +14 -7
- package/tests/git.test.ts +25 -0
- package/tests/gitj.test.ts +43 -0
- package/dist/bin/bundo.js +0 -45
|
@@ -0,0 +1,1935 @@
|
|
|
1
|
+
#!/usr/bin/env bun run
|
|
2
|
+
// @bun
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __toESM = (mod, isNodeMode, target) => {
|
|
9
|
+
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
10
|
+
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
11
|
+
for (let key of __getOwnPropNames(mod))
|
|
12
|
+
if (!__hasOwnProp.call(to, key))
|
|
13
|
+
__defProp(to, key, {
|
|
14
|
+
get: () => mod[key],
|
|
15
|
+
enumerable: true
|
|
16
|
+
});
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
20
|
+
var __require = (id) => {
|
|
21
|
+
return import.meta.require(id);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// node_modules/commander/lib/error.js
|
|
25
|
+
var require_error = __commonJS((exports) => {
|
|
26
|
+
class CommanderError extends Error {
|
|
27
|
+
constructor(exitCode, code, message) {
|
|
28
|
+
super(message);
|
|
29
|
+
Error.captureStackTrace(this, this.constructor);
|
|
30
|
+
this.name = this.constructor.name;
|
|
31
|
+
this.code = code;
|
|
32
|
+
this.exitCode = exitCode;
|
|
33
|
+
this.nestedError = undefined;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
class InvalidArgumentError extends CommanderError {
|
|
38
|
+
constructor(message) {
|
|
39
|
+
super(1, "commander.invalidArgument", message);
|
|
40
|
+
Error.captureStackTrace(this, this.constructor);
|
|
41
|
+
this.name = this.constructor.name;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
exports.CommanderError = CommanderError;
|
|
45
|
+
exports.InvalidArgumentError = InvalidArgumentError;
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
// node_modules/commander/lib/argument.js
|
|
49
|
+
var require_argument = __commonJS((exports) => {
|
|
50
|
+
var humanReadableArgName = function(arg) {
|
|
51
|
+
const nameOutput = arg.name() + (arg.variadic === true ? "..." : "");
|
|
52
|
+
return arg.required ? "<" + nameOutput + ">" : "[" + nameOutput + "]";
|
|
53
|
+
};
|
|
54
|
+
var { InvalidArgumentError } = require_error();
|
|
55
|
+
|
|
56
|
+
class Argument {
|
|
57
|
+
constructor(name, description) {
|
|
58
|
+
this.description = description || "";
|
|
59
|
+
this.variadic = false;
|
|
60
|
+
this.parseArg = undefined;
|
|
61
|
+
this.defaultValue = undefined;
|
|
62
|
+
this.defaultValueDescription = undefined;
|
|
63
|
+
this.argChoices = undefined;
|
|
64
|
+
switch (name[0]) {
|
|
65
|
+
case "<":
|
|
66
|
+
this.required = true;
|
|
67
|
+
this._name = name.slice(1, -1);
|
|
68
|
+
break;
|
|
69
|
+
case "[":
|
|
70
|
+
this.required = false;
|
|
71
|
+
this._name = name.slice(1, -1);
|
|
72
|
+
break;
|
|
73
|
+
default:
|
|
74
|
+
this.required = true;
|
|
75
|
+
this._name = name;
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
if (this._name.length > 3 && this._name.slice(-3) === "...") {
|
|
79
|
+
this.variadic = true;
|
|
80
|
+
this._name = this._name.slice(0, -3);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
name() {
|
|
84
|
+
return this._name;
|
|
85
|
+
}
|
|
86
|
+
_concatValue(value, previous) {
|
|
87
|
+
if (previous === this.defaultValue || !Array.isArray(previous)) {
|
|
88
|
+
return [value];
|
|
89
|
+
}
|
|
90
|
+
return previous.concat(value);
|
|
91
|
+
}
|
|
92
|
+
default(value, description) {
|
|
93
|
+
this.defaultValue = value;
|
|
94
|
+
this.defaultValueDescription = description;
|
|
95
|
+
return this;
|
|
96
|
+
}
|
|
97
|
+
argParser(fn) {
|
|
98
|
+
this.parseArg = fn;
|
|
99
|
+
return this;
|
|
100
|
+
}
|
|
101
|
+
choices(values) {
|
|
102
|
+
this.argChoices = values.slice();
|
|
103
|
+
this.parseArg = (arg, previous) => {
|
|
104
|
+
if (!this.argChoices.includes(arg)) {
|
|
105
|
+
throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(", ")}.`);
|
|
106
|
+
}
|
|
107
|
+
if (this.variadic) {
|
|
108
|
+
return this._concatValue(arg, previous);
|
|
109
|
+
}
|
|
110
|
+
return arg;
|
|
111
|
+
};
|
|
112
|
+
return this;
|
|
113
|
+
}
|
|
114
|
+
argRequired() {
|
|
115
|
+
this.required = true;
|
|
116
|
+
return this;
|
|
117
|
+
}
|
|
118
|
+
argOptional() {
|
|
119
|
+
this.required = false;
|
|
120
|
+
return this;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
exports.Argument = Argument;
|
|
124
|
+
exports.humanReadableArgName = humanReadableArgName;
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
// node_modules/commander/lib/help.js
|
|
128
|
+
var require_help = __commonJS((exports) => {
|
|
129
|
+
var { humanReadableArgName } = require_argument();
|
|
130
|
+
|
|
131
|
+
class Help {
|
|
132
|
+
constructor() {
|
|
133
|
+
this.helpWidth = undefined;
|
|
134
|
+
this.sortSubcommands = false;
|
|
135
|
+
this.sortOptions = false;
|
|
136
|
+
this.showGlobalOptions = false;
|
|
137
|
+
}
|
|
138
|
+
visibleCommands(cmd) {
|
|
139
|
+
const visibleCommands = cmd.commands.filter((cmd2) => !cmd2._hidden);
|
|
140
|
+
if (cmd._hasImplicitHelpCommand()) {
|
|
141
|
+
const [, helpName, helpArgs] = cmd._helpCommandnameAndArgs.match(/([^ ]+) *(.*)/);
|
|
142
|
+
const helpCommand = cmd.createCommand(helpName).helpOption(false);
|
|
143
|
+
helpCommand.description(cmd._helpCommandDescription);
|
|
144
|
+
if (helpArgs)
|
|
145
|
+
helpCommand.arguments(helpArgs);
|
|
146
|
+
visibleCommands.push(helpCommand);
|
|
147
|
+
}
|
|
148
|
+
if (this.sortSubcommands) {
|
|
149
|
+
visibleCommands.sort((a, b) => {
|
|
150
|
+
return a.name().localeCompare(b.name());
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
return visibleCommands;
|
|
154
|
+
}
|
|
155
|
+
compareOptions(a, b) {
|
|
156
|
+
const getSortKey = (option) => {
|
|
157
|
+
return option.short ? option.short.replace(/^-/, "") : option.long.replace(/^--/, "");
|
|
158
|
+
};
|
|
159
|
+
return getSortKey(a).localeCompare(getSortKey(b));
|
|
160
|
+
}
|
|
161
|
+
visibleOptions(cmd) {
|
|
162
|
+
const visibleOptions = cmd.options.filter((option) => !option.hidden);
|
|
163
|
+
const showShortHelpFlag = cmd._hasHelpOption && cmd._helpShortFlag && !cmd._findOption(cmd._helpShortFlag);
|
|
164
|
+
const showLongHelpFlag = cmd._hasHelpOption && !cmd._findOption(cmd._helpLongFlag);
|
|
165
|
+
if (showShortHelpFlag || showLongHelpFlag) {
|
|
166
|
+
let helpOption;
|
|
167
|
+
if (!showShortHelpFlag) {
|
|
168
|
+
helpOption = cmd.createOption(cmd._helpLongFlag, cmd._helpDescription);
|
|
169
|
+
} else if (!showLongHelpFlag) {
|
|
170
|
+
helpOption = cmd.createOption(cmd._helpShortFlag, cmd._helpDescription);
|
|
171
|
+
} else {
|
|
172
|
+
helpOption = cmd.createOption(cmd._helpFlags, cmd._helpDescription);
|
|
173
|
+
}
|
|
174
|
+
visibleOptions.push(helpOption);
|
|
175
|
+
}
|
|
176
|
+
if (this.sortOptions) {
|
|
177
|
+
visibleOptions.sort(this.compareOptions);
|
|
178
|
+
}
|
|
179
|
+
return visibleOptions;
|
|
180
|
+
}
|
|
181
|
+
visibleGlobalOptions(cmd) {
|
|
182
|
+
if (!this.showGlobalOptions)
|
|
183
|
+
return [];
|
|
184
|
+
const globalOptions = [];
|
|
185
|
+
for (let parentCmd = cmd.parent;parentCmd; parentCmd = parentCmd.parent) {
|
|
186
|
+
const visibleOptions = parentCmd.options.filter((option) => !option.hidden);
|
|
187
|
+
globalOptions.push(...visibleOptions);
|
|
188
|
+
}
|
|
189
|
+
if (this.sortOptions) {
|
|
190
|
+
globalOptions.sort(this.compareOptions);
|
|
191
|
+
}
|
|
192
|
+
return globalOptions;
|
|
193
|
+
}
|
|
194
|
+
visibleArguments(cmd) {
|
|
195
|
+
if (cmd._argsDescription) {
|
|
196
|
+
cmd._args.forEach((argument) => {
|
|
197
|
+
argument.description = argument.description || cmd._argsDescription[argument.name()] || "";
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
if (cmd._args.find((argument) => argument.description)) {
|
|
201
|
+
return cmd._args;
|
|
202
|
+
}
|
|
203
|
+
return [];
|
|
204
|
+
}
|
|
205
|
+
subcommandTerm(cmd) {
|
|
206
|
+
const args = cmd._args.map((arg) => humanReadableArgName(arg)).join(" ");
|
|
207
|
+
return cmd._name + (cmd._aliases[0] ? "|" + cmd._aliases[0] : "") + (cmd.options.length ? " [options]" : "") + (args ? " " + args : "");
|
|
208
|
+
}
|
|
209
|
+
optionTerm(option) {
|
|
210
|
+
return option.flags;
|
|
211
|
+
}
|
|
212
|
+
argumentTerm(argument) {
|
|
213
|
+
return argument.name();
|
|
214
|
+
}
|
|
215
|
+
longestSubcommandTermLength(cmd, helper) {
|
|
216
|
+
return helper.visibleCommands(cmd).reduce((max, command) => {
|
|
217
|
+
return Math.max(max, helper.subcommandTerm(command).length);
|
|
218
|
+
}, 0);
|
|
219
|
+
}
|
|
220
|
+
longestOptionTermLength(cmd, helper) {
|
|
221
|
+
return helper.visibleOptions(cmd).reduce((max, option) => {
|
|
222
|
+
return Math.max(max, helper.optionTerm(option).length);
|
|
223
|
+
}, 0);
|
|
224
|
+
}
|
|
225
|
+
longestGlobalOptionTermLength(cmd, helper) {
|
|
226
|
+
return helper.visibleGlobalOptions(cmd).reduce((max, option) => {
|
|
227
|
+
return Math.max(max, helper.optionTerm(option).length);
|
|
228
|
+
}, 0);
|
|
229
|
+
}
|
|
230
|
+
longestArgumentTermLength(cmd, helper) {
|
|
231
|
+
return helper.visibleArguments(cmd).reduce((max, argument) => {
|
|
232
|
+
return Math.max(max, helper.argumentTerm(argument).length);
|
|
233
|
+
}, 0);
|
|
234
|
+
}
|
|
235
|
+
commandUsage(cmd) {
|
|
236
|
+
let cmdName = cmd._name;
|
|
237
|
+
if (cmd._aliases[0]) {
|
|
238
|
+
cmdName = cmdName + "|" + cmd._aliases[0];
|
|
239
|
+
}
|
|
240
|
+
let parentCmdNames = "";
|
|
241
|
+
for (let parentCmd = cmd.parent;parentCmd; parentCmd = parentCmd.parent) {
|
|
242
|
+
parentCmdNames = parentCmd.name() + " " + parentCmdNames;
|
|
243
|
+
}
|
|
244
|
+
return parentCmdNames + cmdName + " " + cmd.usage();
|
|
245
|
+
}
|
|
246
|
+
commandDescription(cmd) {
|
|
247
|
+
return cmd.description();
|
|
248
|
+
}
|
|
249
|
+
subcommandDescription(cmd) {
|
|
250
|
+
return cmd.summary() || cmd.description();
|
|
251
|
+
}
|
|
252
|
+
optionDescription(option) {
|
|
253
|
+
const extraInfo = [];
|
|
254
|
+
if (option.argChoices) {
|
|
255
|
+
extraInfo.push(`choices: ${option.argChoices.map((choice) => JSON.stringify(choice)).join(", ")}`);
|
|
256
|
+
}
|
|
257
|
+
if (option.defaultValue !== undefined) {
|
|
258
|
+
const showDefault = option.required || option.optional || option.isBoolean() && typeof option.defaultValue === "boolean";
|
|
259
|
+
if (showDefault) {
|
|
260
|
+
extraInfo.push(`default: ${option.defaultValueDescription || JSON.stringify(option.defaultValue)}`);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
if (option.presetArg !== undefined && option.optional) {
|
|
264
|
+
extraInfo.push(`preset: ${JSON.stringify(option.presetArg)}`);
|
|
265
|
+
}
|
|
266
|
+
if (option.envVar !== undefined) {
|
|
267
|
+
extraInfo.push(`env: ${option.envVar}`);
|
|
268
|
+
}
|
|
269
|
+
if (extraInfo.length > 0) {
|
|
270
|
+
return `${option.description} (${extraInfo.join(", ")})`;
|
|
271
|
+
}
|
|
272
|
+
return option.description;
|
|
273
|
+
}
|
|
274
|
+
argumentDescription(argument) {
|
|
275
|
+
const extraInfo = [];
|
|
276
|
+
if (argument.argChoices) {
|
|
277
|
+
extraInfo.push(`choices: ${argument.argChoices.map((choice) => JSON.stringify(choice)).join(", ")}`);
|
|
278
|
+
}
|
|
279
|
+
if (argument.defaultValue !== undefined) {
|
|
280
|
+
extraInfo.push(`default: ${argument.defaultValueDescription || JSON.stringify(argument.defaultValue)}`);
|
|
281
|
+
}
|
|
282
|
+
if (extraInfo.length > 0) {
|
|
283
|
+
const extraDescripton = `(${extraInfo.join(", ")})`;
|
|
284
|
+
if (argument.description) {
|
|
285
|
+
return `${argument.description} ${extraDescripton}`;
|
|
286
|
+
}
|
|
287
|
+
return extraDescripton;
|
|
288
|
+
}
|
|
289
|
+
return argument.description;
|
|
290
|
+
}
|
|
291
|
+
formatHelp(cmd, helper) {
|
|
292
|
+
const termWidth = helper.padWidth(cmd, helper);
|
|
293
|
+
const helpWidth = helper.helpWidth || 80;
|
|
294
|
+
const itemIndentWidth = 2;
|
|
295
|
+
const itemSeparatorWidth = 2;
|
|
296
|
+
function formatItem(term, description) {
|
|
297
|
+
if (description) {
|
|
298
|
+
const fullText = `${term.padEnd(termWidth + itemSeparatorWidth)}${description}`;
|
|
299
|
+
return helper.wrap(fullText, helpWidth - itemIndentWidth, termWidth + itemSeparatorWidth);
|
|
300
|
+
}
|
|
301
|
+
return term;
|
|
302
|
+
}
|
|
303
|
+
function formatList(textArray) {
|
|
304
|
+
return textArray.join("\n").replace(/^/gm, " ".repeat(itemIndentWidth));
|
|
305
|
+
}
|
|
306
|
+
let output = [`Usage: ${helper.commandUsage(cmd)}`, ""];
|
|
307
|
+
const commandDescription = helper.commandDescription(cmd);
|
|
308
|
+
if (commandDescription.length > 0) {
|
|
309
|
+
output = output.concat([helper.wrap(commandDescription, helpWidth, 0), ""]);
|
|
310
|
+
}
|
|
311
|
+
const argumentList = helper.visibleArguments(cmd).map((argument) => {
|
|
312
|
+
return formatItem(helper.argumentTerm(argument), helper.argumentDescription(argument));
|
|
313
|
+
});
|
|
314
|
+
if (argumentList.length > 0) {
|
|
315
|
+
output = output.concat(["Arguments:", formatList(argumentList), ""]);
|
|
316
|
+
}
|
|
317
|
+
const optionList = helper.visibleOptions(cmd).map((option) => {
|
|
318
|
+
return formatItem(helper.optionTerm(option), helper.optionDescription(option));
|
|
319
|
+
});
|
|
320
|
+
if (optionList.length > 0) {
|
|
321
|
+
output = output.concat(["Options:", formatList(optionList), ""]);
|
|
322
|
+
}
|
|
323
|
+
if (this.showGlobalOptions) {
|
|
324
|
+
const globalOptionList = helper.visibleGlobalOptions(cmd).map((option) => {
|
|
325
|
+
return formatItem(helper.optionTerm(option), helper.optionDescription(option));
|
|
326
|
+
});
|
|
327
|
+
if (globalOptionList.length > 0) {
|
|
328
|
+
output = output.concat(["Global Options:", formatList(globalOptionList), ""]);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
const commandList = helper.visibleCommands(cmd).map((cmd2) => {
|
|
332
|
+
return formatItem(helper.subcommandTerm(cmd2), helper.subcommandDescription(cmd2));
|
|
333
|
+
});
|
|
334
|
+
if (commandList.length > 0) {
|
|
335
|
+
output = output.concat(["Commands:", formatList(commandList), ""]);
|
|
336
|
+
}
|
|
337
|
+
return output.join("\n");
|
|
338
|
+
}
|
|
339
|
+
padWidth(cmd, helper) {
|
|
340
|
+
return Math.max(helper.longestOptionTermLength(cmd, helper), helper.longestGlobalOptionTermLength(cmd, helper), helper.longestSubcommandTermLength(cmd, helper), helper.longestArgumentTermLength(cmd, helper));
|
|
341
|
+
}
|
|
342
|
+
wrap(str, width, indent, minColumnWidth = 40) {
|
|
343
|
+
const indents = " \\f\\t\\v\xA0\u1680\u2000-\u200A\u202F\u205F\u3000\uFEFF";
|
|
344
|
+
const manualIndent = new RegExp(`[\\n][${indents}]+`);
|
|
345
|
+
if (str.match(manualIndent))
|
|
346
|
+
return str;
|
|
347
|
+
const columnWidth = width - indent;
|
|
348
|
+
if (columnWidth < minColumnWidth)
|
|
349
|
+
return str;
|
|
350
|
+
const leadingStr = str.slice(0, indent);
|
|
351
|
+
const columnText = str.slice(indent).replace("\r\n", "\n");
|
|
352
|
+
const indentString = " ".repeat(indent);
|
|
353
|
+
const zeroWidthSpace = "\u200B";
|
|
354
|
+
const breaks = `\\s${zeroWidthSpace}`;
|
|
355
|
+
const regex = new RegExp(`\n|.{1,${columnWidth - 1}}([${breaks}]|\$)|[^${breaks}]+?([${breaks}]|\$)`, "g");
|
|
356
|
+
const lines = columnText.match(regex) || [];
|
|
357
|
+
return leadingStr + lines.map((line, i) => {
|
|
358
|
+
if (line === "\n")
|
|
359
|
+
return "";
|
|
360
|
+
return (i > 0 ? indentString : "") + line.trimEnd();
|
|
361
|
+
}).join("\n");
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
exports.Help = Help;
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
// node_modules/commander/lib/option.js
|
|
368
|
+
var require_option = __commonJS((exports) => {
|
|
369
|
+
var camelcase = function(str) {
|
|
370
|
+
return str.split("-").reduce((str2, word) => {
|
|
371
|
+
return str2 + word[0].toUpperCase() + word.slice(1);
|
|
372
|
+
});
|
|
373
|
+
};
|
|
374
|
+
var splitOptionFlags = function(flags) {
|
|
375
|
+
let shortFlag;
|
|
376
|
+
let longFlag;
|
|
377
|
+
const flagParts = flags.split(/[ |,]+/);
|
|
378
|
+
if (flagParts.length > 1 && !/^[[<]/.test(flagParts[1]))
|
|
379
|
+
shortFlag = flagParts.shift();
|
|
380
|
+
longFlag = flagParts.shift();
|
|
381
|
+
if (!shortFlag && /^-[^-]$/.test(longFlag)) {
|
|
382
|
+
shortFlag = longFlag;
|
|
383
|
+
longFlag = undefined;
|
|
384
|
+
}
|
|
385
|
+
return { shortFlag, longFlag };
|
|
386
|
+
};
|
|
387
|
+
var { InvalidArgumentError } = require_error();
|
|
388
|
+
|
|
389
|
+
class Option {
|
|
390
|
+
constructor(flags, description) {
|
|
391
|
+
this.flags = flags;
|
|
392
|
+
this.description = description || "";
|
|
393
|
+
this.required = flags.includes("<");
|
|
394
|
+
this.optional = flags.includes("[");
|
|
395
|
+
this.variadic = /\w\.\.\.[>\]]$/.test(flags);
|
|
396
|
+
this.mandatory = false;
|
|
397
|
+
const optionFlags = splitOptionFlags(flags);
|
|
398
|
+
this.short = optionFlags.shortFlag;
|
|
399
|
+
this.long = optionFlags.longFlag;
|
|
400
|
+
this.negate = false;
|
|
401
|
+
if (this.long) {
|
|
402
|
+
this.negate = this.long.startsWith("--no-");
|
|
403
|
+
}
|
|
404
|
+
this.defaultValue = undefined;
|
|
405
|
+
this.defaultValueDescription = undefined;
|
|
406
|
+
this.presetArg = undefined;
|
|
407
|
+
this.envVar = undefined;
|
|
408
|
+
this.parseArg = undefined;
|
|
409
|
+
this.hidden = false;
|
|
410
|
+
this.argChoices = undefined;
|
|
411
|
+
this.conflictsWith = [];
|
|
412
|
+
this.implied = undefined;
|
|
413
|
+
}
|
|
414
|
+
default(value, description) {
|
|
415
|
+
this.defaultValue = value;
|
|
416
|
+
this.defaultValueDescription = description;
|
|
417
|
+
return this;
|
|
418
|
+
}
|
|
419
|
+
preset(arg) {
|
|
420
|
+
this.presetArg = arg;
|
|
421
|
+
return this;
|
|
422
|
+
}
|
|
423
|
+
conflicts(names) {
|
|
424
|
+
this.conflictsWith = this.conflictsWith.concat(names);
|
|
425
|
+
return this;
|
|
426
|
+
}
|
|
427
|
+
implies(impliedOptionValues) {
|
|
428
|
+
let newImplied = impliedOptionValues;
|
|
429
|
+
if (typeof impliedOptionValues === "string") {
|
|
430
|
+
newImplied = { [impliedOptionValues]: true };
|
|
431
|
+
}
|
|
432
|
+
this.implied = Object.assign(this.implied || {}, newImplied);
|
|
433
|
+
return this;
|
|
434
|
+
}
|
|
435
|
+
env(name) {
|
|
436
|
+
this.envVar = name;
|
|
437
|
+
return this;
|
|
438
|
+
}
|
|
439
|
+
argParser(fn) {
|
|
440
|
+
this.parseArg = fn;
|
|
441
|
+
return this;
|
|
442
|
+
}
|
|
443
|
+
makeOptionMandatory(mandatory = true) {
|
|
444
|
+
this.mandatory = !!mandatory;
|
|
445
|
+
return this;
|
|
446
|
+
}
|
|
447
|
+
hideHelp(hide = true) {
|
|
448
|
+
this.hidden = !!hide;
|
|
449
|
+
return this;
|
|
450
|
+
}
|
|
451
|
+
_concatValue(value, previous) {
|
|
452
|
+
if (previous === this.defaultValue || !Array.isArray(previous)) {
|
|
453
|
+
return [value];
|
|
454
|
+
}
|
|
455
|
+
return previous.concat(value);
|
|
456
|
+
}
|
|
457
|
+
choices(values) {
|
|
458
|
+
this.argChoices = values.slice();
|
|
459
|
+
this.parseArg = (arg, previous) => {
|
|
460
|
+
if (!this.argChoices.includes(arg)) {
|
|
461
|
+
throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(", ")}.`);
|
|
462
|
+
}
|
|
463
|
+
if (this.variadic) {
|
|
464
|
+
return this._concatValue(arg, previous);
|
|
465
|
+
}
|
|
466
|
+
return arg;
|
|
467
|
+
};
|
|
468
|
+
return this;
|
|
469
|
+
}
|
|
470
|
+
name() {
|
|
471
|
+
if (this.long) {
|
|
472
|
+
return this.long.replace(/^--/, "");
|
|
473
|
+
}
|
|
474
|
+
return this.short.replace(/^-/, "");
|
|
475
|
+
}
|
|
476
|
+
attributeName() {
|
|
477
|
+
return camelcase(this.name().replace(/^no-/, ""));
|
|
478
|
+
}
|
|
479
|
+
is(arg) {
|
|
480
|
+
return this.short === arg || this.long === arg;
|
|
481
|
+
}
|
|
482
|
+
isBoolean() {
|
|
483
|
+
return !this.required && !this.optional && !this.negate;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
class DualOptions {
|
|
488
|
+
constructor(options) {
|
|
489
|
+
this.positiveOptions = new Map;
|
|
490
|
+
this.negativeOptions = new Map;
|
|
491
|
+
this.dualOptions = new Set;
|
|
492
|
+
options.forEach((option) => {
|
|
493
|
+
if (option.negate) {
|
|
494
|
+
this.negativeOptions.set(option.attributeName(), option);
|
|
495
|
+
} else {
|
|
496
|
+
this.positiveOptions.set(option.attributeName(), option);
|
|
497
|
+
}
|
|
498
|
+
});
|
|
499
|
+
this.negativeOptions.forEach((value, key) => {
|
|
500
|
+
if (this.positiveOptions.has(key)) {
|
|
501
|
+
this.dualOptions.add(key);
|
|
502
|
+
}
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
valueFromOption(value, option) {
|
|
506
|
+
const optionKey = option.attributeName();
|
|
507
|
+
if (!this.dualOptions.has(optionKey))
|
|
508
|
+
return true;
|
|
509
|
+
const preset = this.negativeOptions.get(optionKey).presetArg;
|
|
510
|
+
const negativeValue = preset !== undefined ? preset : false;
|
|
511
|
+
return option.negate === (negativeValue === value);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
exports.Option = Option;
|
|
515
|
+
exports.splitOptionFlags = splitOptionFlags;
|
|
516
|
+
exports.DualOptions = DualOptions;
|
|
517
|
+
});
|
|
518
|
+
|
|
519
|
+
// node_modules/commander/lib/suggestSimilar.js
|
|
520
|
+
var require_suggestSimilar = __commonJS((exports) => {
|
|
521
|
+
var editDistance = function(a, b) {
|
|
522
|
+
if (Math.abs(a.length - b.length) > maxDistance)
|
|
523
|
+
return Math.max(a.length, b.length);
|
|
524
|
+
const d = [];
|
|
525
|
+
for (let i = 0;i <= a.length; i++) {
|
|
526
|
+
d[i] = [i];
|
|
527
|
+
}
|
|
528
|
+
for (let j = 0;j <= b.length; j++) {
|
|
529
|
+
d[0][j] = j;
|
|
530
|
+
}
|
|
531
|
+
for (let j = 1;j <= b.length; j++) {
|
|
532
|
+
for (let i = 1;i <= a.length; i++) {
|
|
533
|
+
let cost = 1;
|
|
534
|
+
if (a[i - 1] === b[j - 1]) {
|
|
535
|
+
cost = 0;
|
|
536
|
+
} else {
|
|
537
|
+
cost = 1;
|
|
538
|
+
}
|
|
539
|
+
d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
|
|
540
|
+
if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
|
|
541
|
+
d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + 1);
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
return d[a.length][b.length];
|
|
546
|
+
};
|
|
547
|
+
var suggestSimilar = function(word, candidates) {
|
|
548
|
+
if (!candidates || candidates.length === 0)
|
|
549
|
+
return "";
|
|
550
|
+
candidates = Array.from(new Set(candidates));
|
|
551
|
+
const searchingOptions = word.startsWith("--");
|
|
552
|
+
if (searchingOptions) {
|
|
553
|
+
word = word.slice(2);
|
|
554
|
+
candidates = candidates.map((candidate) => candidate.slice(2));
|
|
555
|
+
}
|
|
556
|
+
let similar = [];
|
|
557
|
+
let bestDistance = maxDistance;
|
|
558
|
+
const minSimilarity = 0.4;
|
|
559
|
+
candidates.forEach((candidate) => {
|
|
560
|
+
if (candidate.length <= 1)
|
|
561
|
+
return;
|
|
562
|
+
const distance = editDistance(word, candidate);
|
|
563
|
+
const length = Math.max(word.length, candidate.length);
|
|
564
|
+
const similarity = (length - distance) / length;
|
|
565
|
+
if (similarity > minSimilarity) {
|
|
566
|
+
if (distance < bestDistance) {
|
|
567
|
+
bestDistance = distance;
|
|
568
|
+
similar = [candidate];
|
|
569
|
+
} else if (distance === bestDistance) {
|
|
570
|
+
similar.push(candidate);
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
});
|
|
574
|
+
similar.sort((a, b) => a.localeCompare(b));
|
|
575
|
+
if (searchingOptions) {
|
|
576
|
+
similar = similar.map((candidate) => `--${candidate}`);
|
|
577
|
+
}
|
|
578
|
+
if (similar.length > 1) {
|
|
579
|
+
return `\n(Did you mean one of ${similar.join(", ")}?)`;
|
|
580
|
+
}
|
|
581
|
+
if (similar.length === 1) {
|
|
582
|
+
return `\n(Did you mean ${similar[0]}?)`;
|
|
583
|
+
}
|
|
584
|
+
return "";
|
|
585
|
+
};
|
|
586
|
+
var maxDistance = 3;
|
|
587
|
+
exports.suggestSimilar = suggestSimilar;
|
|
588
|
+
});
|
|
589
|
+
|
|
590
|
+
// node_modules/commander/lib/command.js
|
|
591
|
+
var require_command = __commonJS((exports) => {
|
|
592
|
+
var outputHelpIfRequested = function(cmd, args) {
|
|
593
|
+
const helpOption = cmd._hasHelpOption && args.find((arg) => arg === cmd._helpLongFlag || arg === cmd._helpShortFlag);
|
|
594
|
+
if (helpOption) {
|
|
595
|
+
cmd.outputHelp();
|
|
596
|
+
cmd._exit(0, "commander.helpDisplayed", "(outputHelp)");
|
|
597
|
+
}
|
|
598
|
+
};
|
|
599
|
+
var incrementNodeInspectorPort = function(args) {
|
|
600
|
+
return args.map((arg) => {
|
|
601
|
+
if (!arg.startsWith("--inspect")) {
|
|
602
|
+
return arg;
|
|
603
|
+
}
|
|
604
|
+
let debugOption;
|
|
605
|
+
let debugHost = "127.0.0.1";
|
|
606
|
+
let debugPort = "9229";
|
|
607
|
+
let match;
|
|
608
|
+
if ((match = arg.match(/^(--inspect(-brk)?)$/)) !== null) {
|
|
609
|
+
debugOption = match[1];
|
|
610
|
+
} else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+)$/)) !== null) {
|
|
611
|
+
debugOption = match[1];
|
|
612
|
+
if (/^\d+$/.test(match[3])) {
|
|
613
|
+
debugPort = match[3];
|
|
614
|
+
} else {
|
|
615
|
+
debugHost = match[3];
|
|
616
|
+
}
|
|
617
|
+
} else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+):(\d+)$/)) !== null) {
|
|
618
|
+
debugOption = match[1];
|
|
619
|
+
debugHost = match[3];
|
|
620
|
+
debugPort = match[4];
|
|
621
|
+
}
|
|
622
|
+
if (debugOption && debugPort !== "0") {
|
|
623
|
+
return `${debugOption}=${debugHost}:${parseInt(debugPort) + 1}`;
|
|
624
|
+
}
|
|
625
|
+
return arg;
|
|
626
|
+
});
|
|
627
|
+
};
|
|
628
|
+
var getCommandAndParents = function(startCommand) {
|
|
629
|
+
const result = [];
|
|
630
|
+
for (let command = startCommand;command; command = command.parent) {
|
|
631
|
+
result.push(command);
|
|
632
|
+
}
|
|
633
|
+
return result;
|
|
634
|
+
};
|
|
635
|
+
var EventEmitter = import.meta.require("events").EventEmitter;
|
|
636
|
+
var childProcess = import.meta.require("child_process");
|
|
637
|
+
var path = import.meta.require("path");
|
|
638
|
+
var fs = import.meta.require("fs");
|
|
639
|
+
var process2 = import.meta.require("process");
|
|
640
|
+
var { Argument, humanReadableArgName } = require_argument();
|
|
641
|
+
var { CommanderError } = require_error();
|
|
642
|
+
var { Help } = require_help();
|
|
643
|
+
var { Option, splitOptionFlags, DualOptions } = require_option();
|
|
644
|
+
var { suggestSimilar } = require_suggestSimilar();
|
|
645
|
+
|
|
646
|
+
class Command extends EventEmitter {
|
|
647
|
+
constructor(name) {
|
|
648
|
+
super();
|
|
649
|
+
this.commands = [];
|
|
650
|
+
this.options = [];
|
|
651
|
+
this.parent = null;
|
|
652
|
+
this._allowUnknownOption = false;
|
|
653
|
+
this._allowExcessArguments = true;
|
|
654
|
+
this._args = [];
|
|
655
|
+
this.args = [];
|
|
656
|
+
this.rawArgs = [];
|
|
657
|
+
this.processedArgs = [];
|
|
658
|
+
this._scriptPath = null;
|
|
659
|
+
this._name = name || "";
|
|
660
|
+
this._optionValues = {};
|
|
661
|
+
this._optionValueSources = {};
|
|
662
|
+
this._storeOptionsAsProperties = false;
|
|
663
|
+
this._actionHandler = null;
|
|
664
|
+
this._executableHandler = false;
|
|
665
|
+
this._executableFile = null;
|
|
666
|
+
this._executableDir = null;
|
|
667
|
+
this._defaultCommandName = null;
|
|
668
|
+
this._exitCallback = null;
|
|
669
|
+
this._aliases = [];
|
|
670
|
+
this._combineFlagAndOptionalValue = true;
|
|
671
|
+
this._description = "";
|
|
672
|
+
this._summary = "";
|
|
673
|
+
this._argsDescription = undefined;
|
|
674
|
+
this._enablePositionalOptions = false;
|
|
675
|
+
this._passThroughOptions = false;
|
|
676
|
+
this._lifeCycleHooks = {};
|
|
677
|
+
this._showHelpAfterError = false;
|
|
678
|
+
this._showSuggestionAfterError = true;
|
|
679
|
+
this._outputConfiguration = {
|
|
680
|
+
writeOut: (str) => process2.stdout.write(str),
|
|
681
|
+
writeErr: (str) => process2.stderr.write(str),
|
|
682
|
+
getOutHelpWidth: () => process2.stdout.isTTY ? process2.stdout.columns : undefined,
|
|
683
|
+
getErrHelpWidth: () => process2.stderr.isTTY ? process2.stderr.columns : undefined,
|
|
684
|
+
outputError: (str, write) => write(str)
|
|
685
|
+
};
|
|
686
|
+
this._hidden = false;
|
|
687
|
+
this._hasHelpOption = true;
|
|
688
|
+
this._helpFlags = "-h, --help";
|
|
689
|
+
this._helpDescription = "display help for command";
|
|
690
|
+
this._helpShortFlag = "-h";
|
|
691
|
+
this._helpLongFlag = "--help";
|
|
692
|
+
this._addImplicitHelpCommand = undefined;
|
|
693
|
+
this._helpCommandName = "help";
|
|
694
|
+
this._helpCommandnameAndArgs = "help [command]";
|
|
695
|
+
this._helpCommandDescription = "display help for command";
|
|
696
|
+
this._helpConfiguration = {};
|
|
697
|
+
}
|
|
698
|
+
copyInheritedSettings(sourceCommand) {
|
|
699
|
+
this._outputConfiguration = sourceCommand._outputConfiguration;
|
|
700
|
+
this._hasHelpOption = sourceCommand._hasHelpOption;
|
|
701
|
+
this._helpFlags = sourceCommand._helpFlags;
|
|
702
|
+
this._helpDescription = sourceCommand._helpDescription;
|
|
703
|
+
this._helpShortFlag = sourceCommand._helpShortFlag;
|
|
704
|
+
this._helpLongFlag = sourceCommand._helpLongFlag;
|
|
705
|
+
this._helpCommandName = sourceCommand._helpCommandName;
|
|
706
|
+
this._helpCommandnameAndArgs = sourceCommand._helpCommandnameAndArgs;
|
|
707
|
+
this._helpCommandDescription = sourceCommand._helpCommandDescription;
|
|
708
|
+
this._helpConfiguration = sourceCommand._helpConfiguration;
|
|
709
|
+
this._exitCallback = sourceCommand._exitCallback;
|
|
710
|
+
this._storeOptionsAsProperties = sourceCommand._storeOptionsAsProperties;
|
|
711
|
+
this._combineFlagAndOptionalValue = sourceCommand._combineFlagAndOptionalValue;
|
|
712
|
+
this._allowExcessArguments = sourceCommand._allowExcessArguments;
|
|
713
|
+
this._enablePositionalOptions = sourceCommand._enablePositionalOptions;
|
|
714
|
+
this._showHelpAfterError = sourceCommand._showHelpAfterError;
|
|
715
|
+
this._showSuggestionAfterError = sourceCommand._showSuggestionAfterError;
|
|
716
|
+
return this;
|
|
717
|
+
}
|
|
718
|
+
command(nameAndArgs, actionOptsOrExecDesc, execOpts) {
|
|
719
|
+
let desc = actionOptsOrExecDesc;
|
|
720
|
+
let opts = execOpts;
|
|
721
|
+
if (typeof desc === "object" && desc !== null) {
|
|
722
|
+
opts = desc;
|
|
723
|
+
desc = null;
|
|
724
|
+
}
|
|
725
|
+
opts = opts || {};
|
|
726
|
+
const [, name, args] = nameAndArgs.match(/([^ ]+) *(.*)/);
|
|
727
|
+
const cmd = this.createCommand(name);
|
|
728
|
+
if (desc) {
|
|
729
|
+
cmd.description(desc);
|
|
730
|
+
cmd._executableHandler = true;
|
|
731
|
+
}
|
|
732
|
+
if (opts.isDefault)
|
|
733
|
+
this._defaultCommandName = cmd._name;
|
|
734
|
+
cmd._hidden = !!(opts.noHelp || opts.hidden);
|
|
735
|
+
cmd._executableFile = opts.executableFile || null;
|
|
736
|
+
if (args)
|
|
737
|
+
cmd.arguments(args);
|
|
738
|
+
this.commands.push(cmd);
|
|
739
|
+
cmd.parent = this;
|
|
740
|
+
cmd.copyInheritedSettings(this);
|
|
741
|
+
if (desc)
|
|
742
|
+
return this;
|
|
743
|
+
return cmd;
|
|
744
|
+
}
|
|
745
|
+
createCommand(name) {
|
|
746
|
+
return new Command(name);
|
|
747
|
+
}
|
|
748
|
+
createHelp() {
|
|
749
|
+
return Object.assign(new Help, this.configureHelp());
|
|
750
|
+
}
|
|
751
|
+
configureHelp(configuration) {
|
|
752
|
+
if (configuration === undefined)
|
|
753
|
+
return this._helpConfiguration;
|
|
754
|
+
this._helpConfiguration = configuration;
|
|
755
|
+
return this;
|
|
756
|
+
}
|
|
757
|
+
configureOutput(configuration) {
|
|
758
|
+
if (configuration === undefined)
|
|
759
|
+
return this._outputConfiguration;
|
|
760
|
+
Object.assign(this._outputConfiguration, configuration);
|
|
761
|
+
return this;
|
|
762
|
+
}
|
|
763
|
+
showHelpAfterError(displayHelp = true) {
|
|
764
|
+
if (typeof displayHelp !== "string")
|
|
765
|
+
displayHelp = !!displayHelp;
|
|
766
|
+
this._showHelpAfterError = displayHelp;
|
|
767
|
+
return this;
|
|
768
|
+
}
|
|
769
|
+
showSuggestionAfterError(displaySuggestion = true) {
|
|
770
|
+
this._showSuggestionAfterError = !!displaySuggestion;
|
|
771
|
+
return this;
|
|
772
|
+
}
|
|
773
|
+
addCommand(cmd, opts) {
|
|
774
|
+
if (!cmd._name) {
|
|
775
|
+
throw new Error(`Command passed to .addCommand() must have a name
|
|
776
|
+
- specify the name in Command constructor or using .name()`);
|
|
777
|
+
}
|
|
778
|
+
opts = opts || {};
|
|
779
|
+
if (opts.isDefault)
|
|
780
|
+
this._defaultCommandName = cmd._name;
|
|
781
|
+
if (opts.noHelp || opts.hidden)
|
|
782
|
+
cmd._hidden = true;
|
|
783
|
+
this.commands.push(cmd);
|
|
784
|
+
cmd.parent = this;
|
|
785
|
+
return this;
|
|
786
|
+
}
|
|
787
|
+
createArgument(name, description) {
|
|
788
|
+
return new Argument(name, description);
|
|
789
|
+
}
|
|
790
|
+
argument(name, description, fn, defaultValue) {
|
|
791
|
+
const argument = this.createArgument(name, description);
|
|
792
|
+
if (typeof fn === "function") {
|
|
793
|
+
argument.default(defaultValue).argParser(fn);
|
|
794
|
+
} else {
|
|
795
|
+
argument.default(fn);
|
|
796
|
+
}
|
|
797
|
+
this.addArgument(argument);
|
|
798
|
+
return this;
|
|
799
|
+
}
|
|
800
|
+
arguments(names) {
|
|
801
|
+
names.trim().split(/ +/).forEach((detail) => {
|
|
802
|
+
this.argument(detail);
|
|
803
|
+
});
|
|
804
|
+
return this;
|
|
805
|
+
}
|
|
806
|
+
addArgument(argument) {
|
|
807
|
+
const previousArgument = this._args.slice(-1)[0];
|
|
808
|
+
if (previousArgument && previousArgument.variadic) {
|
|
809
|
+
throw new Error(`only the last argument can be variadic '${previousArgument.name()}'`);
|
|
810
|
+
}
|
|
811
|
+
if (argument.required && argument.defaultValue !== undefined && argument.parseArg === undefined) {
|
|
812
|
+
throw new Error(`a default value for a required argument is never used: '${argument.name()}'`);
|
|
813
|
+
}
|
|
814
|
+
this._args.push(argument);
|
|
815
|
+
return this;
|
|
816
|
+
}
|
|
817
|
+
addHelpCommand(enableOrNameAndArgs, description) {
|
|
818
|
+
if (enableOrNameAndArgs === false) {
|
|
819
|
+
this._addImplicitHelpCommand = false;
|
|
820
|
+
} else {
|
|
821
|
+
this._addImplicitHelpCommand = true;
|
|
822
|
+
if (typeof enableOrNameAndArgs === "string") {
|
|
823
|
+
this._helpCommandName = enableOrNameAndArgs.split(" ")[0];
|
|
824
|
+
this._helpCommandnameAndArgs = enableOrNameAndArgs;
|
|
825
|
+
}
|
|
826
|
+
this._helpCommandDescription = description || this._helpCommandDescription;
|
|
827
|
+
}
|
|
828
|
+
return this;
|
|
829
|
+
}
|
|
830
|
+
_hasImplicitHelpCommand() {
|
|
831
|
+
if (this._addImplicitHelpCommand === undefined) {
|
|
832
|
+
return this.commands.length && !this._actionHandler && !this._findCommand("help");
|
|
833
|
+
}
|
|
834
|
+
return this._addImplicitHelpCommand;
|
|
835
|
+
}
|
|
836
|
+
hook(event, listener) {
|
|
837
|
+
const allowedValues = ["preSubcommand", "preAction", "postAction"];
|
|
838
|
+
if (!allowedValues.includes(event)) {
|
|
839
|
+
throw new Error(`Unexpected value for event passed to hook : '${event}'.
|
|
840
|
+
Expecting one of '${allowedValues.join("', '")}'`);
|
|
841
|
+
}
|
|
842
|
+
if (this._lifeCycleHooks[event]) {
|
|
843
|
+
this._lifeCycleHooks[event].push(listener);
|
|
844
|
+
} else {
|
|
845
|
+
this._lifeCycleHooks[event] = [listener];
|
|
846
|
+
}
|
|
847
|
+
return this;
|
|
848
|
+
}
|
|
849
|
+
exitOverride(fn) {
|
|
850
|
+
if (fn) {
|
|
851
|
+
this._exitCallback = fn;
|
|
852
|
+
} else {
|
|
853
|
+
this._exitCallback = (err) => {
|
|
854
|
+
if (err.code !== "commander.executeSubCommandAsync") {
|
|
855
|
+
throw err;
|
|
856
|
+
} else {
|
|
857
|
+
}
|
|
858
|
+
};
|
|
859
|
+
}
|
|
860
|
+
return this;
|
|
861
|
+
}
|
|
862
|
+
_exit(exitCode, code, message) {
|
|
863
|
+
if (this._exitCallback) {
|
|
864
|
+
this._exitCallback(new CommanderError(exitCode, code, message));
|
|
865
|
+
}
|
|
866
|
+
process2.exit(exitCode);
|
|
867
|
+
}
|
|
868
|
+
action(fn) {
|
|
869
|
+
const listener = (args) => {
|
|
870
|
+
const expectedArgsCount = this._args.length;
|
|
871
|
+
const actionArgs = args.slice(0, expectedArgsCount);
|
|
872
|
+
if (this._storeOptionsAsProperties) {
|
|
873
|
+
actionArgs[expectedArgsCount] = this;
|
|
874
|
+
} else {
|
|
875
|
+
actionArgs[expectedArgsCount] = this.opts();
|
|
876
|
+
}
|
|
877
|
+
actionArgs.push(this);
|
|
878
|
+
return fn.apply(this, actionArgs);
|
|
879
|
+
};
|
|
880
|
+
this._actionHandler = listener;
|
|
881
|
+
return this;
|
|
882
|
+
}
|
|
883
|
+
createOption(flags, description) {
|
|
884
|
+
return new Option(flags, description);
|
|
885
|
+
}
|
|
886
|
+
addOption(option) {
|
|
887
|
+
const oname = option.name();
|
|
888
|
+
const name = option.attributeName();
|
|
889
|
+
if (option.negate) {
|
|
890
|
+
const positiveLongFlag = option.long.replace(/^--no-/, "--");
|
|
891
|
+
if (!this._findOption(positiveLongFlag)) {
|
|
892
|
+
this.setOptionValueWithSource(name, option.defaultValue === undefined ? true : option.defaultValue, "default");
|
|
893
|
+
}
|
|
894
|
+
} else if (option.defaultValue !== undefined) {
|
|
895
|
+
this.setOptionValueWithSource(name, option.defaultValue, "default");
|
|
896
|
+
}
|
|
897
|
+
this.options.push(option);
|
|
898
|
+
const handleOptionValue = (val, invalidValueMessage, valueSource) => {
|
|
899
|
+
if (val == null && option.presetArg !== undefined) {
|
|
900
|
+
val = option.presetArg;
|
|
901
|
+
}
|
|
902
|
+
const oldValue = this.getOptionValue(name);
|
|
903
|
+
if (val !== null && option.parseArg) {
|
|
904
|
+
try {
|
|
905
|
+
val = option.parseArg(val, oldValue);
|
|
906
|
+
} catch (err) {
|
|
907
|
+
if (err.code === "commander.invalidArgument") {
|
|
908
|
+
const message = `${invalidValueMessage} ${err.message}`;
|
|
909
|
+
this.error(message, { exitCode: err.exitCode, code: err.code });
|
|
910
|
+
}
|
|
911
|
+
throw err;
|
|
912
|
+
}
|
|
913
|
+
} else if (val !== null && option.variadic) {
|
|
914
|
+
val = option._concatValue(val, oldValue);
|
|
915
|
+
}
|
|
916
|
+
if (val == null) {
|
|
917
|
+
if (option.negate) {
|
|
918
|
+
val = false;
|
|
919
|
+
} else if (option.isBoolean() || option.optional) {
|
|
920
|
+
val = true;
|
|
921
|
+
} else {
|
|
922
|
+
val = "";
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
this.setOptionValueWithSource(name, val, valueSource);
|
|
926
|
+
};
|
|
927
|
+
this.on("option:" + oname, (val) => {
|
|
928
|
+
const invalidValueMessage = `error: option '${option.flags}' argument '${val}' is invalid.`;
|
|
929
|
+
handleOptionValue(val, invalidValueMessage, "cli");
|
|
930
|
+
});
|
|
931
|
+
if (option.envVar) {
|
|
932
|
+
this.on("optionEnv:" + oname, (val) => {
|
|
933
|
+
const invalidValueMessage = `error: option '${option.flags}' value '${val}' from env '${option.envVar}' is invalid.`;
|
|
934
|
+
handleOptionValue(val, invalidValueMessage, "env");
|
|
935
|
+
});
|
|
936
|
+
}
|
|
937
|
+
return this;
|
|
938
|
+
}
|
|
939
|
+
_optionEx(config, flags, description, fn, defaultValue) {
|
|
940
|
+
if (typeof flags === "object" && flags instanceof Option) {
|
|
941
|
+
throw new Error("To add an Option object use addOption() instead of option() or requiredOption()");
|
|
942
|
+
}
|
|
943
|
+
const option = this.createOption(flags, description);
|
|
944
|
+
option.makeOptionMandatory(!!config.mandatory);
|
|
945
|
+
if (typeof fn === "function") {
|
|
946
|
+
option.default(defaultValue).argParser(fn);
|
|
947
|
+
} else if (fn instanceof RegExp) {
|
|
948
|
+
const regex = fn;
|
|
949
|
+
fn = (val, def) => {
|
|
950
|
+
const m = regex.exec(val);
|
|
951
|
+
return m ? m[0] : def;
|
|
952
|
+
};
|
|
953
|
+
option.default(defaultValue).argParser(fn);
|
|
954
|
+
} else {
|
|
955
|
+
option.default(fn);
|
|
956
|
+
}
|
|
957
|
+
return this.addOption(option);
|
|
958
|
+
}
|
|
959
|
+
option(flags, description, fn, defaultValue) {
|
|
960
|
+
return this._optionEx({}, flags, description, fn, defaultValue);
|
|
961
|
+
}
|
|
962
|
+
requiredOption(flags, description, fn, defaultValue) {
|
|
963
|
+
return this._optionEx({ mandatory: true }, flags, description, fn, defaultValue);
|
|
964
|
+
}
|
|
965
|
+
combineFlagAndOptionalValue(combine = true) {
|
|
966
|
+
this._combineFlagAndOptionalValue = !!combine;
|
|
967
|
+
return this;
|
|
968
|
+
}
|
|
969
|
+
allowUnknownOption(allowUnknown = true) {
|
|
970
|
+
this._allowUnknownOption = !!allowUnknown;
|
|
971
|
+
return this;
|
|
972
|
+
}
|
|
973
|
+
allowExcessArguments(allowExcess = true) {
|
|
974
|
+
this._allowExcessArguments = !!allowExcess;
|
|
975
|
+
return this;
|
|
976
|
+
}
|
|
977
|
+
enablePositionalOptions(positional = true) {
|
|
978
|
+
this._enablePositionalOptions = !!positional;
|
|
979
|
+
return this;
|
|
980
|
+
}
|
|
981
|
+
passThroughOptions(passThrough = true) {
|
|
982
|
+
this._passThroughOptions = !!passThrough;
|
|
983
|
+
if (!!this.parent && passThrough && !this.parent._enablePositionalOptions) {
|
|
984
|
+
throw new Error("passThroughOptions can not be used without turning on enablePositionalOptions for parent command(s)");
|
|
985
|
+
}
|
|
986
|
+
return this;
|
|
987
|
+
}
|
|
988
|
+
storeOptionsAsProperties(storeAsProperties = true) {
|
|
989
|
+
this._storeOptionsAsProperties = !!storeAsProperties;
|
|
990
|
+
if (this.options.length) {
|
|
991
|
+
throw new Error("call .storeOptionsAsProperties() before adding options");
|
|
992
|
+
}
|
|
993
|
+
return this;
|
|
994
|
+
}
|
|
995
|
+
getOptionValue(key) {
|
|
996
|
+
if (this._storeOptionsAsProperties) {
|
|
997
|
+
return this[key];
|
|
998
|
+
}
|
|
999
|
+
return this._optionValues[key];
|
|
1000
|
+
}
|
|
1001
|
+
setOptionValue(key, value) {
|
|
1002
|
+
return this.setOptionValueWithSource(key, value, undefined);
|
|
1003
|
+
}
|
|
1004
|
+
setOptionValueWithSource(key, value, source) {
|
|
1005
|
+
if (this._storeOptionsAsProperties) {
|
|
1006
|
+
this[key] = value;
|
|
1007
|
+
} else {
|
|
1008
|
+
this._optionValues[key] = value;
|
|
1009
|
+
}
|
|
1010
|
+
this._optionValueSources[key] = source;
|
|
1011
|
+
return this;
|
|
1012
|
+
}
|
|
1013
|
+
getOptionValueSource(key) {
|
|
1014
|
+
return this._optionValueSources[key];
|
|
1015
|
+
}
|
|
1016
|
+
getOptionValueSourceWithGlobals(key) {
|
|
1017
|
+
let source;
|
|
1018
|
+
getCommandAndParents(this).forEach((cmd) => {
|
|
1019
|
+
if (cmd.getOptionValueSource(key) !== undefined) {
|
|
1020
|
+
source = cmd.getOptionValueSource(key);
|
|
1021
|
+
}
|
|
1022
|
+
});
|
|
1023
|
+
return source;
|
|
1024
|
+
}
|
|
1025
|
+
_prepareUserArgs(argv, parseOptions) {
|
|
1026
|
+
if (argv !== undefined && !Array.isArray(argv)) {
|
|
1027
|
+
throw new Error("first parameter to parse must be array or undefined");
|
|
1028
|
+
}
|
|
1029
|
+
parseOptions = parseOptions || {};
|
|
1030
|
+
if (argv === undefined) {
|
|
1031
|
+
argv = process2.argv;
|
|
1032
|
+
if (process2.versions && process2.versions.electron) {
|
|
1033
|
+
parseOptions.from = "electron";
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
this.rawArgs = argv.slice();
|
|
1037
|
+
let userArgs;
|
|
1038
|
+
switch (parseOptions.from) {
|
|
1039
|
+
case undefined:
|
|
1040
|
+
case "node":
|
|
1041
|
+
this._scriptPath = argv[1];
|
|
1042
|
+
userArgs = argv.slice(2);
|
|
1043
|
+
break;
|
|
1044
|
+
case "electron":
|
|
1045
|
+
if (process2.defaultApp) {
|
|
1046
|
+
this._scriptPath = argv[1];
|
|
1047
|
+
userArgs = argv.slice(2);
|
|
1048
|
+
} else {
|
|
1049
|
+
userArgs = argv.slice(1);
|
|
1050
|
+
}
|
|
1051
|
+
break;
|
|
1052
|
+
case "user":
|
|
1053
|
+
userArgs = argv.slice(0);
|
|
1054
|
+
break;
|
|
1055
|
+
default:
|
|
1056
|
+
throw new Error(`unexpected parse option { from: '${parseOptions.from}' }`);
|
|
1057
|
+
}
|
|
1058
|
+
if (!this._name && this._scriptPath)
|
|
1059
|
+
this.nameFromFilename(this._scriptPath);
|
|
1060
|
+
this._name = this._name || "program";
|
|
1061
|
+
return userArgs;
|
|
1062
|
+
}
|
|
1063
|
+
parse(argv, parseOptions) {
|
|
1064
|
+
const userArgs = this._prepareUserArgs(argv, parseOptions);
|
|
1065
|
+
this._parseCommand([], userArgs);
|
|
1066
|
+
return this;
|
|
1067
|
+
}
|
|
1068
|
+
async parseAsync(argv, parseOptions) {
|
|
1069
|
+
const userArgs = this._prepareUserArgs(argv, parseOptions);
|
|
1070
|
+
await this._parseCommand([], userArgs);
|
|
1071
|
+
return this;
|
|
1072
|
+
}
|
|
1073
|
+
_executeSubCommand(subcommand, args) {
|
|
1074
|
+
args = args.slice();
|
|
1075
|
+
let launchWithNode = false;
|
|
1076
|
+
const sourceExt = [".js", ".ts", ".tsx", ".mjs", ".cjs"];
|
|
1077
|
+
function findFile(baseDir, baseName) {
|
|
1078
|
+
const localBin = path.resolve(baseDir, baseName);
|
|
1079
|
+
if (fs.existsSync(localBin))
|
|
1080
|
+
return localBin;
|
|
1081
|
+
if (sourceExt.includes(path.extname(baseName)))
|
|
1082
|
+
return;
|
|
1083
|
+
const foundExt = sourceExt.find((ext) => fs.existsSync(`${localBin}${ext}`));
|
|
1084
|
+
if (foundExt)
|
|
1085
|
+
return `${localBin}${foundExt}`;
|
|
1086
|
+
return;
|
|
1087
|
+
}
|
|
1088
|
+
this._checkForMissingMandatoryOptions();
|
|
1089
|
+
this._checkForConflictingOptions();
|
|
1090
|
+
let executableFile = subcommand._executableFile || `${this._name}-${subcommand._name}`;
|
|
1091
|
+
let executableDir = this._executableDir || "";
|
|
1092
|
+
if (this._scriptPath) {
|
|
1093
|
+
let resolvedScriptPath;
|
|
1094
|
+
try {
|
|
1095
|
+
resolvedScriptPath = fs.realpathSync(this._scriptPath);
|
|
1096
|
+
} catch (err) {
|
|
1097
|
+
resolvedScriptPath = this._scriptPath;
|
|
1098
|
+
}
|
|
1099
|
+
executableDir = path.resolve(path.dirname(resolvedScriptPath), executableDir);
|
|
1100
|
+
}
|
|
1101
|
+
if (executableDir) {
|
|
1102
|
+
let localFile = findFile(executableDir, executableFile);
|
|
1103
|
+
if (!localFile && !subcommand._executableFile && this._scriptPath) {
|
|
1104
|
+
const legacyName = path.basename(this._scriptPath, path.extname(this._scriptPath));
|
|
1105
|
+
if (legacyName !== this._name) {
|
|
1106
|
+
localFile = findFile(executableDir, `${legacyName}-${subcommand._name}`);
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
executableFile = localFile || executableFile;
|
|
1110
|
+
}
|
|
1111
|
+
launchWithNode = sourceExt.includes(path.extname(executableFile));
|
|
1112
|
+
let proc;
|
|
1113
|
+
if (process2.platform !== "win32") {
|
|
1114
|
+
if (launchWithNode) {
|
|
1115
|
+
args.unshift(executableFile);
|
|
1116
|
+
args = incrementNodeInspectorPort(process2.execArgv).concat(args);
|
|
1117
|
+
proc = childProcess.spawn(process2.argv[0], args, { stdio: "inherit" });
|
|
1118
|
+
} else {
|
|
1119
|
+
proc = childProcess.spawn(executableFile, args, { stdio: "inherit" });
|
|
1120
|
+
}
|
|
1121
|
+
} else {
|
|
1122
|
+
args.unshift(executableFile);
|
|
1123
|
+
args = incrementNodeInspectorPort(process2.execArgv).concat(args);
|
|
1124
|
+
proc = childProcess.spawn(process2.execPath, args, { stdio: "inherit" });
|
|
1125
|
+
}
|
|
1126
|
+
if (!proc.killed) {
|
|
1127
|
+
const signals = ["SIGUSR1", "SIGUSR2", "SIGTERM", "SIGINT", "SIGHUP"];
|
|
1128
|
+
signals.forEach((signal) => {
|
|
1129
|
+
process2.on(signal, () => {
|
|
1130
|
+
if (proc.killed === false && proc.exitCode === null) {
|
|
1131
|
+
proc.kill(signal);
|
|
1132
|
+
}
|
|
1133
|
+
});
|
|
1134
|
+
});
|
|
1135
|
+
}
|
|
1136
|
+
const exitCallback = this._exitCallback;
|
|
1137
|
+
if (!exitCallback) {
|
|
1138
|
+
proc.on("close", process2.exit.bind(process2));
|
|
1139
|
+
} else {
|
|
1140
|
+
proc.on("close", () => {
|
|
1141
|
+
exitCallback(new CommanderError(process2.exitCode || 0, "commander.executeSubCommandAsync", "(close)"));
|
|
1142
|
+
});
|
|
1143
|
+
}
|
|
1144
|
+
proc.on("error", (err) => {
|
|
1145
|
+
if (err.code === "ENOENT") {
|
|
1146
|
+
const executableDirMessage = executableDir ? `searched for local subcommand relative to directory '${executableDir}'` : "no directory for search for local subcommand, use .executableDir() to supply a custom directory";
|
|
1147
|
+
const executableMissing = `'${executableFile}' does not exist
|
|
1148
|
+
- if '${subcommand._name}' is not meant to be an executable command, remove description parameter from '.command()' and use '.description()' instead
|
|
1149
|
+
- if the default executable name is not suitable, use the executableFile option to supply a custom name or path
|
|
1150
|
+
- ${executableDirMessage}`;
|
|
1151
|
+
throw new Error(executableMissing);
|
|
1152
|
+
} else if (err.code === "EACCES") {
|
|
1153
|
+
throw new Error(`'${executableFile}' not executable`);
|
|
1154
|
+
}
|
|
1155
|
+
if (!exitCallback) {
|
|
1156
|
+
process2.exit(1);
|
|
1157
|
+
} else {
|
|
1158
|
+
const wrappedError = new CommanderError(1, "commander.executeSubCommandAsync", "(error)");
|
|
1159
|
+
wrappedError.nestedError = err;
|
|
1160
|
+
exitCallback(wrappedError);
|
|
1161
|
+
}
|
|
1162
|
+
});
|
|
1163
|
+
this.runningCommand = proc;
|
|
1164
|
+
}
|
|
1165
|
+
_dispatchSubcommand(commandName, operands, unknown) {
|
|
1166
|
+
const subCommand = this._findCommand(commandName);
|
|
1167
|
+
if (!subCommand)
|
|
1168
|
+
this.help({ error: true });
|
|
1169
|
+
let hookResult;
|
|
1170
|
+
hookResult = this._chainOrCallSubCommandHook(hookResult, subCommand, "preSubcommand");
|
|
1171
|
+
hookResult = this._chainOrCall(hookResult, () => {
|
|
1172
|
+
if (subCommand._executableHandler) {
|
|
1173
|
+
this._executeSubCommand(subCommand, operands.concat(unknown));
|
|
1174
|
+
} else {
|
|
1175
|
+
return subCommand._parseCommand(operands, unknown);
|
|
1176
|
+
}
|
|
1177
|
+
});
|
|
1178
|
+
return hookResult;
|
|
1179
|
+
}
|
|
1180
|
+
_dispatchHelpCommand(subcommandName) {
|
|
1181
|
+
if (!subcommandName) {
|
|
1182
|
+
this.help();
|
|
1183
|
+
}
|
|
1184
|
+
const subCommand = this._findCommand(subcommandName);
|
|
1185
|
+
if (subCommand && !subCommand._executableHandler) {
|
|
1186
|
+
subCommand.help();
|
|
1187
|
+
}
|
|
1188
|
+
return this._dispatchSubcommand(subcommandName, [], [this._helpLongFlag]);
|
|
1189
|
+
}
|
|
1190
|
+
_checkNumberOfArguments() {
|
|
1191
|
+
this._args.forEach((arg, i) => {
|
|
1192
|
+
if (arg.required && this.args[i] == null) {
|
|
1193
|
+
this.missingArgument(arg.name());
|
|
1194
|
+
}
|
|
1195
|
+
});
|
|
1196
|
+
if (this._args.length > 0 && this._args[this._args.length - 1].variadic) {
|
|
1197
|
+
return;
|
|
1198
|
+
}
|
|
1199
|
+
if (this.args.length > this._args.length) {
|
|
1200
|
+
this._excessArguments(this.args);
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1203
|
+
_processArguments() {
|
|
1204
|
+
const myParseArg = (argument, value, previous) => {
|
|
1205
|
+
let parsedValue = value;
|
|
1206
|
+
if (value !== null && argument.parseArg) {
|
|
1207
|
+
try {
|
|
1208
|
+
parsedValue = argument.parseArg(value, previous);
|
|
1209
|
+
} catch (err) {
|
|
1210
|
+
if (err.code === "commander.invalidArgument") {
|
|
1211
|
+
const message = `error: command-argument value '${value}' is invalid for argument '${argument.name()}'. ${err.message}`;
|
|
1212
|
+
this.error(message, { exitCode: err.exitCode, code: err.code });
|
|
1213
|
+
}
|
|
1214
|
+
throw err;
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
1217
|
+
return parsedValue;
|
|
1218
|
+
};
|
|
1219
|
+
this._checkNumberOfArguments();
|
|
1220
|
+
const processedArgs = [];
|
|
1221
|
+
this._args.forEach((declaredArg, index) => {
|
|
1222
|
+
let value = declaredArg.defaultValue;
|
|
1223
|
+
if (declaredArg.variadic) {
|
|
1224
|
+
if (index < this.args.length) {
|
|
1225
|
+
value = this.args.slice(index);
|
|
1226
|
+
if (declaredArg.parseArg) {
|
|
1227
|
+
value = value.reduce((processed, v) => {
|
|
1228
|
+
return myParseArg(declaredArg, v, processed);
|
|
1229
|
+
}, declaredArg.defaultValue);
|
|
1230
|
+
}
|
|
1231
|
+
} else if (value === undefined) {
|
|
1232
|
+
value = [];
|
|
1233
|
+
}
|
|
1234
|
+
} else if (index < this.args.length) {
|
|
1235
|
+
value = this.args[index];
|
|
1236
|
+
if (declaredArg.parseArg) {
|
|
1237
|
+
value = myParseArg(declaredArg, value, declaredArg.defaultValue);
|
|
1238
|
+
}
|
|
1239
|
+
}
|
|
1240
|
+
processedArgs[index] = value;
|
|
1241
|
+
});
|
|
1242
|
+
this.processedArgs = processedArgs;
|
|
1243
|
+
}
|
|
1244
|
+
_chainOrCall(promise, fn) {
|
|
1245
|
+
if (promise && promise.then && typeof promise.then === "function") {
|
|
1246
|
+
return promise.then(() => fn());
|
|
1247
|
+
}
|
|
1248
|
+
return fn();
|
|
1249
|
+
}
|
|
1250
|
+
_chainOrCallHooks(promise, event) {
|
|
1251
|
+
let result = promise;
|
|
1252
|
+
const hooks = [];
|
|
1253
|
+
getCommandAndParents(this).reverse().filter((cmd) => cmd._lifeCycleHooks[event] !== undefined).forEach((hookedCommand) => {
|
|
1254
|
+
hookedCommand._lifeCycleHooks[event].forEach((callback) => {
|
|
1255
|
+
hooks.push({ hookedCommand, callback });
|
|
1256
|
+
});
|
|
1257
|
+
});
|
|
1258
|
+
if (event === "postAction") {
|
|
1259
|
+
hooks.reverse();
|
|
1260
|
+
}
|
|
1261
|
+
hooks.forEach((hookDetail) => {
|
|
1262
|
+
result = this._chainOrCall(result, () => {
|
|
1263
|
+
return hookDetail.callback(hookDetail.hookedCommand, this);
|
|
1264
|
+
});
|
|
1265
|
+
});
|
|
1266
|
+
return result;
|
|
1267
|
+
}
|
|
1268
|
+
_chainOrCallSubCommandHook(promise, subCommand, event) {
|
|
1269
|
+
let result = promise;
|
|
1270
|
+
if (this._lifeCycleHooks[event] !== undefined) {
|
|
1271
|
+
this._lifeCycleHooks[event].forEach((hook) => {
|
|
1272
|
+
result = this._chainOrCall(result, () => {
|
|
1273
|
+
return hook(this, subCommand);
|
|
1274
|
+
});
|
|
1275
|
+
});
|
|
1276
|
+
}
|
|
1277
|
+
return result;
|
|
1278
|
+
}
|
|
1279
|
+
_parseCommand(operands, unknown) {
|
|
1280
|
+
const parsed = this.parseOptions(unknown);
|
|
1281
|
+
this._parseOptionsEnv();
|
|
1282
|
+
this._parseOptionsImplied();
|
|
1283
|
+
operands = operands.concat(parsed.operands);
|
|
1284
|
+
unknown = parsed.unknown;
|
|
1285
|
+
this.args = operands.concat(unknown);
|
|
1286
|
+
if (operands && this._findCommand(operands[0])) {
|
|
1287
|
+
return this._dispatchSubcommand(operands[0], operands.slice(1), unknown);
|
|
1288
|
+
}
|
|
1289
|
+
if (this._hasImplicitHelpCommand() && operands[0] === this._helpCommandName) {
|
|
1290
|
+
return this._dispatchHelpCommand(operands[1]);
|
|
1291
|
+
}
|
|
1292
|
+
if (this._defaultCommandName) {
|
|
1293
|
+
outputHelpIfRequested(this, unknown);
|
|
1294
|
+
return this._dispatchSubcommand(this._defaultCommandName, operands, unknown);
|
|
1295
|
+
}
|
|
1296
|
+
if (this.commands.length && this.args.length === 0 && !this._actionHandler && !this._defaultCommandName) {
|
|
1297
|
+
this.help({ error: true });
|
|
1298
|
+
}
|
|
1299
|
+
outputHelpIfRequested(this, parsed.unknown);
|
|
1300
|
+
this._checkForMissingMandatoryOptions();
|
|
1301
|
+
this._checkForConflictingOptions();
|
|
1302
|
+
const checkForUnknownOptions = () => {
|
|
1303
|
+
if (parsed.unknown.length > 0) {
|
|
1304
|
+
this.unknownOption(parsed.unknown[0]);
|
|
1305
|
+
}
|
|
1306
|
+
};
|
|
1307
|
+
const commandEvent = `command:${this.name()}`;
|
|
1308
|
+
if (this._actionHandler) {
|
|
1309
|
+
checkForUnknownOptions();
|
|
1310
|
+
this._processArguments();
|
|
1311
|
+
let actionResult;
|
|
1312
|
+
actionResult = this._chainOrCallHooks(actionResult, "preAction");
|
|
1313
|
+
actionResult = this._chainOrCall(actionResult, () => this._actionHandler(this.processedArgs));
|
|
1314
|
+
if (this.parent) {
|
|
1315
|
+
actionResult = this._chainOrCall(actionResult, () => {
|
|
1316
|
+
this.parent.emit(commandEvent, operands, unknown);
|
|
1317
|
+
});
|
|
1318
|
+
}
|
|
1319
|
+
actionResult = this._chainOrCallHooks(actionResult, "postAction");
|
|
1320
|
+
return actionResult;
|
|
1321
|
+
}
|
|
1322
|
+
if (this.parent && this.parent.listenerCount(commandEvent)) {
|
|
1323
|
+
checkForUnknownOptions();
|
|
1324
|
+
this._processArguments();
|
|
1325
|
+
this.parent.emit(commandEvent, operands, unknown);
|
|
1326
|
+
} else if (operands.length) {
|
|
1327
|
+
if (this._findCommand("*")) {
|
|
1328
|
+
return this._dispatchSubcommand("*", operands, unknown);
|
|
1329
|
+
}
|
|
1330
|
+
if (this.listenerCount("command:*")) {
|
|
1331
|
+
this.emit("command:*", operands, unknown);
|
|
1332
|
+
} else if (this.commands.length) {
|
|
1333
|
+
this.unknownCommand();
|
|
1334
|
+
} else {
|
|
1335
|
+
checkForUnknownOptions();
|
|
1336
|
+
this._processArguments();
|
|
1337
|
+
}
|
|
1338
|
+
} else if (this.commands.length) {
|
|
1339
|
+
checkForUnknownOptions();
|
|
1340
|
+
this.help({ error: true });
|
|
1341
|
+
} else {
|
|
1342
|
+
checkForUnknownOptions();
|
|
1343
|
+
this._processArguments();
|
|
1344
|
+
}
|
|
1345
|
+
}
|
|
1346
|
+
_findCommand(name) {
|
|
1347
|
+
if (!name)
|
|
1348
|
+
return;
|
|
1349
|
+
return this.commands.find((cmd) => cmd._name === name || cmd._aliases.includes(name));
|
|
1350
|
+
}
|
|
1351
|
+
_findOption(arg) {
|
|
1352
|
+
return this.options.find((option) => option.is(arg));
|
|
1353
|
+
}
|
|
1354
|
+
_checkForMissingMandatoryOptions() {
|
|
1355
|
+
for (let cmd = this;cmd; cmd = cmd.parent) {
|
|
1356
|
+
cmd.options.forEach((anOption) => {
|
|
1357
|
+
if (anOption.mandatory && cmd.getOptionValue(anOption.attributeName()) === undefined) {
|
|
1358
|
+
cmd.missingMandatoryOptionValue(anOption);
|
|
1359
|
+
}
|
|
1360
|
+
});
|
|
1361
|
+
}
|
|
1362
|
+
}
|
|
1363
|
+
_checkForConflictingLocalOptions() {
|
|
1364
|
+
const definedNonDefaultOptions = this.options.filter((option) => {
|
|
1365
|
+
const optionKey = option.attributeName();
|
|
1366
|
+
if (this.getOptionValue(optionKey) === undefined) {
|
|
1367
|
+
return false;
|
|
1368
|
+
}
|
|
1369
|
+
return this.getOptionValueSource(optionKey) !== "default";
|
|
1370
|
+
});
|
|
1371
|
+
const optionsWithConflicting = definedNonDefaultOptions.filter((option) => option.conflictsWith.length > 0);
|
|
1372
|
+
optionsWithConflicting.forEach((option) => {
|
|
1373
|
+
const conflictingAndDefined = definedNonDefaultOptions.find((defined) => option.conflictsWith.includes(defined.attributeName()));
|
|
1374
|
+
if (conflictingAndDefined) {
|
|
1375
|
+
this._conflictingOption(option, conflictingAndDefined);
|
|
1376
|
+
}
|
|
1377
|
+
});
|
|
1378
|
+
}
|
|
1379
|
+
_checkForConflictingOptions() {
|
|
1380
|
+
for (let cmd = this;cmd; cmd = cmd.parent) {
|
|
1381
|
+
cmd._checkForConflictingLocalOptions();
|
|
1382
|
+
}
|
|
1383
|
+
}
|
|
1384
|
+
parseOptions(argv) {
|
|
1385
|
+
const operands = [];
|
|
1386
|
+
const unknown = [];
|
|
1387
|
+
let dest = operands;
|
|
1388
|
+
const args = argv.slice();
|
|
1389
|
+
function maybeOption(arg) {
|
|
1390
|
+
return arg.length > 1 && arg[0] === "-";
|
|
1391
|
+
}
|
|
1392
|
+
let activeVariadicOption = null;
|
|
1393
|
+
while (args.length) {
|
|
1394
|
+
const arg = args.shift();
|
|
1395
|
+
if (arg === "--") {
|
|
1396
|
+
if (dest === unknown)
|
|
1397
|
+
dest.push(arg);
|
|
1398
|
+
dest.push(...args);
|
|
1399
|
+
break;
|
|
1400
|
+
}
|
|
1401
|
+
if (activeVariadicOption && !maybeOption(arg)) {
|
|
1402
|
+
this.emit(`option:${activeVariadicOption.name()}`, arg);
|
|
1403
|
+
continue;
|
|
1404
|
+
}
|
|
1405
|
+
activeVariadicOption = null;
|
|
1406
|
+
if (maybeOption(arg)) {
|
|
1407
|
+
const option = this._findOption(arg);
|
|
1408
|
+
if (option) {
|
|
1409
|
+
if (option.required) {
|
|
1410
|
+
const value = args.shift();
|
|
1411
|
+
if (value === undefined)
|
|
1412
|
+
this.optionMissingArgument(option);
|
|
1413
|
+
this.emit(`option:${option.name()}`, value);
|
|
1414
|
+
} else if (option.optional) {
|
|
1415
|
+
let value = null;
|
|
1416
|
+
if (args.length > 0 && !maybeOption(args[0])) {
|
|
1417
|
+
value = args.shift();
|
|
1418
|
+
}
|
|
1419
|
+
this.emit(`option:${option.name()}`, value);
|
|
1420
|
+
} else {
|
|
1421
|
+
this.emit(`option:${option.name()}`);
|
|
1422
|
+
}
|
|
1423
|
+
activeVariadicOption = option.variadic ? option : null;
|
|
1424
|
+
continue;
|
|
1425
|
+
}
|
|
1426
|
+
}
|
|
1427
|
+
if (arg.length > 2 && arg[0] === "-" && arg[1] !== "-") {
|
|
1428
|
+
const option = this._findOption(`-${arg[1]}`);
|
|
1429
|
+
if (option) {
|
|
1430
|
+
if (option.required || option.optional && this._combineFlagAndOptionalValue) {
|
|
1431
|
+
this.emit(`option:${option.name()}`, arg.slice(2));
|
|
1432
|
+
} else {
|
|
1433
|
+
this.emit(`option:${option.name()}`);
|
|
1434
|
+
args.unshift(`-${arg.slice(2)}`);
|
|
1435
|
+
}
|
|
1436
|
+
continue;
|
|
1437
|
+
}
|
|
1438
|
+
}
|
|
1439
|
+
if (/^--[^=]+=/.test(arg)) {
|
|
1440
|
+
const index = arg.indexOf("=");
|
|
1441
|
+
const option = this._findOption(arg.slice(0, index));
|
|
1442
|
+
if (option && (option.required || option.optional)) {
|
|
1443
|
+
this.emit(`option:${option.name()}`, arg.slice(index + 1));
|
|
1444
|
+
continue;
|
|
1445
|
+
}
|
|
1446
|
+
}
|
|
1447
|
+
if (maybeOption(arg)) {
|
|
1448
|
+
dest = unknown;
|
|
1449
|
+
}
|
|
1450
|
+
if ((this._enablePositionalOptions || this._passThroughOptions) && operands.length === 0 && unknown.length === 0) {
|
|
1451
|
+
if (this._findCommand(arg)) {
|
|
1452
|
+
operands.push(arg);
|
|
1453
|
+
if (args.length > 0)
|
|
1454
|
+
unknown.push(...args);
|
|
1455
|
+
break;
|
|
1456
|
+
} else if (arg === this._helpCommandName && this._hasImplicitHelpCommand()) {
|
|
1457
|
+
operands.push(arg);
|
|
1458
|
+
if (args.length > 0)
|
|
1459
|
+
operands.push(...args);
|
|
1460
|
+
break;
|
|
1461
|
+
} else if (this._defaultCommandName) {
|
|
1462
|
+
unknown.push(arg);
|
|
1463
|
+
if (args.length > 0)
|
|
1464
|
+
unknown.push(...args);
|
|
1465
|
+
break;
|
|
1466
|
+
}
|
|
1467
|
+
}
|
|
1468
|
+
if (this._passThroughOptions) {
|
|
1469
|
+
dest.push(arg);
|
|
1470
|
+
if (args.length > 0)
|
|
1471
|
+
dest.push(...args);
|
|
1472
|
+
break;
|
|
1473
|
+
}
|
|
1474
|
+
dest.push(arg);
|
|
1475
|
+
}
|
|
1476
|
+
return { operands, unknown };
|
|
1477
|
+
}
|
|
1478
|
+
opts() {
|
|
1479
|
+
if (this._storeOptionsAsProperties) {
|
|
1480
|
+
const result = {};
|
|
1481
|
+
const len = this.options.length;
|
|
1482
|
+
for (let i = 0;i < len; i++) {
|
|
1483
|
+
const key = this.options[i].attributeName();
|
|
1484
|
+
result[key] = key === this._versionOptionName ? this._version : this[key];
|
|
1485
|
+
}
|
|
1486
|
+
return result;
|
|
1487
|
+
}
|
|
1488
|
+
return this._optionValues;
|
|
1489
|
+
}
|
|
1490
|
+
optsWithGlobals() {
|
|
1491
|
+
return getCommandAndParents(this).reduce((combinedOptions, cmd) => Object.assign(combinedOptions, cmd.opts()), {});
|
|
1492
|
+
}
|
|
1493
|
+
error(message, errorOptions) {
|
|
1494
|
+
this._outputConfiguration.outputError(`${message}\n`, this._outputConfiguration.writeErr);
|
|
1495
|
+
if (typeof this._showHelpAfterError === "string") {
|
|
1496
|
+
this._outputConfiguration.writeErr(`${this._showHelpAfterError}\n`);
|
|
1497
|
+
} else if (this._showHelpAfterError) {
|
|
1498
|
+
this._outputConfiguration.writeErr("\n");
|
|
1499
|
+
this.outputHelp({ error: true });
|
|
1500
|
+
}
|
|
1501
|
+
const config = errorOptions || {};
|
|
1502
|
+
const exitCode = config.exitCode || 1;
|
|
1503
|
+
const code = config.code || "commander.error";
|
|
1504
|
+
this._exit(exitCode, code, message);
|
|
1505
|
+
}
|
|
1506
|
+
_parseOptionsEnv() {
|
|
1507
|
+
this.options.forEach((option) => {
|
|
1508
|
+
if (option.envVar && (option.envVar in process2.env)) {
|
|
1509
|
+
const optionKey = option.attributeName();
|
|
1510
|
+
if (this.getOptionValue(optionKey) === undefined || ["default", "config", "env"].includes(this.getOptionValueSource(optionKey))) {
|
|
1511
|
+
if (option.required || option.optional) {
|
|
1512
|
+
this.emit(`optionEnv:${option.name()}`, process2.env[option.envVar]);
|
|
1513
|
+
} else {
|
|
1514
|
+
this.emit(`optionEnv:${option.name()}`);
|
|
1515
|
+
}
|
|
1516
|
+
}
|
|
1517
|
+
}
|
|
1518
|
+
});
|
|
1519
|
+
}
|
|
1520
|
+
_parseOptionsImplied() {
|
|
1521
|
+
const dualHelper = new DualOptions(this.options);
|
|
1522
|
+
const hasCustomOptionValue = (optionKey) => {
|
|
1523
|
+
return this.getOptionValue(optionKey) !== undefined && !["default", "implied"].includes(this.getOptionValueSource(optionKey));
|
|
1524
|
+
};
|
|
1525
|
+
this.options.filter((option) => option.implied !== undefined && hasCustomOptionValue(option.attributeName()) && dualHelper.valueFromOption(this.getOptionValue(option.attributeName()), option)).forEach((option) => {
|
|
1526
|
+
Object.keys(option.implied).filter((impliedKey) => !hasCustomOptionValue(impliedKey)).forEach((impliedKey) => {
|
|
1527
|
+
this.setOptionValueWithSource(impliedKey, option.implied[impliedKey], "implied");
|
|
1528
|
+
});
|
|
1529
|
+
});
|
|
1530
|
+
}
|
|
1531
|
+
missingArgument(name) {
|
|
1532
|
+
const message = `error: missing required argument '${name}'`;
|
|
1533
|
+
this.error(message, { code: "commander.missingArgument" });
|
|
1534
|
+
}
|
|
1535
|
+
optionMissingArgument(option) {
|
|
1536
|
+
const message = `error: option '${option.flags}' argument missing`;
|
|
1537
|
+
this.error(message, { code: "commander.optionMissingArgument" });
|
|
1538
|
+
}
|
|
1539
|
+
missingMandatoryOptionValue(option) {
|
|
1540
|
+
const message = `error: required option '${option.flags}' not specified`;
|
|
1541
|
+
this.error(message, { code: "commander.missingMandatoryOptionValue" });
|
|
1542
|
+
}
|
|
1543
|
+
_conflictingOption(option, conflictingOption) {
|
|
1544
|
+
const findBestOptionFromValue = (option2) => {
|
|
1545
|
+
const optionKey = option2.attributeName();
|
|
1546
|
+
const optionValue = this.getOptionValue(optionKey);
|
|
1547
|
+
const negativeOption = this.options.find((target) => target.negate && optionKey === target.attributeName());
|
|
1548
|
+
const positiveOption = this.options.find((target) => !target.negate && optionKey === target.attributeName());
|
|
1549
|
+
if (negativeOption && (negativeOption.presetArg === undefined && optionValue === false || negativeOption.presetArg !== undefined && optionValue === negativeOption.presetArg)) {
|
|
1550
|
+
return negativeOption;
|
|
1551
|
+
}
|
|
1552
|
+
return positiveOption || option2;
|
|
1553
|
+
};
|
|
1554
|
+
const getErrorMessage = (option2) => {
|
|
1555
|
+
const bestOption = findBestOptionFromValue(option2);
|
|
1556
|
+
const optionKey = bestOption.attributeName();
|
|
1557
|
+
const source = this.getOptionValueSource(optionKey);
|
|
1558
|
+
if (source === "env") {
|
|
1559
|
+
return `environment variable '${bestOption.envVar}'`;
|
|
1560
|
+
}
|
|
1561
|
+
return `option '${bestOption.flags}'`;
|
|
1562
|
+
};
|
|
1563
|
+
const message = `error: ${getErrorMessage(option)} cannot be used with ${getErrorMessage(conflictingOption)}`;
|
|
1564
|
+
this.error(message, { code: "commander.conflictingOption" });
|
|
1565
|
+
}
|
|
1566
|
+
unknownOption(flag) {
|
|
1567
|
+
if (this._allowUnknownOption)
|
|
1568
|
+
return;
|
|
1569
|
+
let suggestion = "";
|
|
1570
|
+
if (flag.startsWith("--") && this._showSuggestionAfterError) {
|
|
1571
|
+
let candidateFlags = [];
|
|
1572
|
+
let command = this;
|
|
1573
|
+
do {
|
|
1574
|
+
const moreFlags = command.createHelp().visibleOptions(command).filter((option) => option.long).map((option) => option.long);
|
|
1575
|
+
candidateFlags = candidateFlags.concat(moreFlags);
|
|
1576
|
+
command = command.parent;
|
|
1577
|
+
} while (command && !command._enablePositionalOptions);
|
|
1578
|
+
suggestion = suggestSimilar(flag, candidateFlags);
|
|
1579
|
+
}
|
|
1580
|
+
const message = `error: unknown option '${flag}'${suggestion}`;
|
|
1581
|
+
this.error(message, { code: "commander.unknownOption" });
|
|
1582
|
+
}
|
|
1583
|
+
_excessArguments(receivedArgs) {
|
|
1584
|
+
if (this._allowExcessArguments)
|
|
1585
|
+
return;
|
|
1586
|
+
const expected = this._args.length;
|
|
1587
|
+
const s = expected === 1 ? "" : "s";
|
|
1588
|
+
const forSubcommand = this.parent ? ` for '${this.name()}'` : "";
|
|
1589
|
+
const message = `error: too many arguments${forSubcommand}. Expected ${expected} argument${s} but got ${receivedArgs.length}.`;
|
|
1590
|
+
this.error(message, { code: "commander.excessArguments" });
|
|
1591
|
+
}
|
|
1592
|
+
unknownCommand() {
|
|
1593
|
+
const unknownName = this.args[0];
|
|
1594
|
+
let suggestion = "";
|
|
1595
|
+
if (this._showSuggestionAfterError) {
|
|
1596
|
+
const candidateNames = [];
|
|
1597
|
+
this.createHelp().visibleCommands(this).forEach((command) => {
|
|
1598
|
+
candidateNames.push(command.name());
|
|
1599
|
+
if (command.alias())
|
|
1600
|
+
candidateNames.push(command.alias());
|
|
1601
|
+
});
|
|
1602
|
+
suggestion = suggestSimilar(unknownName, candidateNames);
|
|
1603
|
+
}
|
|
1604
|
+
const message = `error: unknown command '${unknownName}'${suggestion}`;
|
|
1605
|
+
this.error(message, { code: "commander.unknownCommand" });
|
|
1606
|
+
}
|
|
1607
|
+
version(str, flags, description) {
|
|
1608
|
+
if (str === undefined)
|
|
1609
|
+
return this._version;
|
|
1610
|
+
this._version = str;
|
|
1611
|
+
flags = flags || "-V, --version";
|
|
1612
|
+
description = description || "output the version number";
|
|
1613
|
+
const versionOption = this.createOption(flags, description);
|
|
1614
|
+
this._versionOptionName = versionOption.attributeName();
|
|
1615
|
+
this.options.push(versionOption);
|
|
1616
|
+
this.on("option:" + versionOption.name(), () => {
|
|
1617
|
+
this._outputConfiguration.writeOut(`${str}\n`);
|
|
1618
|
+
this._exit(0, "commander.version", str);
|
|
1619
|
+
});
|
|
1620
|
+
return this;
|
|
1621
|
+
}
|
|
1622
|
+
description(str, argsDescription) {
|
|
1623
|
+
if (str === undefined && argsDescription === undefined)
|
|
1624
|
+
return this._description;
|
|
1625
|
+
this._description = str;
|
|
1626
|
+
if (argsDescription) {
|
|
1627
|
+
this._argsDescription = argsDescription;
|
|
1628
|
+
}
|
|
1629
|
+
return this;
|
|
1630
|
+
}
|
|
1631
|
+
summary(str) {
|
|
1632
|
+
if (str === undefined)
|
|
1633
|
+
return this._summary;
|
|
1634
|
+
this._summary = str;
|
|
1635
|
+
return this;
|
|
1636
|
+
}
|
|
1637
|
+
alias(alias) {
|
|
1638
|
+
if (alias === undefined)
|
|
1639
|
+
return this._aliases[0];
|
|
1640
|
+
let command = this;
|
|
1641
|
+
if (this.commands.length !== 0 && this.commands[this.commands.length - 1]._executableHandler) {
|
|
1642
|
+
command = this.commands[this.commands.length - 1];
|
|
1643
|
+
}
|
|
1644
|
+
if (alias === command._name)
|
|
1645
|
+
throw new Error("Command alias can\'t be the same as its name");
|
|
1646
|
+
command._aliases.push(alias);
|
|
1647
|
+
return this;
|
|
1648
|
+
}
|
|
1649
|
+
aliases(aliases) {
|
|
1650
|
+
if (aliases === undefined)
|
|
1651
|
+
return this._aliases;
|
|
1652
|
+
aliases.forEach((alias) => this.alias(alias));
|
|
1653
|
+
return this;
|
|
1654
|
+
}
|
|
1655
|
+
usage(str) {
|
|
1656
|
+
if (str === undefined) {
|
|
1657
|
+
if (this._usage)
|
|
1658
|
+
return this._usage;
|
|
1659
|
+
const args = this._args.map((arg) => {
|
|
1660
|
+
return humanReadableArgName(arg);
|
|
1661
|
+
});
|
|
1662
|
+
return [].concat(this.options.length || this._hasHelpOption ? "[options]" : [], this.commands.length ? "[command]" : [], this._args.length ? args : []).join(" ");
|
|
1663
|
+
}
|
|
1664
|
+
this._usage = str;
|
|
1665
|
+
return this;
|
|
1666
|
+
}
|
|
1667
|
+
name(str) {
|
|
1668
|
+
if (str === undefined)
|
|
1669
|
+
return this._name;
|
|
1670
|
+
this._name = str;
|
|
1671
|
+
return this;
|
|
1672
|
+
}
|
|
1673
|
+
nameFromFilename(filename) {
|
|
1674
|
+
this._name = path.basename(filename, path.extname(filename));
|
|
1675
|
+
return this;
|
|
1676
|
+
}
|
|
1677
|
+
executableDir(path2) {
|
|
1678
|
+
if (path2 === undefined)
|
|
1679
|
+
return this._executableDir;
|
|
1680
|
+
this._executableDir = path2;
|
|
1681
|
+
return this;
|
|
1682
|
+
}
|
|
1683
|
+
helpInformation(contextOptions) {
|
|
1684
|
+
const helper = this.createHelp();
|
|
1685
|
+
if (helper.helpWidth === undefined) {
|
|
1686
|
+
helper.helpWidth = contextOptions && contextOptions.error ? this._outputConfiguration.getErrHelpWidth() : this._outputConfiguration.getOutHelpWidth();
|
|
1687
|
+
}
|
|
1688
|
+
return helper.formatHelp(this, helper);
|
|
1689
|
+
}
|
|
1690
|
+
_getHelpContext(contextOptions) {
|
|
1691
|
+
contextOptions = contextOptions || {};
|
|
1692
|
+
const context = { error: !!contextOptions.error };
|
|
1693
|
+
let write;
|
|
1694
|
+
if (context.error) {
|
|
1695
|
+
write = (arg) => this._outputConfiguration.writeErr(arg);
|
|
1696
|
+
} else {
|
|
1697
|
+
write = (arg) => this._outputConfiguration.writeOut(arg);
|
|
1698
|
+
}
|
|
1699
|
+
context.write = contextOptions.write || write;
|
|
1700
|
+
context.command = this;
|
|
1701
|
+
return context;
|
|
1702
|
+
}
|
|
1703
|
+
outputHelp(contextOptions) {
|
|
1704
|
+
let deprecatedCallback;
|
|
1705
|
+
if (typeof contextOptions === "function") {
|
|
1706
|
+
deprecatedCallback = contextOptions;
|
|
1707
|
+
contextOptions = undefined;
|
|
1708
|
+
}
|
|
1709
|
+
const context = this._getHelpContext(contextOptions);
|
|
1710
|
+
getCommandAndParents(this).reverse().forEach((command) => command.emit("beforeAllHelp", context));
|
|
1711
|
+
this.emit("beforeHelp", context);
|
|
1712
|
+
let helpInformation = this.helpInformation(context);
|
|
1713
|
+
if (deprecatedCallback) {
|
|
1714
|
+
helpInformation = deprecatedCallback(helpInformation);
|
|
1715
|
+
if (typeof helpInformation !== "string" && !Buffer.isBuffer(helpInformation)) {
|
|
1716
|
+
throw new Error("outputHelp callback must return a string or a Buffer");
|
|
1717
|
+
}
|
|
1718
|
+
}
|
|
1719
|
+
context.write(helpInformation);
|
|
1720
|
+
this.emit(this._helpLongFlag);
|
|
1721
|
+
this.emit("afterHelp", context);
|
|
1722
|
+
getCommandAndParents(this).forEach((command) => command.emit("afterAllHelp", context));
|
|
1723
|
+
}
|
|
1724
|
+
helpOption(flags, description) {
|
|
1725
|
+
if (typeof flags === "boolean") {
|
|
1726
|
+
this._hasHelpOption = flags;
|
|
1727
|
+
return this;
|
|
1728
|
+
}
|
|
1729
|
+
this._helpFlags = flags || this._helpFlags;
|
|
1730
|
+
this._helpDescription = description || this._helpDescription;
|
|
1731
|
+
const helpFlags = splitOptionFlags(this._helpFlags);
|
|
1732
|
+
this._helpShortFlag = helpFlags.shortFlag;
|
|
1733
|
+
this._helpLongFlag = helpFlags.longFlag;
|
|
1734
|
+
return this;
|
|
1735
|
+
}
|
|
1736
|
+
help(contextOptions) {
|
|
1737
|
+
this.outputHelp(contextOptions);
|
|
1738
|
+
let exitCode = process2.exitCode || 0;
|
|
1739
|
+
if (exitCode === 0 && contextOptions && typeof contextOptions !== "function" && contextOptions.error) {
|
|
1740
|
+
exitCode = 1;
|
|
1741
|
+
}
|
|
1742
|
+
this._exit(exitCode, "commander.help", "(outputHelp)");
|
|
1743
|
+
}
|
|
1744
|
+
addHelpText(position, text) {
|
|
1745
|
+
const allowedValues = ["beforeAll", "before", "after", "afterAll"];
|
|
1746
|
+
if (!allowedValues.includes(position)) {
|
|
1747
|
+
throw new Error(`Unexpected value for position to addHelpText.
|
|
1748
|
+
Expecting one of '${allowedValues.join("', '")}'`);
|
|
1749
|
+
}
|
|
1750
|
+
const helpEvent = `${position}Help`;
|
|
1751
|
+
this.on(helpEvent, (context) => {
|
|
1752
|
+
let helpStr;
|
|
1753
|
+
if (typeof text === "function") {
|
|
1754
|
+
helpStr = text({ error: context.error, command: context.command });
|
|
1755
|
+
} else {
|
|
1756
|
+
helpStr = text;
|
|
1757
|
+
}
|
|
1758
|
+
if (helpStr) {
|
|
1759
|
+
context.write(`${helpStr}\n`);
|
|
1760
|
+
}
|
|
1761
|
+
});
|
|
1762
|
+
return this;
|
|
1763
|
+
}
|
|
1764
|
+
}
|
|
1765
|
+
exports.Command = Command;
|
|
1766
|
+
});
|
|
1767
|
+
|
|
1768
|
+
// node_modules/commander/index.js
|
|
1769
|
+
var require_commander = __commonJS((exports, module) => {
|
|
1770
|
+
var { Argument } = require_argument();
|
|
1771
|
+
var { Command } = require_command();
|
|
1772
|
+
var { CommanderError, InvalidArgumentError } = require_error();
|
|
1773
|
+
var { Help } = require_help();
|
|
1774
|
+
var { Option } = require_option();
|
|
1775
|
+
exports = module.exports = new Command;
|
|
1776
|
+
exports.program = exports;
|
|
1777
|
+
exports.Argument = Argument;
|
|
1778
|
+
exports.Command = Command;
|
|
1779
|
+
exports.CommanderError = CommanderError;
|
|
1780
|
+
exports.Help = Help;
|
|
1781
|
+
exports.InvalidArgumentError = InvalidArgumentError;
|
|
1782
|
+
exports.InvalidOptionArgumentError = InvalidArgumentError;
|
|
1783
|
+
exports.Option = Option;
|
|
1784
|
+
});
|
|
1785
|
+
|
|
1786
|
+
// node_modules/commander/esm.mjs
|
|
1787
|
+
var import_ = __toESM(require_commander(), 1);
|
|
1788
|
+
var {
|
|
1789
|
+
program,
|
|
1790
|
+
createCommand,
|
|
1791
|
+
createArgument,
|
|
1792
|
+
createOption,
|
|
1793
|
+
CommanderError,
|
|
1794
|
+
InvalidArgumentError,
|
|
1795
|
+
InvalidOptionArgumentError,
|
|
1796
|
+
Command,
|
|
1797
|
+
Argument,
|
|
1798
|
+
Option,
|
|
1799
|
+
Help
|
|
1800
|
+
} = import_.default;
|
|
1801
|
+
|
|
1802
|
+
// lib/spawn.ts
|
|
1803
|
+
async function spawn(args, options = defaultOptions) {
|
|
1804
|
+
const proc = Bun.spawn(args, { stdout: "pipe", stderr: "pipe" });
|
|
1805
|
+
const stdout = new Response(proc.stdout);
|
|
1806
|
+
const stderr = new Response(proc.stderr);
|
|
1807
|
+
const [out, err, exitCode, signal] = await Promise.all([stdout.text(), stderr.text(), proc.exitCode, proc.signalCode]);
|
|
1808
|
+
let code = 0;
|
|
1809
|
+
if (exitCode !== null) {
|
|
1810
|
+
code = exitCode;
|
|
1811
|
+
}
|
|
1812
|
+
if (!out && !err && !options.expectQuiet) {
|
|
1813
|
+
console.warn(`No output from ${args.join(" ")}`);
|
|
1814
|
+
}
|
|
1815
|
+
return { out: out.trim(), err: err.trim(), code };
|
|
1816
|
+
}
|
|
1817
|
+
async function doCommand(args) {
|
|
1818
|
+
const { out, err } = await spawn(args);
|
|
1819
|
+
if (err)
|
|
1820
|
+
console.error(err);
|
|
1821
|
+
return out;
|
|
1822
|
+
}
|
|
1823
|
+
var defaultOptions = {
|
|
1824
|
+
expectQuiet: false
|
|
1825
|
+
};
|
|
1826
|
+
|
|
1827
|
+
// lib/git.ts
|
|
1828
|
+
async function getConfig(key) {
|
|
1829
|
+
return doCommand(["git", "config", "--get", key]);
|
|
1830
|
+
}
|
|
1831
|
+
async function createBranch(name) {
|
|
1832
|
+
return doCommand(["git", "checkout", "-b", name]);
|
|
1833
|
+
}
|
|
1834
|
+
async function getCurrentBranch() {
|
|
1835
|
+
return doCommand(["git", "rev-parse", "--abbrev-ref", "HEAD"]);
|
|
1836
|
+
}
|
|
1837
|
+
async function getRemote() {
|
|
1838
|
+
return doCommand(["git", "ls-remote", "--get-url", "origin"]);
|
|
1839
|
+
}
|
|
1840
|
+
|
|
1841
|
+
// lib/gitlab.ts
|
|
1842
|
+
import path from "path";
|
|
1843
|
+
async function getGitlabConfig() {
|
|
1844
|
+
const host = await getConfig("gitlab.host");
|
|
1845
|
+
if (!host)
|
|
1846
|
+
throw new Error("gitlab.host not in git config");
|
|
1847
|
+
const user = await getConfig("user.email");
|
|
1848
|
+
if (!user)
|
|
1849
|
+
throw new Error("user.email not in git config");
|
|
1850
|
+
const token = await getConfig("gitlab.token");
|
|
1851
|
+
if (!token)
|
|
1852
|
+
throw new Error("gitlab.token not in git config");
|
|
1853
|
+
return { host, user, token };
|
|
1854
|
+
}
|
|
1855
|
+
async function get(endpoint) {
|
|
1856
|
+
const method = "GET";
|
|
1857
|
+
const { host, token } = await getGitlabConfig();
|
|
1858
|
+
const base = `https://${host}/api/v4`;
|
|
1859
|
+
const uri = `${base}/${endpoint}`;
|
|
1860
|
+
const headers = new Headers;
|
|
1861
|
+
headers.append("Accept", "application/json");
|
|
1862
|
+
headers.append("Private-Token", token);
|
|
1863
|
+
const options = {
|
|
1864
|
+
method,
|
|
1865
|
+
headers
|
|
1866
|
+
};
|
|
1867
|
+
const request = new Request(uri, options);
|
|
1868
|
+
const response = await fetch(request);
|
|
1869
|
+
return await response.json();
|
|
1870
|
+
}
|
|
1871
|
+
async function whoami() {
|
|
1872
|
+
return await get("/user");
|
|
1873
|
+
}
|
|
1874
|
+
async function getProjects(paths) {
|
|
1875
|
+
let search = "";
|
|
1876
|
+
if (paths.length > 0) {
|
|
1877
|
+
search = "&search=" + paths.join(",");
|
|
1878
|
+
}
|
|
1879
|
+
return await get(`/projects?visibility=private&membership=true&simple=true${search}`);
|
|
1880
|
+
}
|
|
1881
|
+
async function findProject(ssh_url) {
|
|
1882
|
+
const parts = ssh_url.split(":");
|
|
1883
|
+
if (parts.length != 2) {
|
|
1884
|
+
throw new Error(`${ssh_url} is invalid, could not be split into two parts at :`);
|
|
1885
|
+
}
|
|
1886
|
+
const name = path.basename(parts[1], ".git");
|
|
1887
|
+
const projects = await getProjects([name]);
|
|
1888
|
+
const project = projects.find((p) => {
|
|
1889
|
+
return p.ssh_url_to_repo === ssh_url;
|
|
1890
|
+
});
|
|
1891
|
+
if (!project) {
|
|
1892
|
+
throw new Error(`No project with ssh_url_to_repo ${ssh_url} found`);
|
|
1893
|
+
}
|
|
1894
|
+
return project;
|
|
1895
|
+
}
|
|
1896
|
+
async function getMergeRequest(id) {
|
|
1897
|
+
return await get(`/merge_requests/${id}`);
|
|
1898
|
+
}
|
|
1899
|
+
|
|
1900
|
+
// lib/is_main.ts
|
|
1901
|
+
import path2 from "path";
|
|
1902
|
+
function isMain(self) {
|
|
1903
|
+
const exe = path2.basename(Bun.argv[1]).split(".")[0];
|
|
1904
|
+
return exe == self || import.meta.main;
|
|
1905
|
+
}
|
|
1906
|
+
|
|
1907
|
+
// bin/git-lab-projects.ts
|
|
1908
|
+
function create() {
|
|
1909
|
+
const program2 = new Command;
|
|
1910
|
+
program2.name("projects").description("List projects for current user").option("-v, --verbose", "Verbose output").argument("[path...]", "Namespace paths to filter by").action(async (paths, options) => {
|
|
1911
|
+
const projects = await getProjects(paths);
|
|
1912
|
+
if (!projects) {
|
|
1913
|
+
console.error(`No projects!`);
|
|
1914
|
+
process.exit(1);
|
|
1915
|
+
}
|
|
1916
|
+
if (options.verbose) {
|
|
1917
|
+
console.log(projects);
|
|
1918
|
+
} else {
|
|
1919
|
+
let filtered = projects.map((p) => {
|
|
1920
|
+
const { id, name, path_with_namespace, ssh_url_to_repo } = p;
|
|
1921
|
+
return { id, name, path_with_namespace, ssh_url_to_repo };
|
|
1922
|
+
});
|
|
1923
|
+
console.log(filtered);
|
|
1924
|
+
}
|
|
1925
|
+
});
|
|
1926
|
+
return program2;
|
|
1927
|
+
}
|
|
1928
|
+
if (isMain("git-lab-projects")) {
|
|
1929
|
+
await create().parseAsync(Bun.argv);
|
|
1930
|
+
}
|
|
1931
|
+
var git_lab_projects_default = create;
|
|
1932
|
+
export {
|
|
1933
|
+
git_lab_projects_default as default,
|
|
1934
|
+
create
|
|
1935
|
+
};
|