codeorbit 0.9.4 → 0.10.1
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/dist/cli.js +392 -3166
- package/dist/cli.js.map +1 -1
- package/dist/index.js +10 -2
- package/dist/index.js.map +1 -1
- package/dist/server.js +10 -2
- package/dist/server.js.map +1 -1
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -1,3057 +1,16 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
2
|
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
9
3
|
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
10
4
|
}) : x)(function(x) {
|
|
11
5
|
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
12
6
|
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
13
7
|
});
|
|
14
|
-
var __commonJS = (cb, mod) => function __require2() {
|
|
15
|
-
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
16
|
-
};
|
|
17
|
-
var __copyProps = (to, from, except, desc) => {
|
|
18
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
19
|
-
for (let key of __getOwnPropNames(from))
|
|
20
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
21
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
22
|
-
}
|
|
23
|
-
return to;
|
|
24
|
-
};
|
|
25
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
26
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
27
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
28
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
29
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
30
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
31
|
-
mod
|
|
32
|
-
));
|
|
33
|
-
|
|
34
|
-
// node_modules/commander/lib/error.js
|
|
35
|
-
var require_error = __commonJS({
|
|
36
|
-
"node_modules/commander/lib/error.js"(exports) {
|
|
37
|
-
"use strict";
|
|
38
|
-
var CommanderError2 = class extends Error {
|
|
39
|
-
/**
|
|
40
|
-
* Constructs the CommanderError class
|
|
41
|
-
* @param {number} exitCode suggested exit code which could be used with process.exit
|
|
42
|
-
* @param {string} code an id string representing the error
|
|
43
|
-
* @param {string} message human-readable description of the error
|
|
44
|
-
*/
|
|
45
|
-
constructor(exitCode, code, message) {
|
|
46
|
-
super(message);
|
|
47
|
-
Error.captureStackTrace(this, this.constructor);
|
|
48
|
-
this.name = this.constructor.name;
|
|
49
|
-
this.code = code;
|
|
50
|
-
this.exitCode = exitCode;
|
|
51
|
-
this.nestedError = void 0;
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
var InvalidArgumentError2 = class extends CommanderError2 {
|
|
55
|
-
/**
|
|
56
|
-
* Constructs the InvalidArgumentError class
|
|
57
|
-
* @param {string} [message] explanation of why argument is invalid
|
|
58
|
-
*/
|
|
59
|
-
constructor(message) {
|
|
60
|
-
super(1, "commander.invalidArgument", message);
|
|
61
|
-
Error.captureStackTrace(this, this.constructor);
|
|
62
|
-
this.name = this.constructor.name;
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
exports.CommanderError = CommanderError2;
|
|
66
|
-
exports.InvalidArgumentError = InvalidArgumentError2;
|
|
67
|
-
}
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
// node_modules/commander/lib/argument.js
|
|
71
|
-
var require_argument = __commonJS({
|
|
72
|
-
"node_modules/commander/lib/argument.js"(exports) {
|
|
73
|
-
"use strict";
|
|
74
|
-
var { InvalidArgumentError: InvalidArgumentError2 } = require_error();
|
|
75
|
-
var Argument2 = class {
|
|
76
|
-
/**
|
|
77
|
-
* Initialize a new command argument with the given name and description.
|
|
78
|
-
* The default is that the argument is required, and you can explicitly
|
|
79
|
-
* indicate this with <> around the name. Put [] around the name for an optional argument.
|
|
80
|
-
*
|
|
81
|
-
* @param {string} name
|
|
82
|
-
* @param {string} [description]
|
|
83
|
-
*/
|
|
84
|
-
constructor(name, description) {
|
|
85
|
-
this.description = description || "";
|
|
86
|
-
this.variadic = false;
|
|
87
|
-
this.parseArg = void 0;
|
|
88
|
-
this.defaultValue = void 0;
|
|
89
|
-
this.defaultValueDescription = void 0;
|
|
90
|
-
this.argChoices = void 0;
|
|
91
|
-
switch (name[0]) {
|
|
92
|
-
case "<":
|
|
93
|
-
this.required = true;
|
|
94
|
-
this._name = name.slice(1, -1);
|
|
95
|
-
break;
|
|
96
|
-
case "[":
|
|
97
|
-
this.required = false;
|
|
98
|
-
this._name = name.slice(1, -1);
|
|
99
|
-
break;
|
|
100
|
-
default:
|
|
101
|
-
this.required = true;
|
|
102
|
-
this._name = name;
|
|
103
|
-
break;
|
|
104
|
-
}
|
|
105
|
-
if (this._name.length > 3 && this._name.slice(-3) === "...") {
|
|
106
|
-
this.variadic = true;
|
|
107
|
-
this._name = this._name.slice(0, -3);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* Return argument name.
|
|
112
|
-
*
|
|
113
|
-
* @return {string}
|
|
114
|
-
*/
|
|
115
|
-
name() {
|
|
116
|
-
return this._name;
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* @package
|
|
120
|
-
*/
|
|
121
|
-
_concatValue(value, previous) {
|
|
122
|
-
if (previous === this.defaultValue || !Array.isArray(previous)) {
|
|
123
|
-
return [value];
|
|
124
|
-
}
|
|
125
|
-
return previous.concat(value);
|
|
126
|
-
}
|
|
127
|
-
/**
|
|
128
|
-
* Set the default value, and optionally supply the description to be displayed in the help.
|
|
129
|
-
*
|
|
130
|
-
* @param {*} value
|
|
131
|
-
* @param {string} [description]
|
|
132
|
-
* @return {Argument}
|
|
133
|
-
*/
|
|
134
|
-
default(value, description) {
|
|
135
|
-
this.defaultValue = value;
|
|
136
|
-
this.defaultValueDescription = description;
|
|
137
|
-
return this;
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* Set the custom handler for processing CLI command arguments into argument values.
|
|
141
|
-
*
|
|
142
|
-
* @param {Function} [fn]
|
|
143
|
-
* @return {Argument}
|
|
144
|
-
*/
|
|
145
|
-
argParser(fn) {
|
|
146
|
-
this.parseArg = fn;
|
|
147
|
-
return this;
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Only allow argument value to be one of choices.
|
|
151
|
-
*
|
|
152
|
-
* @param {string[]} values
|
|
153
|
-
* @return {Argument}
|
|
154
|
-
*/
|
|
155
|
-
choices(values) {
|
|
156
|
-
this.argChoices = values.slice();
|
|
157
|
-
this.parseArg = (arg, previous) => {
|
|
158
|
-
if (!this.argChoices.includes(arg)) {
|
|
159
|
-
throw new InvalidArgumentError2(
|
|
160
|
-
`Allowed choices are ${this.argChoices.join(", ")}.`
|
|
161
|
-
);
|
|
162
|
-
}
|
|
163
|
-
if (this.variadic) {
|
|
164
|
-
return this._concatValue(arg, previous);
|
|
165
|
-
}
|
|
166
|
-
return arg;
|
|
167
|
-
};
|
|
168
|
-
return this;
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* Make argument required.
|
|
172
|
-
*
|
|
173
|
-
* @returns {Argument}
|
|
174
|
-
*/
|
|
175
|
-
argRequired() {
|
|
176
|
-
this.required = true;
|
|
177
|
-
return this;
|
|
178
|
-
}
|
|
179
|
-
/**
|
|
180
|
-
* Make argument optional.
|
|
181
|
-
*
|
|
182
|
-
* @returns {Argument}
|
|
183
|
-
*/
|
|
184
|
-
argOptional() {
|
|
185
|
-
this.required = false;
|
|
186
|
-
return this;
|
|
187
|
-
}
|
|
188
|
-
};
|
|
189
|
-
function humanReadableArgName(arg) {
|
|
190
|
-
const nameOutput = arg.name() + (arg.variadic === true ? "..." : "");
|
|
191
|
-
return arg.required ? "<" + nameOutput + ">" : "[" + nameOutput + "]";
|
|
192
|
-
}
|
|
193
|
-
exports.Argument = Argument2;
|
|
194
|
-
exports.humanReadableArgName = humanReadableArgName;
|
|
195
|
-
}
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
// node_modules/commander/lib/help.js
|
|
199
|
-
var require_help = __commonJS({
|
|
200
|
-
"node_modules/commander/lib/help.js"(exports) {
|
|
201
|
-
"use strict";
|
|
202
|
-
var { humanReadableArgName } = require_argument();
|
|
203
|
-
var Help2 = class {
|
|
204
|
-
constructor() {
|
|
205
|
-
this.helpWidth = void 0;
|
|
206
|
-
this.sortSubcommands = false;
|
|
207
|
-
this.sortOptions = false;
|
|
208
|
-
this.showGlobalOptions = false;
|
|
209
|
-
}
|
|
210
|
-
/**
|
|
211
|
-
* Get an array of the visible subcommands. Includes a placeholder for the implicit help command, if there is one.
|
|
212
|
-
*
|
|
213
|
-
* @param {Command} cmd
|
|
214
|
-
* @returns {Command[]}
|
|
215
|
-
*/
|
|
216
|
-
visibleCommands(cmd) {
|
|
217
|
-
const visibleCommands = cmd.commands.filter((cmd2) => !cmd2._hidden);
|
|
218
|
-
const helpCommand = cmd._getHelpCommand();
|
|
219
|
-
if (helpCommand && !helpCommand._hidden) {
|
|
220
|
-
visibleCommands.push(helpCommand);
|
|
221
|
-
}
|
|
222
|
-
if (this.sortSubcommands) {
|
|
223
|
-
visibleCommands.sort((a, b) => {
|
|
224
|
-
return a.name().localeCompare(b.name());
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
return visibleCommands;
|
|
228
|
-
}
|
|
229
|
-
/**
|
|
230
|
-
* Compare options for sort.
|
|
231
|
-
*
|
|
232
|
-
* @param {Option} a
|
|
233
|
-
* @param {Option} b
|
|
234
|
-
* @returns {number}
|
|
235
|
-
*/
|
|
236
|
-
compareOptions(a, b) {
|
|
237
|
-
const getSortKey = (option) => {
|
|
238
|
-
return option.short ? option.short.replace(/^-/, "") : option.long.replace(/^--/, "");
|
|
239
|
-
};
|
|
240
|
-
return getSortKey(a).localeCompare(getSortKey(b));
|
|
241
|
-
}
|
|
242
|
-
/**
|
|
243
|
-
* Get an array of the visible options. Includes a placeholder for the implicit help option, if there is one.
|
|
244
|
-
*
|
|
245
|
-
* @param {Command} cmd
|
|
246
|
-
* @returns {Option[]}
|
|
247
|
-
*/
|
|
248
|
-
visibleOptions(cmd) {
|
|
249
|
-
const visibleOptions = cmd.options.filter((option) => !option.hidden);
|
|
250
|
-
const helpOption = cmd._getHelpOption();
|
|
251
|
-
if (helpOption && !helpOption.hidden) {
|
|
252
|
-
const removeShort = helpOption.short && cmd._findOption(helpOption.short);
|
|
253
|
-
const removeLong = helpOption.long && cmd._findOption(helpOption.long);
|
|
254
|
-
if (!removeShort && !removeLong) {
|
|
255
|
-
visibleOptions.push(helpOption);
|
|
256
|
-
} else if (helpOption.long && !removeLong) {
|
|
257
|
-
visibleOptions.push(
|
|
258
|
-
cmd.createOption(helpOption.long, helpOption.description)
|
|
259
|
-
);
|
|
260
|
-
} else if (helpOption.short && !removeShort) {
|
|
261
|
-
visibleOptions.push(
|
|
262
|
-
cmd.createOption(helpOption.short, helpOption.description)
|
|
263
|
-
);
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
if (this.sortOptions) {
|
|
267
|
-
visibleOptions.sort(this.compareOptions);
|
|
268
|
-
}
|
|
269
|
-
return visibleOptions;
|
|
270
|
-
}
|
|
271
|
-
/**
|
|
272
|
-
* Get an array of the visible global options. (Not including help.)
|
|
273
|
-
*
|
|
274
|
-
* @param {Command} cmd
|
|
275
|
-
* @returns {Option[]}
|
|
276
|
-
*/
|
|
277
|
-
visibleGlobalOptions(cmd) {
|
|
278
|
-
if (!this.showGlobalOptions) return [];
|
|
279
|
-
const globalOptions = [];
|
|
280
|
-
for (let ancestorCmd = cmd.parent; ancestorCmd; ancestorCmd = ancestorCmd.parent) {
|
|
281
|
-
const visibleOptions = ancestorCmd.options.filter(
|
|
282
|
-
(option) => !option.hidden
|
|
283
|
-
);
|
|
284
|
-
globalOptions.push(...visibleOptions);
|
|
285
|
-
}
|
|
286
|
-
if (this.sortOptions) {
|
|
287
|
-
globalOptions.sort(this.compareOptions);
|
|
288
|
-
}
|
|
289
|
-
return globalOptions;
|
|
290
|
-
}
|
|
291
|
-
/**
|
|
292
|
-
* Get an array of the arguments if any have a description.
|
|
293
|
-
*
|
|
294
|
-
* @param {Command} cmd
|
|
295
|
-
* @returns {Argument[]}
|
|
296
|
-
*/
|
|
297
|
-
visibleArguments(cmd) {
|
|
298
|
-
if (cmd._argsDescription) {
|
|
299
|
-
cmd.registeredArguments.forEach((argument) => {
|
|
300
|
-
argument.description = argument.description || cmd._argsDescription[argument.name()] || "";
|
|
301
|
-
});
|
|
302
|
-
}
|
|
303
|
-
if (cmd.registeredArguments.find((argument) => argument.description)) {
|
|
304
|
-
return cmd.registeredArguments;
|
|
305
|
-
}
|
|
306
|
-
return [];
|
|
307
|
-
}
|
|
308
|
-
/**
|
|
309
|
-
* Get the command term to show in the list of subcommands.
|
|
310
|
-
*
|
|
311
|
-
* @param {Command} cmd
|
|
312
|
-
* @returns {string}
|
|
313
|
-
*/
|
|
314
|
-
subcommandTerm(cmd) {
|
|
315
|
-
const args = cmd.registeredArguments.map((arg) => humanReadableArgName(arg)).join(" ");
|
|
316
|
-
return cmd._name + (cmd._aliases[0] ? "|" + cmd._aliases[0] : "") + (cmd.options.length ? " [options]" : "") + // simplistic check for non-help option
|
|
317
|
-
(args ? " " + args : "");
|
|
318
|
-
}
|
|
319
|
-
/**
|
|
320
|
-
* Get the option term to show in the list of options.
|
|
321
|
-
*
|
|
322
|
-
* @param {Option} option
|
|
323
|
-
* @returns {string}
|
|
324
|
-
*/
|
|
325
|
-
optionTerm(option) {
|
|
326
|
-
return option.flags;
|
|
327
|
-
}
|
|
328
|
-
/**
|
|
329
|
-
* Get the argument term to show in the list of arguments.
|
|
330
|
-
*
|
|
331
|
-
* @param {Argument} argument
|
|
332
|
-
* @returns {string}
|
|
333
|
-
*/
|
|
334
|
-
argumentTerm(argument) {
|
|
335
|
-
return argument.name();
|
|
336
|
-
}
|
|
337
|
-
/**
|
|
338
|
-
* Get the longest command term length.
|
|
339
|
-
*
|
|
340
|
-
* @param {Command} cmd
|
|
341
|
-
* @param {Help} helper
|
|
342
|
-
* @returns {number}
|
|
343
|
-
*/
|
|
344
|
-
longestSubcommandTermLength(cmd, helper) {
|
|
345
|
-
return helper.visibleCommands(cmd).reduce((max, command) => {
|
|
346
|
-
return Math.max(max, helper.subcommandTerm(command).length);
|
|
347
|
-
}, 0);
|
|
348
|
-
}
|
|
349
|
-
/**
|
|
350
|
-
* Get the longest option term length.
|
|
351
|
-
*
|
|
352
|
-
* @param {Command} cmd
|
|
353
|
-
* @param {Help} helper
|
|
354
|
-
* @returns {number}
|
|
355
|
-
*/
|
|
356
|
-
longestOptionTermLength(cmd, helper) {
|
|
357
|
-
return helper.visibleOptions(cmd).reduce((max, option) => {
|
|
358
|
-
return Math.max(max, helper.optionTerm(option).length);
|
|
359
|
-
}, 0);
|
|
360
|
-
}
|
|
361
|
-
/**
|
|
362
|
-
* Get the longest global option term length.
|
|
363
|
-
*
|
|
364
|
-
* @param {Command} cmd
|
|
365
|
-
* @param {Help} helper
|
|
366
|
-
* @returns {number}
|
|
367
|
-
*/
|
|
368
|
-
longestGlobalOptionTermLength(cmd, helper) {
|
|
369
|
-
return helper.visibleGlobalOptions(cmd).reduce((max, option) => {
|
|
370
|
-
return Math.max(max, helper.optionTerm(option).length);
|
|
371
|
-
}, 0);
|
|
372
|
-
}
|
|
373
|
-
/**
|
|
374
|
-
* Get the longest argument term length.
|
|
375
|
-
*
|
|
376
|
-
* @param {Command} cmd
|
|
377
|
-
* @param {Help} helper
|
|
378
|
-
* @returns {number}
|
|
379
|
-
*/
|
|
380
|
-
longestArgumentTermLength(cmd, helper) {
|
|
381
|
-
return helper.visibleArguments(cmd).reduce((max, argument) => {
|
|
382
|
-
return Math.max(max, helper.argumentTerm(argument).length);
|
|
383
|
-
}, 0);
|
|
384
|
-
}
|
|
385
|
-
/**
|
|
386
|
-
* Get the command usage to be displayed at the top of the built-in help.
|
|
387
|
-
*
|
|
388
|
-
* @param {Command} cmd
|
|
389
|
-
* @returns {string}
|
|
390
|
-
*/
|
|
391
|
-
commandUsage(cmd) {
|
|
392
|
-
let cmdName = cmd._name;
|
|
393
|
-
if (cmd._aliases[0]) {
|
|
394
|
-
cmdName = cmdName + "|" + cmd._aliases[0];
|
|
395
|
-
}
|
|
396
|
-
let ancestorCmdNames = "";
|
|
397
|
-
for (let ancestorCmd = cmd.parent; ancestorCmd; ancestorCmd = ancestorCmd.parent) {
|
|
398
|
-
ancestorCmdNames = ancestorCmd.name() + " " + ancestorCmdNames;
|
|
399
|
-
}
|
|
400
|
-
return ancestorCmdNames + cmdName + " " + cmd.usage();
|
|
401
|
-
}
|
|
402
|
-
/**
|
|
403
|
-
* Get the description for the command.
|
|
404
|
-
*
|
|
405
|
-
* @param {Command} cmd
|
|
406
|
-
* @returns {string}
|
|
407
|
-
*/
|
|
408
|
-
commandDescription(cmd) {
|
|
409
|
-
return cmd.description();
|
|
410
|
-
}
|
|
411
|
-
/**
|
|
412
|
-
* Get the subcommand summary to show in the list of subcommands.
|
|
413
|
-
* (Fallback to description for backwards compatibility.)
|
|
414
|
-
*
|
|
415
|
-
* @param {Command} cmd
|
|
416
|
-
* @returns {string}
|
|
417
|
-
*/
|
|
418
|
-
subcommandDescription(cmd) {
|
|
419
|
-
return cmd.summary() || cmd.description();
|
|
420
|
-
}
|
|
421
|
-
/**
|
|
422
|
-
* Get the option description to show in the list of options.
|
|
423
|
-
*
|
|
424
|
-
* @param {Option} option
|
|
425
|
-
* @return {string}
|
|
426
|
-
*/
|
|
427
|
-
optionDescription(option) {
|
|
428
|
-
const extraInfo = [];
|
|
429
|
-
if (option.argChoices) {
|
|
430
|
-
extraInfo.push(
|
|
431
|
-
// use stringify to match the display of the default value
|
|
432
|
-
`choices: ${option.argChoices.map((choice) => JSON.stringify(choice)).join(", ")}`
|
|
433
|
-
);
|
|
434
|
-
}
|
|
435
|
-
if (option.defaultValue !== void 0) {
|
|
436
|
-
const showDefault = option.required || option.optional || option.isBoolean() && typeof option.defaultValue === "boolean";
|
|
437
|
-
if (showDefault) {
|
|
438
|
-
extraInfo.push(
|
|
439
|
-
`default: ${option.defaultValueDescription || JSON.stringify(option.defaultValue)}`
|
|
440
|
-
);
|
|
441
|
-
}
|
|
442
|
-
}
|
|
443
|
-
if (option.presetArg !== void 0 && option.optional) {
|
|
444
|
-
extraInfo.push(`preset: ${JSON.stringify(option.presetArg)}`);
|
|
445
|
-
}
|
|
446
|
-
if (option.envVar !== void 0) {
|
|
447
|
-
extraInfo.push(`env: ${option.envVar}`);
|
|
448
|
-
}
|
|
449
|
-
if (extraInfo.length > 0) {
|
|
450
|
-
return `${option.description} (${extraInfo.join(", ")})`;
|
|
451
|
-
}
|
|
452
|
-
return option.description;
|
|
453
|
-
}
|
|
454
|
-
/**
|
|
455
|
-
* Get the argument description to show in the list of arguments.
|
|
456
|
-
*
|
|
457
|
-
* @param {Argument} argument
|
|
458
|
-
* @return {string}
|
|
459
|
-
*/
|
|
460
|
-
argumentDescription(argument) {
|
|
461
|
-
const extraInfo = [];
|
|
462
|
-
if (argument.argChoices) {
|
|
463
|
-
extraInfo.push(
|
|
464
|
-
// use stringify to match the display of the default value
|
|
465
|
-
`choices: ${argument.argChoices.map((choice) => JSON.stringify(choice)).join(", ")}`
|
|
466
|
-
);
|
|
467
|
-
}
|
|
468
|
-
if (argument.defaultValue !== void 0) {
|
|
469
|
-
extraInfo.push(
|
|
470
|
-
`default: ${argument.defaultValueDescription || JSON.stringify(argument.defaultValue)}`
|
|
471
|
-
);
|
|
472
|
-
}
|
|
473
|
-
if (extraInfo.length > 0) {
|
|
474
|
-
const extraDescripton = `(${extraInfo.join(", ")})`;
|
|
475
|
-
if (argument.description) {
|
|
476
|
-
return `${argument.description} ${extraDescripton}`;
|
|
477
|
-
}
|
|
478
|
-
return extraDescripton;
|
|
479
|
-
}
|
|
480
|
-
return argument.description;
|
|
481
|
-
}
|
|
482
|
-
/**
|
|
483
|
-
* Generate the built-in help text.
|
|
484
|
-
*
|
|
485
|
-
* @param {Command} cmd
|
|
486
|
-
* @param {Help} helper
|
|
487
|
-
* @returns {string}
|
|
488
|
-
*/
|
|
489
|
-
formatHelp(cmd, helper) {
|
|
490
|
-
const termWidth = helper.padWidth(cmd, helper);
|
|
491
|
-
const helpWidth = helper.helpWidth || 80;
|
|
492
|
-
const itemIndentWidth = 2;
|
|
493
|
-
const itemSeparatorWidth = 2;
|
|
494
|
-
function formatItem(term, description) {
|
|
495
|
-
if (description) {
|
|
496
|
-
const fullText = `${term.padEnd(termWidth + itemSeparatorWidth)}${description}`;
|
|
497
|
-
return helper.wrap(
|
|
498
|
-
fullText,
|
|
499
|
-
helpWidth - itemIndentWidth,
|
|
500
|
-
termWidth + itemSeparatorWidth
|
|
501
|
-
);
|
|
502
|
-
}
|
|
503
|
-
return term;
|
|
504
|
-
}
|
|
505
|
-
function formatList(textArray) {
|
|
506
|
-
return textArray.join("\n").replace(/^/gm, " ".repeat(itemIndentWidth));
|
|
507
|
-
}
|
|
508
|
-
let output = [`Usage: ${helper.commandUsage(cmd)}`, ""];
|
|
509
|
-
const commandDescription = helper.commandDescription(cmd);
|
|
510
|
-
if (commandDescription.length > 0) {
|
|
511
|
-
output = output.concat([
|
|
512
|
-
helper.wrap(commandDescription, helpWidth, 0),
|
|
513
|
-
""
|
|
514
|
-
]);
|
|
515
|
-
}
|
|
516
|
-
const argumentList = helper.visibleArguments(cmd).map((argument) => {
|
|
517
|
-
return formatItem(
|
|
518
|
-
helper.argumentTerm(argument),
|
|
519
|
-
helper.argumentDescription(argument)
|
|
520
|
-
);
|
|
521
|
-
});
|
|
522
|
-
if (argumentList.length > 0) {
|
|
523
|
-
output = output.concat(["Arguments:", formatList(argumentList), ""]);
|
|
524
|
-
}
|
|
525
|
-
const optionList = helper.visibleOptions(cmd).map((option) => {
|
|
526
|
-
return formatItem(
|
|
527
|
-
helper.optionTerm(option),
|
|
528
|
-
helper.optionDescription(option)
|
|
529
|
-
);
|
|
530
|
-
});
|
|
531
|
-
if (optionList.length > 0) {
|
|
532
|
-
output = output.concat(["Options:", formatList(optionList), ""]);
|
|
533
|
-
}
|
|
534
|
-
if (this.showGlobalOptions) {
|
|
535
|
-
const globalOptionList = helper.visibleGlobalOptions(cmd).map((option) => {
|
|
536
|
-
return formatItem(
|
|
537
|
-
helper.optionTerm(option),
|
|
538
|
-
helper.optionDescription(option)
|
|
539
|
-
);
|
|
540
|
-
});
|
|
541
|
-
if (globalOptionList.length > 0) {
|
|
542
|
-
output = output.concat([
|
|
543
|
-
"Global Options:",
|
|
544
|
-
formatList(globalOptionList),
|
|
545
|
-
""
|
|
546
|
-
]);
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
const commandList = helper.visibleCommands(cmd).map((cmd2) => {
|
|
550
|
-
return formatItem(
|
|
551
|
-
helper.subcommandTerm(cmd2),
|
|
552
|
-
helper.subcommandDescription(cmd2)
|
|
553
|
-
);
|
|
554
|
-
});
|
|
555
|
-
if (commandList.length > 0) {
|
|
556
|
-
output = output.concat(["Commands:", formatList(commandList), ""]);
|
|
557
|
-
}
|
|
558
|
-
return output.join("\n");
|
|
559
|
-
}
|
|
560
|
-
/**
|
|
561
|
-
* Calculate the pad width from the maximum term length.
|
|
562
|
-
*
|
|
563
|
-
* @param {Command} cmd
|
|
564
|
-
* @param {Help} helper
|
|
565
|
-
* @returns {number}
|
|
566
|
-
*/
|
|
567
|
-
padWidth(cmd, helper) {
|
|
568
|
-
return Math.max(
|
|
569
|
-
helper.longestOptionTermLength(cmd, helper),
|
|
570
|
-
helper.longestGlobalOptionTermLength(cmd, helper),
|
|
571
|
-
helper.longestSubcommandTermLength(cmd, helper),
|
|
572
|
-
helper.longestArgumentTermLength(cmd, helper)
|
|
573
|
-
);
|
|
574
|
-
}
|
|
575
|
-
/**
|
|
576
|
-
* Wrap the given string to width characters per line, with lines after the first indented.
|
|
577
|
-
* Do not wrap if insufficient room for wrapping (minColumnWidth), or string is manually formatted.
|
|
578
|
-
*
|
|
579
|
-
* @param {string} str
|
|
580
|
-
* @param {number} width
|
|
581
|
-
* @param {number} indent
|
|
582
|
-
* @param {number} [minColumnWidth=40]
|
|
583
|
-
* @return {string}
|
|
584
|
-
*
|
|
585
|
-
*/
|
|
586
|
-
wrap(str, width, indent, minColumnWidth = 40) {
|
|
587
|
-
const indents = " \\f\\t\\v\xA0\u1680\u2000-\u200A\u202F\u205F\u3000\uFEFF";
|
|
588
|
-
const manualIndent = new RegExp(`[\\n][${indents}]+`);
|
|
589
|
-
if (str.match(manualIndent)) return str;
|
|
590
|
-
const columnWidth = width - indent;
|
|
591
|
-
if (columnWidth < minColumnWidth) return str;
|
|
592
|
-
const leadingStr = str.slice(0, indent);
|
|
593
|
-
const columnText = str.slice(indent).replace("\r\n", "\n");
|
|
594
|
-
const indentString = " ".repeat(indent);
|
|
595
|
-
const zeroWidthSpace = "\u200B";
|
|
596
|
-
const breaks = `\\s${zeroWidthSpace}`;
|
|
597
|
-
const regex = new RegExp(
|
|
598
|
-
`
|
|
599
|
-
|.{1,${columnWidth - 1}}([${breaks}]|$)|[^${breaks}]+?([${breaks}]|$)`,
|
|
600
|
-
"g"
|
|
601
|
-
);
|
|
602
|
-
const lines = columnText.match(regex) || [];
|
|
603
|
-
return leadingStr + lines.map((line, i) => {
|
|
604
|
-
if (line === "\n") return "";
|
|
605
|
-
return (i > 0 ? indentString : "") + line.trimEnd();
|
|
606
|
-
}).join("\n");
|
|
607
|
-
}
|
|
608
|
-
};
|
|
609
|
-
exports.Help = Help2;
|
|
610
|
-
}
|
|
611
|
-
});
|
|
612
|
-
|
|
613
|
-
// node_modules/commander/lib/option.js
|
|
614
|
-
var require_option = __commonJS({
|
|
615
|
-
"node_modules/commander/lib/option.js"(exports) {
|
|
616
|
-
"use strict";
|
|
617
|
-
var { InvalidArgumentError: InvalidArgumentError2 } = require_error();
|
|
618
|
-
var Option2 = class {
|
|
619
|
-
/**
|
|
620
|
-
* Initialize a new `Option` with the given `flags` and `description`.
|
|
621
|
-
*
|
|
622
|
-
* @param {string} flags
|
|
623
|
-
* @param {string} [description]
|
|
624
|
-
*/
|
|
625
|
-
constructor(flags, description) {
|
|
626
|
-
this.flags = flags;
|
|
627
|
-
this.description = description || "";
|
|
628
|
-
this.required = flags.includes("<");
|
|
629
|
-
this.optional = flags.includes("[");
|
|
630
|
-
this.variadic = /\w\.\.\.[>\]]$/.test(flags);
|
|
631
|
-
this.mandatory = false;
|
|
632
|
-
const optionFlags = splitOptionFlags(flags);
|
|
633
|
-
this.short = optionFlags.shortFlag;
|
|
634
|
-
this.long = optionFlags.longFlag;
|
|
635
|
-
this.negate = false;
|
|
636
|
-
if (this.long) {
|
|
637
|
-
this.negate = this.long.startsWith("--no-");
|
|
638
|
-
}
|
|
639
|
-
this.defaultValue = void 0;
|
|
640
|
-
this.defaultValueDescription = void 0;
|
|
641
|
-
this.presetArg = void 0;
|
|
642
|
-
this.envVar = void 0;
|
|
643
|
-
this.parseArg = void 0;
|
|
644
|
-
this.hidden = false;
|
|
645
|
-
this.argChoices = void 0;
|
|
646
|
-
this.conflictsWith = [];
|
|
647
|
-
this.implied = void 0;
|
|
648
|
-
}
|
|
649
|
-
/**
|
|
650
|
-
* Set the default value, and optionally supply the description to be displayed in the help.
|
|
651
|
-
*
|
|
652
|
-
* @param {*} value
|
|
653
|
-
* @param {string} [description]
|
|
654
|
-
* @return {Option}
|
|
655
|
-
*/
|
|
656
|
-
default(value, description) {
|
|
657
|
-
this.defaultValue = value;
|
|
658
|
-
this.defaultValueDescription = description;
|
|
659
|
-
return this;
|
|
660
|
-
}
|
|
661
|
-
/**
|
|
662
|
-
* Preset to use when option used without option-argument, especially optional but also boolean and negated.
|
|
663
|
-
* The custom processing (parseArg) is called.
|
|
664
|
-
*
|
|
665
|
-
* @example
|
|
666
|
-
* new Option('--color').default('GREYSCALE').preset('RGB');
|
|
667
|
-
* new Option('--donate [amount]').preset('20').argParser(parseFloat);
|
|
668
|
-
*
|
|
669
|
-
* @param {*} arg
|
|
670
|
-
* @return {Option}
|
|
671
|
-
*/
|
|
672
|
-
preset(arg) {
|
|
673
|
-
this.presetArg = arg;
|
|
674
|
-
return this;
|
|
675
|
-
}
|
|
676
|
-
/**
|
|
677
|
-
* Add option name(s) that conflict with this option.
|
|
678
|
-
* An error will be displayed if conflicting options are found during parsing.
|
|
679
|
-
*
|
|
680
|
-
* @example
|
|
681
|
-
* new Option('--rgb').conflicts('cmyk');
|
|
682
|
-
* new Option('--js').conflicts(['ts', 'jsx']);
|
|
683
|
-
*
|
|
684
|
-
* @param {(string | string[])} names
|
|
685
|
-
* @return {Option}
|
|
686
|
-
*/
|
|
687
|
-
conflicts(names) {
|
|
688
|
-
this.conflictsWith = this.conflictsWith.concat(names);
|
|
689
|
-
return this;
|
|
690
|
-
}
|
|
691
|
-
/**
|
|
692
|
-
* Specify implied option values for when this option is set and the implied options are not.
|
|
693
|
-
*
|
|
694
|
-
* The custom processing (parseArg) is not called on the implied values.
|
|
695
|
-
*
|
|
696
|
-
* @example
|
|
697
|
-
* program
|
|
698
|
-
* .addOption(new Option('--log', 'write logging information to file'))
|
|
699
|
-
* .addOption(new Option('--trace', 'log extra details').implies({ log: 'trace.txt' }));
|
|
700
|
-
*
|
|
701
|
-
* @param {object} impliedOptionValues
|
|
702
|
-
* @return {Option}
|
|
703
|
-
*/
|
|
704
|
-
implies(impliedOptionValues) {
|
|
705
|
-
let newImplied = impliedOptionValues;
|
|
706
|
-
if (typeof impliedOptionValues === "string") {
|
|
707
|
-
newImplied = { [impliedOptionValues]: true };
|
|
708
|
-
}
|
|
709
|
-
this.implied = Object.assign(this.implied || {}, newImplied);
|
|
710
|
-
return this;
|
|
711
|
-
}
|
|
712
|
-
/**
|
|
713
|
-
* Set environment variable to check for option value.
|
|
714
|
-
*
|
|
715
|
-
* An environment variable is only used if when processed the current option value is
|
|
716
|
-
* undefined, or the source of the current value is 'default' or 'config' or 'env'.
|
|
717
|
-
*
|
|
718
|
-
* @param {string} name
|
|
719
|
-
* @return {Option}
|
|
720
|
-
*/
|
|
721
|
-
env(name) {
|
|
722
|
-
this.envVar = name;
|
|
723
|
-
return this;
|
|
724
|
-
}
|
|
725
|
-
/**
|
|
726
|
-
* Set the custom handler for processing CLI option arguments into option values.
|
|
727
|
-
*
|
|
728
|
-
* @param {Function} [fn]
|
|
729
|
-
* @return {Option}
|
|
730
|
-
*/
|
|
731
|
-
argParser(fn) {
|
|
732
|
-
this.parseArg = fn;
|
|
733
|
-
return this;
|
|
734
|
-
}
|
|
735
|
-
/**
|
|
736
|
-
* Whether the option is mandatory and must have a value after parsing.
|
|
737
|
-
*
|
|
738
|
-
* @param {boolean} [mandatory=true]
|
|
739
|
-
* @return {Option}
|
|
740
|
-
*/
|
|
741
|
-
makeOptionMandatory(mandatory = true) {
|
|
742
|
-
this.mandatory = !!mandatory;
|
|
743
|
-
return this;
|
|
744
|
-
}
|
|
745
|
-
/**
|
|
746
|
-
* Hide option in help.
|
|
747
|
-
*
|
|
748
|
-
* @param {boolean} [hide=true]
|
|
749
|
-
* @return {Option}
|
|
750
|
-
*/
|
|
751
|
-
hideHelp(hide = true) {
|
|
752
|
-
this.hidden = !!hide;
|
|
753
|
-
return this;
|
|
754
|
-
}
|
|
755
|
-
/**
|
|
756
|
-
* @package
|
|
757
|
-
*/
|
|
758
|
-
_concatValue(value, previous) {
|
|
759
|
-
if (previous === this.defaultValue || !Array.isArray(previous)) {
|
|
760
|
-
return [value];
|
|
761
|
-
}
|
|
762
|
-
return previous.concat(value);
|
|
763
|
-
}
|
|
764
|
-
/**
|
|
765
|
-
* Only allow option value to be one of choices.
|
|
766
|
-
*
|
|
767
|
-
* @param {string[]} values
|
|
768
|
-
* @return {Option}
|
|
769
|
-
*/
|
|
770
|
-
choices(values) {
|
|
771
|
-
this.argChoices = values.slice();
|
|
772
|
-
this.parseArg = (arg, previous) => {
|
|
773
|
-
if (!this.argChoices.includes(arg)) {
|
|
774
|
-
throw new InvalidArgumentError2(
|
|
775
|
-
`Allowed choices are ${this.argChoices.join(", ")}.`
|
|
776
|
-
);
|
|
777
|
-
}
|
|
778
|
-
if (this.variadic) {
|
|
779
|
-
return this._concatValue(arg, previous);
|
|
780
|
-
}
|
|
781
|
-
return arg;
|
|
782
|
-
};
|
|
783
|
-
return this;
|
|
784
|
-
}
|
|
785
|
-
/**
|
|
786
|
-
* Return option name.
|
|
787
|
-
*
|
|
788
|
-
* @return {string}
|
|
789
|
-
*/
|
|
790
|
-
name() {
|
|
791
|
-
if (this.long) {
|
|
792
|
-
return this.long.replace(/^--/, "");
|
|
793
|
-
}
|
|
794
|
-
return this.short.replace(/^-/, "");
|
|
795
|
-
}
|
|
796
|
-
/**
|
|
797
|
-
* Return option name, in a camelcase format that can be used
|
|
798
|
-
* as a object attribute key.
|
|
799
|
-
*
|
|
800
|
-
* @return {string}
|
|
801
|
-
*/
|
|
802
|
-
attributeName() {
|
|
803
|
-
return camelcase(this.name().replace(/^no-/, ""));
|
|
804
|
-
}
|
|
805
|
-
/**
|
|
806
|
-
* Check if `arg` matches the short or long flag.
|
|
807
|
-
*
|
|
808
|
-
* @param {string} arg
|
|
809
|
-
* @return {boolean}
|
|
810
|
-
* @package
|
|
811
|
-
*/
|
|
812
|
-
is(arg) {
|
|
813
|
-
return this.short === arg || this.long === arg;
|
|
814
|
-
}
|
|
815
|
-
/**
|
|
816
|
-
* Return whether a boolean option.
|
|
817
|
-
*
|
|
818
|
-
* Options are one of boolean, negated, required argument, or optional argument.
|
|
819
|
-
*
|
|
820
|
-
* @return {boolean}
|
|
821
|
-
* @package
|
|
822
|
-
*/
|
|
823
|
-
isBoolean() {
|
|
824
|
-
return !this.required && !this.optional && !this.negate;
|
|
825
|
-
}
|
|
826
|
-
};
|
|
827
|
-
var DualOptions = class {
|
|
828
|
-
/**
|
|
829
|
-
* @param {Option[]} options
|
|
830
|
-
*/
|
|
831
|
-
constructor(options) {
|
|
832
|
-
this.positiveOptions = /* @__PURE__ */ new Map();
|
|
833
|
-
this.negativeOptions = /* @__PURE__ */ new Map();
|
|
834
|
-
this.dualOptions = /* @__PURE__ */ new Set();
|
|
835
|
-
options.forEach((option) => {
|
|
836
|
-
if (option.negate) {
|
|
837
|
-
this.negativeOptions.set(option.attributeName(), option);
|
|
838
|
-
} else {
|
|
839
|
-
this.positiveOptions.set(option.attributeName(), option);
|
|
840
|
-
}
|
|
841
|
-
});
|
|
842
|
-
this.negativeOptions.forEach((value, key) => {
|
|
843
|
-
if (this.positiveOptions.has(key)) {
|
|
844
|
-
this.dualOptions.add(key);
|
|
845
|
-
}
|
|
846
|
-
});
|
|
847
|
-
}
|
|
848
|
-
/**
|
|
849
|
-
* Did the value come from the option, and not from possible matching dual option?
|
|
850
|
-
*
|
|
851
|
-
* @param {*} value
|
|
852
|
-
* @param {Option} option
|
|
853
|
-
* @returns {boolean}
|
|
854
|
-
*/
|
|
855
|
-
valueFromOption(value, option) {
|
|
856
|
-
const optionKey = option.attributeName();
|
|
857
|
-
if (!this.dualOptions.has(optionKey)) return true;
|
|
858
|
-
const preset = this.negativeOptions.get(optionKey).presetArg;
|
|
859
|
-
const negativeValue = preset !== void 0 ? preset : false;
|
|
860
|
-
return option.negate === (negativeValue === value);
|
|
861
|
-
}
|
|
862
|
-
};
|
|
863
|
-
function camelcase(str) {
|
|
864
|
-
return str.split("-").reduce((str2, word) => {
|
|
865
|
-
return str2 + word[0].toUpperCase() + word.slice(1);
|
|
866
|
-
});
|
|
867
|
-
}
|
|
868
|
-
function splitOptionFlags(flags) {
|
|
869
|
-
let shortFlag;
|
|
870
|
-
let longFlag;
|
|
871
|
-
const flagParts = flags.split(/[ |,]+/);
|
|
872
|
-
if (flagParts.length > 1 && !/^[[<]/.test(flagParts[1]))
|
|
873
|
-
shortFlag = flagParts.shift();
|
|
874
|
-
longFlag = flagParts.shift();
|
|
875
|
-
if (!shortFlag && /^-[^-]$/.test(longFlag)) {
|
|
876
|
-
shortFlag = longFlag;
|
|
877
|
-
longFlag = void 0;
|
|
878
|
-
}
|
|
879
|
-
return { shortFlag, longFlag };
|
|
880
|
-
}
|
|
881
|
-
exports.Option = Option2;
|
|
882
|
-
exports.DualOptions = DualOptions;
|
|
883
|
-
}
|
|
884
|
-
});
|
|
885
|
-
|
|
886
|
-
// node_modules/commander/lib/suggestSimilar.js
|
|
887
|
-
var require_suggestSimilar = __commonJS({
|
|
888
|
-
"node_modules/commander/lib/suggestSimilar.js"(exports) {
|
|
889
|
-
"use strict";
|
|
890
|
-
var maxDistance = 3;
|
|
891
|
-
function editDistance(a, b) {
|
|
892
|
-
if (Math.abs(a.length - b.length) > maxDistance)
|
|
893
|
-
return Math.max(a.length, b.length);
|
|
894
|
-
const d = [];
|
|
895
|
-
for (let i = 0; i <= a.length; i++) {
|
|
896
|
-
d[i] = [i];
|
|
897
|
-
}
|
|
898
|
-
for (let j = 0; j <= b.length; j++) {
|
|
899
|
-
d[0][j] = j;
|
|
900
|
-
}
|
|
901
|
-
for (let j = 1; j <= b.length; j++) {
|
|
902
|
-
for (let i = 1; i <= a.length; i++) {
|
|
903
|
-
let cost = 1;
|
|
904
|
-
if (a[i - 1] === b[j - 1]) {
|
|
905
|
-
cost = 0;
|
|
906
|
-
} else {
|
|
907
|
-
cost = 1;
|
|
908
|
-
}
|
|
909
|
-
d[i][j] = Math.min(
|
|
910
|
-
d[i - 1][j] + 1,
|
|
911
|
-
// deletion
|
|
912
|
-
d[i][j - 1] + 1,
|
|
913
|
-
// insertion
|
|
914
|
-
d[i - 1][j - 1] + cost
|
|
915
|
-
// substitution
|
|
916
|
-
);
|
|
917
|
-
if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
|
|
918
|
-
d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + 1);
|
|
919
|
-
}
|
|
920
|
-
}
|
|
921
|
-
}
|
|
922
|
-
return d[a.length][b.length];
|
|
923
|
-
}
|
|
924
|
-
function suggestSimilar(word, candidates) {
|
|
925
|
-
if (!candidates || candidates.length === 0) return "";
|
|
926
|
-
candidates = Array.from(new Set(candidates));
|
|
927
|
-
const searchingOptions = word.startsWith("--");
|
|
928
|
-
if (searchingOptions) {
|
|
929
|
-
word = word.slice(2);
|
|
930
|
-
candidates = candidates.map((candidate) => candidate.slice(2));
|
|
931
|
-
}
|
|
932
|
-
let similar = [];
|
|
933
|
-
let bestDistance = maxDistance;
|
|
934
|
-
const minSimilarity = 0.4;
|
|
935
|
-
candidates.forEach((candidate) => {
|
|
936
|
-
if (candidate.length <= 1) return;
|
|
937
|
-
const distance = editDistance(word, candidate);
|
|
938
|
-
const length = Math.max(word.length, candidate.length);
|
|
939
|
-
const similarity = (length - distance) / length;
|
|
940
|
-
if (similarity > minSimilarity) {
|
|
941
|
-
if (distance < bestDistance) {
|
|
942
|
-
bestDistance = distance;
|
|
943
|
-
similar = [candidate];
|
|
944
|
-
} else if (distance === bestDistance) {
|
|
945
|
-
similar.push(candidate);
|
|
946
|
-
}
|
|
947
|
-
}
|
|
948
|
-
});
|
|
949
|
-
similar.sort((a, b) => a.localeCompare(b));
|
|
950
|
-
if (searchingOptions) {
|
|
951
|
-
similar = similar.map((candidate) => `--${candidate}`);
|
|
952
|
-
}
|
|
953
|
-
if (similar.length > 1) {
|
|
954
|
-
return `
|
|
955
|
-
(Did you mean one of ${similar.join(", ")}?)`;
|
|
956
|
-
}
|
|
957
|
-
if (similar.length === 1) {
|
|
958
|
-
return `
|
|
959
|
-
(Did you mean ${similar[0]}?)`;
|
|
960
|
-
}
|
|
961
|
-
return "";
|
|
962
|
-
}
|
|
963
|
-
exports.suggestSimilar = suggestSimilar;
|
|
964
|
-
}
|
|
965
|
-
});
|
|
966
|
-
|
|
967
|
-
// node_modules/commander/lib/command.js
|
|
968
|
-
var require_command = __commonJS({
|
|
969
|
-
"node_modules/commander/lib/command.js"(exports) {
|
|
970
|
-
"use strict";
|
|
971
|
-
var EventEmitter = __require("events").EventEmitter;
|
|
972
|
-
var childProcess = __require("child_process");
|
|
973
|
-
var path13 = __require("path");
|
|
974
|
-
var fs10 = __require("fs");
|
|
975
|
-
var process2 = __require("process");
|
|
976
|
-
var { Argument: Argument2, humanReadableArgName } = require_argument();
|
|
977
|
-
var { CommanderError: CommanderError2 } = require_error();
|
|
978
|
-
var { Help: Help2 } = require_help();
|
|
979
|
-
var { Option: Option2, DualOptions } = require_option();
|
|
980
|
-
var { suggestSimilar } = require_suggestSimilar();
|
|
981
|
-
var Command2 = class _Command extends EventEmitter {
|
|
982
|
-
/**
|
|
983
|
-
* Initialize a new `Command`.
|
|
984
|
-
*
|
|
985
|
-
* @param {string} [name]
|
|
986
|
-
*/
|
|
987
|
-
constructor(name) {
|
|
988
|
-
super();
|
|
989
|
-
this.commands = [];
|
|
990
|
-
this.options = [];
|
|
991
|
-
this.parent = null;
|
|
992
|
-
this._allowUnknownOption = false;
|
|
993
|
-
this._allowExcessArguments = true;
|
|
994
|
-
this.registeredArguments = [];
|
|
995
|
-
this._args = this.registeredArguments;
|
|
996
|
-
this.args = [];
|
|
997
|
-
this.rawArgs = [];
|
|
998
|
-
this.processedArgs = [];
|
|
999
|
-
this._scriptPath = null;
|
|
1000
|
-
this._name = name || "";
|
|
1001
|
-
this._optionValues = {};
|
|
1002
|
-
this._optionValueSources = {};
|
|
1003
|
-
this._storeOptionsAsProperties = false;
|
|
1004
|
-
this._actionHandler = null;
|
|
1005
|
-
this._executableHandler = false;
|
|
1006
|
-
this._executableFile = null;
|
|
1007
|
-
this._executableDir = null;
|
|
1008
|
-
this._defaultCommandName = null;
|
|
1009
|
-
this._exitCallback = null;
|
|
1010
|
-
this._aliases = [];
|
|
1011
|
-
this._combineFlagAndOptionalValue = true;
|
|
1012
|
-
this._description = "";
|
|
1013
|
-
this._summary = "";
|
|
1014
|
-
this._argsDescription = void 0;
|
|
1015
|
-
this._enablePositionalOptions = false;
|
|
1016
|
-
this._passThroughOptions = false;
|
|
1017
|
-
this._lifeCycleHooks = {};
|
|
1018
|
-
this._showHelpAfterError = false;
|
|
1019
|
-
this._showSuggestionAfterError = true;
|
|
1020
|
-
this._outputConfiguration = {
|
|
1021
|
-
writeOut: (str) => process2.stdout.write(str),
|
|
1022
|
-
writeErr: (str) => process2.stderr.write(str),
|
|
1023
|
-
getOutHelpWidth: () => process2.stdout.isTTY ? process2.stdout.columns : void 0,
|
|
1024
|
-
getErrHelpWidth: () => process2.stderr.isTTY ? process2.stderr.columns : void 0,
|
|
1025
|
-
outputError: (str, write) => write(str)
|
|
1026
|
-
};
|
|
1027
|
-
this._hidden = false;
|
|
1028
|
-
this._helpOption = void 0;
|
|
1029
|
-
this._addImplicitHelpCommand = void 0;
|
|
1030
|
-
this._helpCommand = void 0;
|
|
1031
|
-
this._helpConfiguration = {};
|
|
1032
|
-
}
|
|
1033
|
-
/**
|
|
1034
|
-
* Copy settings that are useful to have in common across root command and subcommands.
|
|
1035
|
-
*
|
|
1036
|
-
* (Used internally when adding a command using `.command()` so subcommands inherit parent settings.)
|
|
1037
|
-
*
|
|
1038
|
-
* @param {Command} sourceCommand
|
|
1039
|
-
* @return {Command} `this` command for chaining
|
|
1040
|
-
*/
|
|
1041
|
-
copyInheritedSettings(sourceCommand) {
|
|
1042
|
-
this._outputConfiguration = sourceCommand._outputConfiguration;
|
|
1043
|
-
this._helpOption = sourceCommand._helpOption;
|
|
1044
|
-
this._helpCommand = sourceCommand._helpCommand;
|
|
1045
|
-
this._helpConfiguration = sourceCommand._helpConfiguration;
|
|
1046
|
-
this._exitCallback = sourceCommand._exitCallback;
|
|
1047
|
-
this._storeOptionsAsProperties = sourceCommand._storeOptionsAsProperties;
|
|
1048
|
-
this._combineFlagAndOptionalValue = sourceCommand._combineFlagAndOptionalValue;
|
|
1049
|
-
this._allowExcessArguments = sourceCommand._allowExcessArguments;
|
|
1050
|
-
this._enablePositionalOptions = sourceCommand._enablePositionalOptions;
|
|
1051
|
-
this._showHelpAfterError = sourceCommand._showHelpAfterError;
|
|
1052
|
-
this._showSuggestionAfterError = sourceCommand._showSuggestionAfterError;
|
|
1053
|
-
return this;
|
|
1054
|
-
}
|
|
1055
|
-
/**
|
|
1056
|
-
* @returns {Command[]}
|
|
1057
|
-
* @private
|
|
1058
|
-
*/
|
|
1059
|
-
_getCommandAndAncestors() {
|
|
1060
|
-
const result = [];
|
|
1061
|
-
for (let command = this; command; command = command.parent) {
|
|
1062
|
-
result.push(command);
|
|
1063
|
-
}
|
|
1064
|
-
return result;
|
|
1065
|
-
}
|
|
1066
|
-
/**
|
|
1067
|
-
* Define a command.
|
|
1068
|
-
*
|
|
1069
|
-
* There are two styles of command: pay attention to where to put the description.
|
|
1070
|
-
*
|
|
1071
|
-
* @example
|
|
1072
|
-
* // Command implemented using action handler (description is supplied separately to `.command`)
|
|
1073
|
-
* program
|
|
1074
|
-
* .command('clone <source> [destination]')
|
|
1075
|
-
* .description('clone a repository into a newly created directory')
|
|
1076
|
-
* .action((source, destination) => {
|
|
1077
|
-
* console.log('clone command called');
|
|
1078
|
-
* });
|
|
1079
|
-
*
|
|
1080
|
-
* // Command implemented using separate executable file (description is second parameter to `.command`)
|
|
1081
|
-
* program
|
|
1082
|
-
* .command('start <service>', 'start named service')
|
|
1083
|
-
* .command('stop [service]', 'stop named service, or all if no name supplied');
|
|
1084
|
-
*
|
|
1085
|
-
* @param {string} nameAndArgs - command name and arguments, args are `<required>` or `[optional]` and last may also be `variadic...`
|
|
1086
|
-
* @param {(object | string)} [actionOptsOrExecDesc] - configuration options (for action), or description (for executable)
|
|
1087
|
-
* @param {object} [execOpts] - configuration options (for executable)
|
|
1088
|
-
* @return {Command} returns new command for action handler, or `this` for executable command
|
|
1089
|
-
*/
|
|
1090
|
-
command(nameAndArgs, actionOptsOrExecDesc, execOpts) {
|
|
1091
|
-
let desc = actionOptsOrExecDesc;
|
|
1092
|
-
let opts = execOpts;
|
|
1093
|
-
if (typeof desc === "object" && desc !== null) {
|
|
1094
|
-
opts = desc;
|
|
1095
|
-
desc = null;
|
|
1096
|
-
}
|
|
1097
|
-
opts = opts || {};
|
|
1098
|
-
const [, name, args] = nameAndArgs.match(/([^ ]+) *(.*)/);
|
|
1099
|
-
const cmd = this.createCommand(name);
|
|
1100
|
-
if (desc) {
|
|
1101
|
-
cmd.description(desc);
|
|
1102
|
-
cmd._executableHandler = true;
|
|
1103
|
-
}
|
|
1104
|
-
if (opts.isDefault) this._defaultCommandName = cmd._name;
|
|
1105
|
-
cmd._hidden = !!(opts.noHelp || opts.hidden);
|
|
1106
|
-
cmd._executableFile = opts.executableFile || null;
|
|
1107
|
-
if (args) cmd.arguments(args);
|
|
1108
|
-
this._registerCommand(cmd);
|
|
1109
|
-
cmd.parent = this;
|
|
1110
|
-
cmd.copyInheritedSettings(this);
|
|
1111
|
-
if (desc) return this;
|
|
1112
|
-
return cmd;
|
|
1113
|
-
}
|
|
1114
|
-
/**
|
|
1115
|
-
* Factory routine to create a new unattached command.
|
|
1116
|
-
*
|
|
1117
|
-
* See .command() for creating an attached subcommand, which uses this routine to
|
|
1118
|
-
* create the command. You can override createCommand to customise subcommands.
|
|
1119
|
-
*
|
|
1120
|
-
* @param {string} [name]
|
|
1121
|
-
* @return {Command} new command
|
|
1122
|
-
*/
|
|
1123
|
-
createCommand(name) {
|
|
1124
|
-
return new _Command(name);
|
|
1125
|
-
}
|
|
1126
|
-
/**
|
|
1127
|
-
* You can customise the help with a subclass of Help by overriding createHelp,
|
|
1128
|
-
* or by overriding Help properties using configureHelp().
|
|
1129
|
-
*
|
|
1130
|
-
* @return {Help}
|
|
1131
|
-
*/
|
|
1132
|
-
createHelp() {
|
|
1133
|
-
return Object.assign(new Help2(), this.configureHelp());
|
|
1134
|
-
}
|
|
1135
|
-
/**
|
|
1136
|
-
* You can customise the help by overriding Help properties using configureHelp(),
|
|
1137
|
-
* or with a subclass of Help by overriding createHelp().
|
|
1138
|
-
*
|
|
1139
|
-
* @param {object} [configuration] - configuration options
|
|
1140
|
-
* @return {(Command | object)} `this` command for chaining, or stored configuration
|
|
1141
|
-
*/
|
|
1142
|
-
configureHelp(configuration) {
|
|
1143
|
-
if (configuration === void 0) return this._helpConfiguration;
|
|
1144
|
-
this._helpConfiguration = configuration;
|
|
1145
|
-
return this;
|
|
1146
|
-
}
|
|
1147
|
-
/**
|
|
1148
|
-
* The default output goes to stdout and stderr. You can customise this for special
|
|
1149
|
-
* applications. You can also customise the display of errors by overriding outputError.
|
|
1150
|
-
*
|
|
1151
|
-
* The configuration properties are all functions:
|
|
1152
|
-
*
|
|
1153
|
-
* // functions to change where being written, stdout and stderr
|
|
1154
|
-
* writeOut(str)
|
|
1155
|
-
* writeErr(str)
|
|
1156
|
-
* // matching functions to specify width for wrapping help
|
|
1157
|
-
* getOutHelpWidth()
|
|
1158
|
-
* getErrHelpWidth()
|
|
1159
|
-
* // functions based on what is being written out
|
|
1160
|
-
* outputError(str, write) // used for displaying errors, and not used for displaying help
|
|
1161
|
-
*
|
|
1162
|
-
* @param {object} [configuration] - configuration options
|
|
1163
|
-
* @return {(Command | object)} `this` command for chaining, or stored configuration
|
|
1164
|
-
*/
|
|
1165
|
-
configureOutput(configuration) {
|
|
1166
|
-
if (configuration === void 0) return this._outputConfiguration;
|
|
1167
|
-
Object.assign(this._outputConfiguration, configuration);
|
|
1168
|
-
return this;
|
|
1169
|
-
}
|
|
1170
|
-
/**
|
|
1171
|
-
* Display the help or a custom message after an error occurs.
|
|
1172
|
-
*
|
|
1173
|
-
* @param {(boolean|string)} [displayHelp]
|
|
1174
|
-
* @return {Command} `this` command for chaining
|
|
1175
|
-
*/
|
|
1176
|
-
showHelpAfterError(displayHelp = true) {
|
|
1177
|
-
if (typeof displayHelp !== "string") displayHelp = !!displayHelp;
|
|
1178
|
-
this._showHelpAfterError = displayHelp;
|
|
1179
|
-
return this;
|
|
1180
|
-
}
|
|
1181
|
-
/**
|
|
1182
|
-
* Display suggestion of similar commands for unknown commands, or options for unknown options.
|
|
1183
|
-
*
|
|
1184
|
-
* @param {boolean} [displaySuggestion]
|
|
1185
|
-
* @return {Command} `this` command for chaining
|
|
1186
|
-
*/
|
|
1187
|
-
showSuggestionAfterError(displaySuggestion = true) {
|
|
1188
|
-
this._showSuggestionAfterError = !!displaySuggestion;
|
|
1189
|
-
return this;
|
|
1190
|
-
}
|
|
1191
|
-
/**
|
|
1192
|
-
* Add a prepared subcommand.
|
|
1193
|
-
*
|
|
1194
|
-
* See .command() for creating an attached subcommand which inherits settings from its parent.
|
|
1195
|
-
*
|
|
1196
|
-
* @param {Command} cmd - new subcommand
|
|
1197
|
-
* @param {object} [opts] - configuration options
|
|
1198
|
-
* @return {Command} `this` command for chaining
|
|
1199
|
-
*/
|
|
1200
|
-
addCommand(cmd, opts) {
|
|
1201
|
-
if (!cmd._name) {
|
|
1202
|
-
throw new Error(`Command passed to .addCommand() must have a name
|
|
1203
|
-
- specify the name in Command constructor or using .name()`);
|
|
1204
|
-
}
|
|
1205
|
-
opts = opts || {};
|
|
1206
|
-
if (opts.isDefault) this._defaultCommandName = cmd._name;
|
|
1207
|
-
if (opts.noHelp || opts.hidden) cmd._hidden = true;
|
|
1208
|
-
this._registerCommand(cmd);
|
|
1209
|
-
cmd.parent = this;
|
|
1210
|
-
cmd._checkForBrokenPassThrough();
|
|
1211
|
-
return this;
|
|
1212
|
-
}
|
|
1213
|
-
/**
|
|
1214
|
-
* Factory routine to create a new unattached argument.
|
|
1215
|
-
*
|
|
1216
|
-
* See .argument() for creating an attached argument, which uses this routine to
|
|
1217
|
-
* create the argument. You can override createArgument to return a custom argument.
|
|
1218
|
-
*
|
|
1219
|
-
* @param {string} name
|
|
1220
|
-
* @param {string} [description]
|
|
1221
|
-
* @return {Argument} new argument
|
|
1222
|
-
*/
|
|
1223
|
-
createArgument(name, description) {
|
|
1224
|
-
return new Argument2(name, description);
|
|
1225
|
-
}
|
|
1226
|
-
/**
|
|
1227
|
-
* Define argument syntax for command.
|
|
1228
|
-
*
|
|
1229
|
-
* The default is that the argument is required, and you can explicitly
|
|
1230
|
-
* indicate this with <> around the name. Put [] around the name for an optional argument.
|
|
1231
|
-
*
|
|
1232
|
-
* @example
|
|
1233
|
-
* program.argument('<input-file>');
|
|
1234
|
-
* program.argument('[output-file]');
|
|
1235
|
-
*
|
|
1236
|
-
* @param {string} name
|
|
1237
|
-
* @param {string} [description]
|
|
1238
|
-
* @param {(Function|*)} [fn] - custom argument processing function
|
|
1239
|
-
* @param {*} [defaultValue]
|
|
1240
|
-
* @return {Command} `this` command for chaining
|
|
1241
|
-
*/
|
|
1242
|
-
argument(name, description, fn, defaultValue) {
|
|
1243
|
-
const argument = this.createArgument(name, description);
|
|
1244
|
-
if (typeof fn === "function") {
|
|
1245
|
-
argument.default(defaultValue).argParser(fn);
|
|
1246
|
-
} else {
|
|
1247
|
-
argument.default(fn);
|
|
1248
|
-
}
|
|
1249
|
-
this.addArgument(argument);
|
|
1250
|
-
return this;
|
|
1251
|
-
}
|
|
1252
|
-
/**
|
|
1253
|
-
* Define argument syntax for command, adding multiple at once (without descriptions).
|
|
1254
|
-
*
|
|
1255
|
-
* See also .argument().
|
|
1256
|
-
*
|
|
1257
|
-
* @example
|
|
1258
|
-
* program.arguments('<cmd> [env]');
|
|
1259
|
-
*
|
|
1260
|
-
* @param {string} names
|
|
1261
|
-
* @return {Command} `this` command for chaining
|
|
1262
|
-
*/
|
|
1263
|
-
arguments(names) {
|
|
1264
|
-
names.trim().split(/ +/).forEach((detail) => {
|
|
1265
|
-
this.argument(detail);
|
|
1266
|
-
});
|
|
1267
|
-
return this;
|
|
1268
|
-
}
|
|
1269
|
-
/**
|
|
1270
|
-
* Define argument syntax for command, adding a prepared argument.
|
|
1271
|
-
*
|
|
1272
|
-
* @param {Argument} argument
|
|
1273
|
-
* @return {Command} `this` command for chaining
|
|
1274
|
-
*/
|
|
1275
|
-
addArgument(argument) {
|
|
1276
|
-
const previousArgument = this.registeredArguments.slice(-1)[0];
|
|
1277
|
-
if (previousArgument && previousArgument.variadic) {
|
|
1278
|
-
throw new Error(
|
|
1279
|
-
`only the last argument can be variadic '${previousArgument.name()}'`
|
|
1280
|
-
);
|
|
1281
|
-
}
|
|
1282
|
-
if (argument.required && argument.defaultValue !== void 0 && argument.parseArg === void 0) {
|
|
1283
|
-
throw new Error(
|
|
1284
|
-
`a default value for a required argument is never used: '${argument.name()}'`
|
|
1285
|
-
);
|
|
1286
|
-
}
|
|
1287
|
-
this.registeredArguments.push(argument);
|
|
1288
|
-
return this;
|
|
1289
|
-
}
|
|
1290
|
-
/**
|
|
1291
|
-
* Customise or override default help command. By default a help command is automatically added if your command has subcommands.
|
|
1292
|
-
*
|
|
1293
|
-
* @example
|
|
1294
|
-
* program.helpCommand('help [cmd]');
|
|
1295
|
-
* program.helpCommand('help [cmd]', 'show help');
|
|
1296
|
-
* program.helpCommand(false); // suppress default help command
|
|
1297
|
-
* program.helpCommand(true); // add help command even if no subcommands
|
|
1298
|
-
*
|
|
1299
|
-
* @param {string|boolean} enableOrNameAndArgs - enable with custom name and/or arguments, or boolean to override whether added
|
|
1300
|
-
* @param {string} [description] - custom description
|
|
1301
|
-
* @return {Command} `this` command for chaining
|
|
1302
|
-
*/
|
|
1303
|
-
helpCommand(enableOrNameAndArgs, description) {
|
|
1304
|
-
if (typeof enableOrNameAndArgs === "boolean") {
|
|
1305
|
-
this._addImplicitHelpCommand = enableOrNameAndArgs;
|
|
1306
|
-
return this;
|
|
1307
|
-
}
|
|
1308
|
-
enableOrNameAndArgs = enableOrNameAndArgs ?? "help [command]";
|
|
1309
|
-
const [, helpName, helpArgs] = enableOrNameAndArgs.match(/([^ ]+) *(.*)/);
|
|
1310
|
-
const helpDescription = description ?? "display help for command";
|
|
1311
|
-
const helpCommand = this.createCommand(helpName);
|
|
1312
|
-
helpCommand.helpOption(false);
|
|
1313
|
-
if (helpArgs) helpCommand.arguments(helpArgs);
|
|
1314
|
-
if (helpDescription) helpCommand.description(helpDescription);
|
|
1315
|
-
this._addImplicitHelpCommand = true;
|
|
1316
|
-
this._helpCommand = helpCommand;
|
|
1317
|
-
return this;
|
|
1318
|
-
}
|
|
1319
|
-
/**
|
|
1320
|
-
* Add prepared custom help command.
|
|
1321
|
-
*
|
|
1322
|
-
* @param {(Command|string|boolean)} helpCommand - custom help command, or deprecated enableOrNameAndArgs as for `.helpCommand()`
|
|
1323
|
-
* @param {string} [deprecatedDescription] - deprecated custom description used with custom name only
|
|
1324
|
-
* @return {Command} `this` command for chaining
|
|
1325
|
-
*/
|
|
1326
|
-
addHelpCommand(helpCommand, deprecatedDescription) {
|
|
1327
|
-
if (typeof helpCommand !== "object") {
|
|
1328
|
-
this.helpCommand(helpCommand, deprecatedDescription);
|
|
1329
|
-
return this;
|
|
1330
|
-
}
|
|
1331
|
-
this._addImplicitHelpCommand = true;
|
|
1332
|
-
this._helpCommand = helpCommand;
|
|
1333
|
-
return this;
|
|
1334
|
-
}
|
|
1335
|
-
/**
|
|
1336
|
-
* Lazy create help command.
|
|
1337
|
-
*
|
|
1338
|
-
* @return {(Command|null)}
|
|
1339
|
-
* @package
|
|
1340
|
-
*/
|
|
1341
|
-
_getHelpCommand() {
|
|
1342
|
-
const hasImplicitHelpCommand = this._addImplicitHelpCommand ?? (this.commands.length && !this._actionHandler && !this._findCommand("help"));
|
|
1343
|
-
if (hasImplicitHelpCommand) {
|
|
1344
|
-
if (this._helpCommand === void 0) {
|
|
1345
|
-
this.helpCommand(void 0, void 0);
|
|
1346
|
-
}
|
|
1347
|
-
return this._helpCommand;
|
|
1348
|
-
}
|
|
1349
|
-
return null;
|
|
1350
|
-
}
|
|
1351
|
-
/**
|
|
1352
|
-
* Add hook for life cycle event.
|
|
1353
|
-
*
|
|
1354
|
-
* @param {string} event
|
|
1355
|
-
* @param {Function} listener
|
|
1356
|
-
* @return {Command} `this` command for chaining
|
|
1357
|
-
*/
|
|
1358
|
-
hook(event, listener) {
|
|
1359
|
-
const allowedValues = ["preSubcommand", "preAction", "postAction"];
|
|
1360
|
-
if (!allowedValues.includes(event)) {
|
|
1361
|
-
throw new Error(`Unexpected value for event passed to hook : '${event}'.
|
|
1362
|
-
Expecting one of '${allowedValues.join("', '")}'`);
|
|
1363
|
-
}
|
|
1364
|
-
if (this._lifeCycleHooks[event]) {
|
|
1365
|
-
this._lifeCycleHooks[event].push(listener);
|
|
1366
|
-
} else {
|
|
1367
|
-
this._lifeCycleHooks[event] = [listener];
|
|
1368
|
-
}
|
|
1369
|
-
return this;
|
|
1370
|
-
}
|
|
1371
|
-
/**
|
|
1372
|
-
* Register callback to use as replacement for calling process.exit.
|
|
1373
|
-
*
|
|
1374
|
-
* @param {Function} [fn] optional callback which will be passed a CommanderError, defaults to throwing
|
|
1375
|
-
* @return {Command} `this` command for chaining
|
|
1376
|
-
*/
|
|
1377
|
-
exitOverride(fn) {
|
|
1378
|
-
if (fn) {
|
|
1379
|
-
this._exitCallback = fn;
|
|
1380
|
-
} else {
|
|
1381
|
-
this._exitCallback = (err) => {
|
|
1382
|
-
if (err.code !== "commander.executeSubCommandAsync") {
|
|
1383
|
-
throw err;
|
|
1384
|
-
} else {
|
|
1385
|
-
}
|
|
1386
|
-
};
|
|
1387
|
-
}
|
|
1388
|
-
return this;
|
|
1389
|
-
}
|
|
1390
|
-
/**
|
|
1391
|
-
* Call process.exit, and _exitCallback if defined.
|
|
1392
|
-
*
|
|
1393
|
-
* @param {number} exitCode exit code for using with process.exit
|
|
1394
|
-
* @param {string} code an id string representing the error
|
|
1395
|
-
* @param {string} message human-readable description of the error
|
|
1396
|
-
* @return never
|
|
1397
|
-
* @private
|
|
1398
|
-
*/
|
|
1399
|
-
_exit(exitCode, code, message) {
|
|
1400
|
-
if (this._exitCallback) {
|
|
1401
|
-
this._exitCallback(new CommanderError2(exitCode, code, message));
|
|
1402
|
-
}
|
|
1403
|
-
process2.exit(exitCode);
|
|
1404
|
-
}
|
|
1405
|
-
/**
|
|
1406
|
-
* Register callback `fn` for the command.
|
|
1407
|
-
*
|
|
1408
|
-
* @example
|
|
1409
|
-
* program
|
|
1410
|
-
* .command('serve')
|
|
1411
|
-
* .description('start service')
|
|
1412
|
-
* .action(function() {
|
|
1413
|
-
* // do work here
|
|
1414
|
-
* });
|
|
1415
|
-
*
|
|
1416
|
-
* @param {Function} fn
|
|
1417
|
-
* @return {Command} `this` command for chaining
|
|
1418
|
-
*/
|
|
1419
|
-
action(fn) {
|
|
1420
|
-
const listener = (args) => {
|
|
1421
|
-
const expectedArgsCount = this.registeredArguments.length;
|
|
1422
|
-
const actionArgs = args.slice(0, expectedArgsCount);
|
|
1423
|
-
if (this._storeOptionsAsProperties) {
|
|
1424
|
-
actionArgs[expectedArgsCount] = this;
|
|
1425
|
-
} else {
|
|
1426
|
-
actionArgs[expectedArgsCount] = this.opts();
|
|
1427
|
-
}
|
|
1428
|
-
actionArgs.push(this);
|
|
1429
|
-
return fn.apply(this, actionArgs);
|
|
1430
|
-
};
|
|
1431
|
-
this._actionHandler = listener;
|
|
1432
|
-
return this;
|
|
1433
|
-
}
|
|
1434
|
-
/**
|
|
1435
|
-
* Factory routine to create a new unattached option.
|
|
1436
|
-
*
|
|
1437
|
-
* See .option() for creating an attached option, which uses this routine to
|
|
1438
|
-
* create the option. You can override createOption to return a custom option.
|
|
1439
|
-
*
|
|
1440
|
-
* @param {string} flags
|
|
1441
|
-
* @param {string} [description]
|
|
1442
|
-
* @return {Option} new option
|
|
1443
|
-
*/
|
|
1444
|
-
createOption(flags, description) {
|
|
1445
|
-
return new Option2(flags, description);
|
|
1446
|
-
}
|
|
1447
|
-
/**
|
|
1448
|
-
* Wrap parseArgs to catch 'commander.invalidArgument'.
|
|
1449
|
-
*
|
|
1450
|
-
* @param {(Option | Argument)} target
|
|
1451
|
-
* @param {string} value
|
|
1452
|
-
* @param {*} previous
|
|
1453
|
-
* @param {string} invalidArgumentMessage
|
|
1454
|
-
* @private
|
|
1455
|
-
*/
|
|
1456
|
-
_callParseArg(target, value, previous, invalidArgumentMessage) {
|
|
1457
|
-
try {
|
|
1458
|
-
return target.parseArg(value, previous);
|
|
1459
|
-
} catch (err) {
|
|
1460
|
-
if (err.code === "commander.invalidArgument") {
|
|
1461
|
-
const message = `${invalidArgumentMessage} ${err.message}`;
|
|
1462
|
-
this.error(message, { exitCode: err.exitCode, code: err.code });
|
|
1463
|
-
}
|
|
1464
|
-
throw err;
|
|
1465
|
-
}
|
|
1466
|
-
}
|
|
1467
|
-
/**
|
|
1468
|
-
* Check for option flag conflicts.
|
|
1469
|
-
* Register option if no conflicts found, or throw on conflict.
|
|
1470
|
-
*
|
|
1471
|
-
* @param {Option} option
|
|
1472
|
-
* @private
|
|
1473
|
-
*/
|
|
1474
|
-
_registerOption(option) {
|
|
1475
|
-
const matchingOption = option.short && this._findOption(option.short) || option.long && this._findOption(option.long);
|
|
1476
|
-
if (matchingOption) {
|
|
1477
|
-
const matchingFlag = option.long && this._findOption(option.long) ? option.long : option.short;
|
|
1478
|
-
throw new Error(`Cannot add option '${option.flags}'${this._name && ` to command '${this._name}'`} due to conflicting flag '${matchingFlag}'
|
|
1479
|
-
- already used by option '${matchingOption.flags}'`);
|
|
1480
|
-
}
|
|
1481
|
-
this.options.push(option);
|
|
1482
|
-
}
|
|
1483
|
-
/**
|
|
1484
|
-
* Check for command name and alias conflicts with existing commands.
|
|
1485
|
-
* Register command if no conflicts found, or throw on conflict.
|
|
1486
|
-
*
|
|
1487
|
-
* @param {Command} command
|
|
1488
|
-
* @private
|
|
1489
|
-
*/
|
|
1490
|
-
_registerCommand(command) {
|
|
1491
|
-
const knownBy = (cmd) => {
|
|
1492
|
-
return [cmd.name()].concat(cmd.aliases());
|
|
1493
|
-
};
|
|
1494
|
-
const alreadyUsed = knownBy(command).find(
|
|
1495
|
-
(name) => this._findCommand(name)
|
|
1496
|
-
);
|
|
1497
|
-
if (alreadyUsed) {
|
|
1498
|
-
const existingCmd = knownBy(this._findCommand(alreadyUsed)).join("|");
|
|
1499
|
-
const newCmd = knownBy(command).join("|");
|
|
1500
|
-
throw new Error(
|
|
1501
|
-
`cannot add command '${newCmd}' as already have command '${existingCmd}'`
|
|
1502
|
-
);
|
|
1503
|
-
}
|
|
1504
|
-
this.commands.push(command);
|
|
1505
|
-
}
|
|
1506
|
-
/**
|
|
1507
|
-
* Add an option.
|
|
1508
|
-
*
|
|
1509
|
-
* @param {Option} option
|
|
1510
|
-
* @return {Command} `this` command for chaining
|
|
1511
|
-
*/
|
|
1512
|
-
addOption(option) {
|
|
1513
|
-
this._registerOption(option);
|
|
1514
|
-
const oname = option.name();
|
|
1515
|
-
const name = option.attributeName();
|
|
1516
|
-
if (option.negate) {
|
|
1517
|
-
const positiveLongFlag = option.long.replace(/^--no-/, "--");
|
|
1518
|
-
if (!this._findOption(positiveLongFlag)) {
|
|
1519
|
-
this.setOptionValueWithSource(
|
|
1520
|
-
name,
|
|
1521
|
-
option.defaultValue === void 0 ? true : option.defaultValue,
|
|
1522
|
-
"default"
|
|
1523
|
-
);
|
|
1524
|
-
}
|
|
1525
|
-
} else if (option.defaultValue !== void 0) {
|
|
1526
|
-
this.setOptionValueWithSource(name, option.defaultValue, "default");
|
|
1527
|
-
}
|
|
1528
|
-
const handleOptionValue = (val, invalidValueMessage, valueSource) => {
|
|
1529
|
-
if (val == null && option.presetArg !== void 0) {
|
|
1530
|
-
val = option.presetArg;
|
|
1531
|
-
}
|
|
1532
|
-
const oldValue = this.getOptionValue(name);
|
|
1533
|
-
if (val !== null && option.parseArg) {
|
|
1534
|
-
val = this._callParseArg(option, val, oldValue, invalidValueMessage);
|
|
1535
|
-
} else if (val !== null && option.variadic) {
|
|
1536
|
-
val = option._concatValue(val, oldValue);
|
|
1537
|
-
}
|
|
1538
|
-
if (val == null) {
|
|
1539
|
-
if (option.negate) {
|
|
1540
|
-
val = false;
|
|
1541
|
-
} else if (option.isBoolean() || option.optional) {
|
|
1542
|
-
val = true;
|
|
1543
|
-
} else {
|
|
1544
|
-
val = "";
|
|
1545
|
-
}
|
|
1546
|
-
}
|
|
1547
|
-
this.setOptionValueWithSource(name, val, valueSource);
|
|
1548
|
-
};
|
|
1549
|
-
this.on("option:" + oname, (val) => {
|
|
1550
|
-
const invalidValueMessage = `error: option '${option.flags}' argument '${val}' is invalid.`;
|
|
1551
|
-
handleOptionValue(val, invalidValueMessage, "cli");
|
|
1552
|
-
});
|
|
1553
|
-
if (option.envVar) {
|
|
1554
|
-
this.on("optionEnv:" + oname, (val) => {
|
|
1555
|
-
const invalidValueMessage = `error: option '${option.flags}' value '${val}' from env '${option.envVar}' is invalid.`;
|
|
1556
|
-
handleOptionValue(val, invalidValueMessage, "env");
|
|
1557
|
-
});
|
|
1558
|
-
}
|
|
1559
|
-
return this;
|
|
1560
|
-
}
|
|
1561
|
-
/**
|
|
1562
|
-
* Internal implementation shared by .option() and .requiredOption()
|
|
1563
|
-
*
|
|
1564
|
-
* @return {Command} `this` command for chaining
|
|
1565
|
-
* @private
|
|
1566
|
-
*/
|
|
1567
|
-
_optionEx(config, flags, description, fn, defaultValue) {
|
|
1568
|
-
if (typeof flags === "object" && flags instanceof Option2) {
|
|
1569
|
-
throw new Error(
|
|
1570
|
-
"To add an Option object use addOption() instead of option() or requiredOption()"
|
|
1571
|
-
);
|
|
1572
|
-
}
|
|
1573
|
-
const option = this.createOption(flags, description);
|
|
1574
|
-
option.makeOptionMandatory(!!config.mandatory);
|
|
1575
|
-
if (typeof fn === "function") {
|
|
1576
|
-
option.default(defaultValue).argParser(fn);
|
|
1577
|
-
} else if (fn instanceof RegExp) {
|
|
1578
|
-
const regex = fn;
|
|
1579
|
-
fn = (val, def) => {
|
|
1580
|
-
const m = regex.exec(val);
|
|
1581
|
-
return m ? m[0] : def;
|
|
1582
|
-
};
|
|
1583
|
-
option.default(defaultValue).argParser(fn);
|
|
1584
|
-
} else {
|
|
1585
|
-
option.default(fn);
|
|
1586
|
-
}
|
|
1587
|
-
return this.addOption(option);
|
|
1588
|
-
}
|
|
1589
|
-
/**
|
|
1590
|
-
* Define option with `flags`, `description`, and optional argument parsing function or `defaultValue` or both.
|
|
1591
|
-
*
|
|
1592
|
-
* The `flags` string contains the short and/or long flags, separated by comma, a pipe or space. A required
|
|
1593
|
-
* option-argument is indicated by `<>` and an optional option-argument by `[]`.
|
|
1594
|
-
*
|
|
1595
|
-
* See the README for more details, and see also addOption() and requiredOption().
|
|
1596
|
-
*
|
|
1597
|
-
* @example
|
|
1598
|
-
* program
|
|
1599
|
-
* .option('-p, --pepper', 'add pepper')
|
|
1600
|
-
* .option('-p, --pizza-type <TYPE>', 'type of pizza') // required option-argument
|
|
1601
|
-
* .option('-c, --cheese [CHEESE]', 'add extra cheese', 'mozzarella') // optional option-argument with default
|
|
1602
|
-
* .option('-t, --tip <VALUE>', 'add tip to purchase cost', parseFloat) // custom parse function
|
|
1603
|
-
*
|
|
1604
|
-
* @param {string} flags
|
|
1605
|
-
* @param {string} [description]
|
|
1606
|
-
* @param {(Function|*)} [parseArg] - custom option processing function or default value
|
|
1607
|
-
* @param {*} [defaultValue]
|
|
1608
|
-
* @return {Command} `this` command for chaining
|
|
1609
|
-
*/
|
|
1610
|
-
option(flags, description, parseArg, defaultValue) {
|
|
1611
|
-
return this._optionEx({}, flags, description, parseArg, defaultValue);
|
|
1612
|
-
}
|
|
1613
|
-
/**
|
|
1614
|
-
* Add a required option which must have a value after parsing. This usually means
|
|
1615
|
-
* the option must be specified on the command line. (Otherwise the same as .option().)
|
|
1616
|
-
*
|
|
1617
|
-
* The `flags` string contains the short and/or long flags, separated by comma, a pipe or space.
|
|
1618
|
-
*
|
|
1619
|
-
* @param {string} flags
|
|
1620
|
-
* @param {string} [description]
|
|
1621
|
-
* @param {(Function|*)} [parseArg] - custom option processing function or default value
|
|
1622
|
-
* @param {*} [defaultValue]
|
|
1623
|
-
* @return {Command} `this` command for chaining
|
|
1624
|
-
*/
|
|
1625
|
-
requiredOption(flags, description, parseArg, defaultValue) {
|
|
1626
|
-
return this._optionEx(
|
|
1627
|
-
{ mandatory: true },
|
|
1628
|
-
flags,
|
|
1629
|
-
description,
|
|
1630
|
-
parseArg,
|
|
1631
|
-
defaultValue
|
|
1632
|
-
);
|
|
1633
|
-
}
|
|
1634
|
-
/**
|
|
1635
|
-
* Alter parsing of short flags with optional values.
|
|
1636
|
-
*
|
|
1637
|
-
* @example
|
|
1638
|
-
* // for `.option('-f,--flag [value]'):
|
|
1639
|
-
* program.combineFlagAndOptionalValue(true); // `-f80` is treated like `--flag=80`, this is the default behaviour
|
|
1640
|
-
* program.combineFlagAndOptionalValue(false) // `-fb` is treated like `-f -b`
|
|
1641
|
-
*
|
|
1642
|
-
* @param {boolean} [combine] - if `true` or omitted, an optional value can be specified directly after the flag.
|
|
1643
|
-
* @return {Command} `this` command for chaining
|
|
1644
|
-
*/
|
|
1645
|
-
combineFlagAndOptionalValue(combine = true) {
|
|
1646
|
-
this._combineFlagAndOptionalValue = !!combine;
|
|
1647
|
-
return this;
|
|
1648
|
-
}
|
|
1649
|
-
/**
|
|
1650
|
-
* Allow unknown options on the command line.
|
|
1651
|
-
*
|
|
1652
|
-
* @param {boolean} [allowUnknown] - if `true` or omitted, no error will be thrown for unknown options.
|
|
1653
|
-
* @return {Command} `this` command for chaining
|
|
1654
|
-
*/
|
|
1655
|
-
allowUnknownOption(allowUnknown = true) {
|
|
1656
|
-
this._allowUnknownOption = !!allowUnknown;
|
|
1657
|
-
return this;
|
|
1658
|
-
}
|
|
1659
|
-
/**
|
|
1660
|
-
* Allow excess command-arguments on the command line. Pass false to make excess arguments an error.
|
|
1661
|
-
*
|
|
1662
|
-
* @param {boolean} [allowExcess] - if `true` or omitted, no error will be thrown for excess arguments.
|
|
1663
|
-
* @return {Command} `this` command for chaining
|
|
1664
|
-
*/
|
|
1665
|
-
allowExcessArguments(allowExcess = true) {
|
|
1666
|
-
this._allowExcessArguments = !!allowExcess;
|
|
1667
|
-
return this;
|
|
1668
|
-
}
|
|
1669
|
-
/**
|
|
1670
|
-
* Enable positional options. Positional means global options are specified before subcommands which lets
|
|
1671
|
-
* subcommands reuse the same option names, and also enables subcommands to turn on passThroughOptions.
|
|
1672
|
-
* The default behaviour is non-positional and global options may appear anywhere on the command line.
|
|
1673
|
-
*
|
|
1674
|
-
* @param {boolean} [positional]
|
|
1675
|
-
* @return {Command} `this` command for chaining
|
|
1676
|
-
*/
|
|
1677
|
-
enablePositionalOptions(positional = true) {
|
|
1678
|
-
this._enablePositionalOptions = !!positional;
|
|
1679
|
-
return this;
|
|
1680
|
-
}
|
|
1681
|
-
/**
|
|
1682
|
-
* Pass through options that come after command-arguments rather than treat them as command-options,
|
|
1683
|
-
* so actual command-options come before command-arguments. Turning this on for a subcommand requires
|
|
1684
|
-
* positional options to have been enabled on the program (parent commands).
|
|
1685
|
-
* The default behaviour is non-positional and options may appear before or after command-arguments.
|
|
1686
|
-
*
|
|
1687
|
-
* @param {boolean} [passThrough] for unknown options.
|
|
1688
|
-
* @return {Command} `this` command for chaining
|
|
1689
|
-
*/
|
|
1690
|
-
passThroughOptions(passThrough = true) {
|
|
1691
|
-
this._passThroughOptions = !!passThrough;
|
|
1692
|
-
this._checkForBrokenPassThrough();
|
|
1693
|
-
return this;
|
|
1694
|
-
}
|
|
1695
|
-
/**
|
|
1696
|
-
* @private
|
|
1697
|
-
*/
|
|
1698
|
-
_checkForBrokenPassThrough() {
|
|
1699
|
-
if (this.parent && this._passThroughOptions && !this.parent._enablePositionalOptions) {
|
|
1700
|
-
throw new Error(
|
|
1701
|
-
`passThroughOptions cannot be used for '${this._name}' without turning on enablePositionalOptions for parent command(s)`
|
|
1702
|
-
);
|
|
1703
|
-
}
|
|
1704
|
-
}
|
|
1705
|
-
/**
|
|
1706
|
-
* Whether to store option values as properties on command object,
|
|
1707
|
-
* or store separately (specify false). In both cases the option values can be accessed using .opts().
|
|
1708
|
-
*
|
|
1709
|
-
* @param {boolean} [storeAsProperties=true]
|
|
1710
|
-
* @return {Command} `this` command for chaining
|
|
1711
|
-
*/
|
|
1712
|
-
storeOptionsAsProperties(storeAsProperties = true) {
|
|
1713
|
-
if (this.options.length) {
|
|
1714
|
-
throw new Error("call .storeOptionsAsProperties() before adding options");
|
|
1715
|
-
}
|
|
1716
|
-
if (Object.keys(this._optionValues).length) {
|
|
1717
|
-
throw new Error(
|
|
1718
|
-
"call .storeOptionsAsProperties() before setting option values"
|
|
1719
|
-
);
|
|
1720
|
-
}
|
|
1721
|
-
this._storeOptionsAsProperties = !!storeAsProperties;
|
|
1722
|
-
return this;
|
|
1723
|
-
}
|
|
1724
|
-
/**
|
|
1725
|
-
* Retrieve option value.
|
|
1726
|
-
*
|
|
1727
|
-
* @param {string} key
|
|
1728
|
-
* @return {object} value
|
|
1729
|
-
*/
|
|
1730
|
-
getOptionValue(key) {
|
|
1731
|
-
if (this._storeOptionsAsProperties) {
|
|
1732
|
-
return this[key];
|
|
1733
|
-
}
|
|
1734
|
-
return this._optionValues[key];
|
|
1735
|
-
}
|
|
1736
|
-
/**
|
|
1737
|
-
* Store option value.
|
|
1738
|
-
*
|
|
1739
|
-
* @param {string} key
|
|
1740
|
-
* @param {object} value
|
|
1741
|
-
* @return {Command} `this` command for chaining
|
|
1742
|
-
*/
|
|
1743
|
-
setOptionValue(key, value) {
|
|
1744
|
-
return this.setOptionValueWithSource(key, value, void 0);
|
|
1745
|
-
}
|
|
1746
|
-
/**
|
|
1747
|
-
* Store option value and where the value came from.
|
|
1748
|
-
*
|
|
1749
|
-
* @param {string} key
|
|
1750
|
-
* @param {object} value
|
|
1751
|
-
* @param {string} source - expected values are default/config/env/cli/implied
|
|
1752
|
-
* @return {Command} `this` command for chaining
|
|
1753
|
-
*/
|
|
1754
|
-
setOptionValueWithSource(key, value, source) {
|
|
1755
|
-
if (this._storeOptionsAsProperties) {
|
|
1756
|
-
this[key] = value;
|
|
1757
|
-
} else {
|
|
1758
|
-
this._optionValues[key] = value;
|
|
1759
|
-
}
|
|
1760
|
-
this._optionValueSources[key] = source;
|
|
1761
|
-
return this;
|
|
1762
|
-
}
|
|
1763
|
-
/**
|
|
1764
|
-
* Get source of option value.
|
|
1765
|
-
* Expected values are default | config | env | cli | implied
|
|
1766
|
-
*
|
|
1767
|
-
* @param {string} key
|
|
1768
|
-
* @return {string}
|
|
1769
|
-
*/
|
|
1770
|
-
getOptionValueSource(key) {
|
|
1771
|
-
return this._optionValueSources[key];
|
|
1772
|
-
}
|
|
1773
|
-
/**
|
|
1774
|
-
* Get source of option value. See also .optsWithGlobals().
|
|
1775
|
-
* Expected values are default | config | env | cli | implied
|
|
1776
|
-
*
|
|
1777
|
-
* @param {string} key
|
|
1778
|
-
* @return {string}
|
|
1779
|
-
*/
|
|
1780
|
-
getOptionValueSourceWithGlobals(key) {
|
|
1781
|
-
let source;
|
|
1782
|
-
this._getCommandAndAncestors().forEach((cmd) => {
|
|
1783
|
-
if (cmd.getOptionValueSource(key) !== void 0) {
|
|
1784
|
-
source = cmd.getOptionValueSource(key);
|
|
1785
|
-
}
|
|
1786
|
-
});
|
|
1787
|
-
return source;
|
|
1788
|
-
}
|
|
1789
|
-
/**
|
|
1790
|
-
* Get user arguments from implied or explicit arguments.
|
|
1791
|
-
* Side-effects: set _scriptPath if args included script. Used for default program name, and subcommand searches.
|
|
1792
|
-
*
|
|
1793
|
-
* @private
|
|
1794
|
-
*/
|
|
1795
|
-
_prepareUserArgs(argv, parseOptions) {
|
|
1796
|
-
if (argv !== void 0 && !Array.isArray(argv)) {
|
|
1797
|
-
throw new Error("first parameter to parse must be array or undefined");
|
|
1798
|
-
}
|
|
1799
|
-
parseOptions = parseOptions || {};
|
|
1800
|
-
if (argv === void 0 && parseOptions.from === void 0) {
|
|
1801
|
-
if (process2.versions?.electron) {
|
|
1802
|
-
parseOptions.from = "electron";
|
|
1803
|
-
}
|
|
1804
|
-
const execArgv = process2.execArgv ?? [];
|
|
1805
|
-
if (execArgv.includes("-e") || execArgv.includes("--eval") || execArgv.includes("-p") || execArgv.includes("--print")) {
|
|
1806
|
-
parseOptions.from = "eval";
|
|
1807
|
-
}
|
|
1808
|
-
}
|
|
1809
|
-
if (argv === void 0) {
|
|
1810
|
-
argv = process2.argv;
|
|
1811
|
-
}
|
|
1812
|
-
this.rawArgs = argv.slice();
|
|
1813
|
-
let userArgs;
|
|
1814
|
-
switch (parseOptions.from) {
|
|
1815
|
-
case void 0:
|
|
1816
|
-
case "node":
|
|
1817
|
-
this._scriptPath = argv[1];
|
|
1818
|
-
userArgs = argv.slice(2);
|
|
1819
|
-
break;
|
|
1820
|
-
case "electron":
|
|
1821
|
-
if (process2.defaultApp) {
|
|
1822
|
-
this._scriptPath = argv[1];
|
|
1823
|
-
userArgs = argv.slice(2);
|
|
1824
|
-
} else {
|
|
1825
|
-
userArgs = argv.slice(1);
|
|
1826
|
-
}
|
|
1827
|
-
break;
|
|
1828
|
-
case "user":
|
|
1829
|
-
userArgs = argv.slice(0);
|
|
1830
|
-
break;
|
|
1831
|
-
case "eval":
|
|
1832
|
-
userArgs = argv.slice(1);
|
|
1833
|
-
break;
|
|
1834
|
-
default:
|
|
1835
|
-
throw new Error(
|
|
1836
|
-
`unexpected parse option { from: '${parseOptions.from}' }`
|
|
1837
|
-
);
|
|
1838
|
-
}
|
|
1839
|
-
if (!this._name && this._scriptPath)
|
|
1840
|
-
this.nameFromFilename(this._scriptPath);
|
|
1841
|
-
this._name = this._name || "program";
|
|
1842
|
-
return userArgs;
|
|
1843
|
-
}
|
|
1844
|
-
/**
|
|
1845
|
-
* Parse `argv`, setting options and invoking commands when defined.
|
|
1846
|
-
*
|
|
1847
|
-
* Use parseAsync instead of parse if any of your action handlers are async.
|
|
1848
|
-
*
|
|
1849
|
-
* Call with no parameters to parse `process.argv`. Detects Electron and special node options like `node --eval`. Easy mode!
|
|
1850
|
-
*
|
|
1851
|
-
* Or call with an array of strings to parse, and optionally where the user arguments start by specifying where the arguments are `from`:
|
|
1852
|
-
* - `'node'`: default, `argv[0]` is the application and `argv[1]` is the script being run, with user arguments after that
|
|
1853
|
-
* - `'electron'`: `argv[0]` is the application and `argv[1]` varies depending on whether the electron application is packaged
|
|
1854
|
-
* - `'user'`: just user arguments
|
|
1855
|
-
*
|
|
1856
|
-
* @example
|
|
1857
|
-
* program.parse(); // parse process.argv and auto-detect electron and special node flags
|
|
1858
|
-
* program.parse(process.argv); // assume argv[0] is app and argv[1] is script
|
|
1859
|
-
* program.parse(my-args, { from: 'user' }); // just user supplied arguments, nothing special about argv[0]
|
|
1860
|
-
*
|
|
1861
|
-
* @param {string[]} [argv] - optional, defaults to process.argv
|
|
1862
|
-
* @param {object} [parseOptions] - optionally specify style of options with from: node/user/electron
|
|
1863
|
-
* @param {string} [parseOptions.from] - where the args are from: 'node', 'user', 'electron'
|
|
1864
|
-
* @return {Command} `this` command for chaining
|
|
1865
|
-
*/
|
|
1866
|
-
parse(argv, parseOptions) {
|
|
1867
|
-
const userArgs = this._prepareUserArgs(argv, parseOptions);
|
|
1868
|
-
this._parseCommand([], userArgs);
|
|
1869
|
-
return this;
|
|
1870
|
-
}
|
|
1871
|
-
/**
|
|
1872
|
-
* Parse `argv`, setting options and invoking commands when defined.
|
|
1873
|
-
*
|
|
1874
|
-
* Call with no parameters to parse `process.argv`. Detects Electron and special node options like `node --eval`. Easy mode!
|
|
1875
|
-
*
|
|
1876
|
-
* Or call with an array of strings to parse, and optionally where the user arguments start by specifying where the arguments are `from`:
|
|
1877
|
-
* - `'node'`: default, `argv[0]` is the application and `argv[1]` is the script being run, with user arguments after that
|
|
1878
|
-
* - `'electron'`: `argv[0]` is the application and `argv[1]` varies depending on whether the electron application is packaged
|
|
1879
|
-
* - `'user'`: just user arguments
|
|
1880
|
-
*
|
|
1881
|
-
* @example
|
|
1882
|
-
* await program.parseAsync(); // parse process.argv and auto-detect electron and special node flags
|
|
1883
|
-
* await program.parseAsync(process.argv); // assume argv[0] is app and argv[1] is script
|
|
1884
|
-
* await program.parseAsync(my-args, { from: 'user' }); // just user supplied arguments, nothing special about argv[0]
|
|
1885
|
-
*
|
|
1886
|
-
* @param {string[]} [argv]
|
|
1887
|
-
* @param {object} [parseOptions]
|
|
1888
|
-
* @param {string} parseOptions.from - where the args are from: 'node', 'user', 'electron'
|
|
1889
|
-
* @return {Promise}
|
|
1890
|
-
*/
|
|
1891
|
-
async parseAsync(argv, parseOptions) {
|
|
1892
|
-
const userArgs = this._prepareUserArgs(argv, parseOptions);
|
|
1893
|
-
await this._parseCommand([], userArgs);
|
|
1894
|
-
return this;
|
|
1895
|
-
}
|
|
1896
|
-
/**
|
|
1897
|
-
* Execute a sub-command executable.
|
|
1898
|
-
*
|
|
1899
|
-
* @private
|
|
1900
|
-
*/
|
|
1901
|
-
_executeSubCommand(subcommand, args) {
|
|
1902
|
-
args = args.slice();
|
|
1903
|
-
let launchWithNode = false;
|
|
1904
|
-
const sourceExt = [".js", ".ts", ".tsx", ".mjs", ".cjs"];
|
|
1905
|
-
function findFile(baseDir, baseName) {
|
|
1906
|
-
const localBin = path13.resolve(baseDir, baseName);
|
|
1907
|
-
if (fs10.existsSync(localBin)) return localBin;
|
|
1908
|
-
if (sourceExt.includes(path13.extname(baseName))) return void 0;
|
|
1909
|
-
const foundExt = sourceExt.find(
|
|
1910
|
-
(ext) => fs10.existsSync(`${localBin}${ext}`)
|
|
1911
|
-
);
|
|
1912
|
-
if (foundExt) return `${localBin}${foundExt}`;
|
|
1913
|
-
return void 0;
|
|
1914
|
-
}
|
|
1915
|
-
this._checkForMissingMandatoryOptions();
|
|
1916
|
-
this._checkForConflictingOptions();
|
|
1917
|
-
let executableFile = subcommand._executableFile || `${this._name}-${subcommand._name}`;
|
|
1918
|
-
let executableDir = this._executableDir || "";
|
|
1919
|
-
if (this._scriptPath) {
|
|
1920
|
-
let resolvedScriptPath;
|
|
1921
|
-
try {
|
|
1922
|
-
resolvedScriptPath = fs10.realpathSync(this._scriptPath);
|
|
1923
|
-
} catch (err) {
|
|
1924
|
-
resolvedScriptPath = this._scriptPath;
|
|
1925
|
-
}
|
|
1926
|
-
executableDir = path13.resolve(
|
|
1927
|
-
path13.dirname(resolvedScriptPath),
|
|
1928
|
-
executableDir
|
|
1929
|
-
);
|
|
1930
|
-
}
|
|
1931
|
-
if (executableDir) {
|
|
1932
|
-
let localFile = findFile(executableDir, executableFile);
|
|
1933
|
-
if (!localFile && !subcommand._executableFile && this._scriptPath) {
|
|
1934
|
-
const legacyName = path13.basename(
|
|
1935
|
-
this._scriptPath,
|
|
1936
|
-
path13.extname(this._scriptPath)
|
|
1937
|
-
);
|
|
1938
|
-
if (legacyName !== this._name) {
|
|
1939
|
-
localFile = findFile(
|
|
1940
|
-
executableDir,
|
|
1941
|
-
`${legacyName}-${subcommand._name}`
|
|
1942
|
-
);
|
|
1943
|
-
}
|
|
1944
|
-
}
|
|
1945
|
-
executableFile = localFile || executableFile;
|
|
1946
|
-
}
|
|
1947
|
-
launchWithNode = sourceExt.includes(path13.extname(executableFile));
|
|
1948
|
-
let proc;
|
|
1949
|
-
if (process2.platform !== "win32") {
|
|
1950
|
-
if (launchWithNode) {
|
|
1951
|
-
args.unshift(executableFile);
|
|
1952
|
-
args = incrementNodeInspectorPort(process2.execArgv).concat(args);
|
|
1953
|
-
proc = childProcess.spawn(process2.argv[0], args, { stdio: "inherit" });
|
|
1954
|
-
} else {
|
|
1955
|
-
proc = childProcess.spawn(executableFile, args, { stdio: "inherit" });
|
|
1956
|
-
}
|
|
1957
|
-
} else {
|
|
1958
|
-
args.unshift(executableFile);
|
|
1959
|
-
args = incrementNodeInspectorPort(process2.execArgv).concat(args);
|
|
1960
|
-
proc = childProcess.spawn(process2.execPath, args, { stdio: "inherit" });
|
|
1961
|
-
}
|
|
1962
|
-
if (!proc.killed) {
|
|
1963
|
-
const signals = ["SIGUSR1", "SIGUSR2", "SIGTERM", "SIGINT", "SIGHUP"];
|
|
1964
|
-
signals.forEach((signal) => {
|
|
1965
|
-
process2.on(signal, () => {
|
|
1966
|
-
if (proc.killed === false && proc.exitCode === null) {
|
|
1967
|
-
proc.kill(signal);
|
|
1968
|
-
}
|
|
1969
|
-
});
|
|
1970
|
-
});
|
|
1971
|
-
}
|
|
1972
|
-
const exitCallback = this._exitCallback;
|
|
1973
|
-
proc.on("close", (code) => {
|
|
1974
|
-
code = code ?? 1;
|
|
1975
|
-
if (!exitCallback) {
|
|
1976
|
-
process2.exit(code);
|
|
1977
|
-
} else {
|
|
1978
|
-
exitCallback(
|
|
1979
|
-
new CommanderError2(
|
|
1980
|
-
code,
|
|
1981
|
-
"commander.executeSubCommandAsync",
|
|
1982
|
-
"(close)"
|
|
1983
|
-
)
|
|
1984
|
-
);
|
|
1985
|
-
}
|
|
1986
|
-
});
|
|
1987
|
-
proc.on("error", (err) => {
|
|
1988
|
-
if (err.code === "ENOENT") {
|
|
1989
|
-
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";
|
|
1990
|
-
const executableMissing = `'${executableFile}' does not exist
|
|
1991
|
-
- if '${subcommand._name}' is not meant to be an executable command, remove description parameter from '.command()' and use '.description()' instead
|
|
1992
|
-
- if the default executable name is not suitable, use the executableFile option to supply a custom name or path
|
|
1993
|
-
- ${executableDirMessage}`;
|
|
1994
|
-
throw new Error(executableMissing);
|
|
1995
|
-
} else if (err.code === "EACCES") {
|
|
1996
|
-
throw new Error(`'${executableFile}' not executable`);
|
|
1997
|
-
}
|
|
1998
|
-
if (!exitCallback) {
|
|
1999
|
-
process2.exit(1);
|
|
2000
|
-
} else {
|
|
2001
|
-
const wrappedError = new CommanderError2(
|
|
2002
|
-
1,
|
|
2003
|
-
"commander.executeSubCommandAsync",
|
|
2004
|
-
"(error)"
|
|
2005
|
-
);
|
|
2006
|
-
wrappedError.nestedError = err;
|
|
2007
|
-
exitCallback(wrappedError);
|
|
2008
|
-
}
|
|
2009
|
-
});
|
|
2010
|
-
this.runningCommand = proc;
|
|
2011
|
-
}
|
|
2012
|
-
/**
|
|
2013
|
-
* @private
|
|
2014
|
-
*/
|
|
2015
|
-
_dispatchSubcommand(commandName, operands, unknown) {
|
|
2016
|
-
const subCommand = this._findCommand(commandName);
|
|
2017
|
-
if (!subCommand) this.help({ error: true });
|
|
2018
|
-
let promiseChain;
|
|
2019
|
-
promiseChain = this._chainOrCallSubCommandHook(
|
|
2020
|
-
promiseChain,
|
|
2021
|
-
subCommand,
|
|
2022
|
-
"preSubcommand"
|
|
2023
|
-
);
|
|
2024
|
-
promiseChain = this._chainOrCall(promiseChain, () => {
|
|
2025
|
-
if (subCommand._executableHandler) {
|
|
2026
|
-
this._executeSubCommand(subCommand, operands.concat(unknown));
|
|
2027
|
-
} else {
|
|
2028
|
-
return subCommand._parseCommand(operands, unknown);
|
|
2029
|
-
}
|
|
2030
|
-
});
|
|
2031
|
-
return promiseChain;
|
|
2032
|
-
}
|
|
2033
|
-
/**
|
|
2034
|
-
* Invoke help directly if possible, or dispatch if necessary.
|
|
2035
|
-
* e.g. help foo
|
|
2036
|
-
*
|
|
2037
|
-
* @private
|
|
2038
|
-
*/
|
|
2039
|
-
_dispatchHelpCommand(subcommandName) {
|
|
2040
|
-
if (!subcommandName) {
|
|
2041
|
-
this.help();
|
|
2042
|
-
}
|
|
2043
|
-
const subCommand = this._findCommand(subcommandName);
|
|
2044
|
-
if (subCommand && !subCommand._executableHandler) {
|
|
2045
|
-
subCommand.help();
|
|
2046
|
-
}
|
|
2047
|
-
return this._dispatchSubcommand(
|
|
2048
|
-
subcommandName,
|
|
2049
|
-
[],
|
|
2050
|
-
[this._getHelpOption()?.long ?? this._getHelpOption()?.short ?? "--help"]
|
|
2051
|
-
);
|
|
2052
|
-
}
|
|
2053
|
-
/**
|
|
2054
|
-
* Check this.args against expected this.registeredArguments.
|
|
2055
|
-
*
|
|
2056
|
-
* @private
|
|
2057
|
-
*/
|
|
2058
|
-
_checkNumberOfArguments() {
|
|
2059
|
-
this.registeredArguments.forEach((arg, i) => {
|
|
2060
|
-
if (arg.required && this.args[i] == null) {
|
|
2061
|
-
this.missingArgument(arg.name());
|
|
2062
|
-
}
|
|
2063
|
-
});
|
|
2064
|
-
if (this.registeredArguments.length > 0 && this.registeredArguments[this.registeredArguments.length - 1].variadic) {
|
|
2065
|
-
return;
|
|
2066
|
-
}
|
|
2067
|
-
if (this.args.length > this.registeredArguments.length) {
|
|
2068
|
-
this._excessArguments(this.args);
|
|
2069
|
-
}
|
|
2070
|
-
}
|
|
2071
|
-
/**
|
|
2072
|
-
* Process this.args using this.registeredArguments and save as this.processedArgs!
|
|
2073
|
-
*
|
|
2074
|
-
* @private
|
|
2075
|
-
*/
|
|
2076
|
-
_processArguments() {
|
|
2077
|
-
const myParseArg = (argument, value, previous) => {
|
|
2078
|
-
let parsedValue = value;
|
|
2079
|
-
if (value !== null && argument.parseArg) {
|
|
2080
|
-
const invalidValueMessage = `error: command-argument value '${value}' is invalid for argument '${argument.name()}'.`;
|
|
2081
|
-
parsedValue = this._callParseArg(
|
|
2082
|
-
argument,
|
|
2083
|
-
value,
|
|
2084
|
-
previous,
|
|
2085
|
-
invalidValueMessage
|
|
2086
|
-
);
|
|
2087
|
-
}
|
|
2088
|
-
return parsedValue;
|
|
2089
|
-
};
|
|
2090
|
-
this._checkNumberOfArguments();
|
|
2091
|
-
const processedArgs = [];
|
|
2092
|
-
this.registeredArguments.forEach((declaredArg, index) => {
|
|
2093
|
-
let value = declaredArg.defaultValue;
|
|
2094
|
-
if (declaredArg.variadic) {
|
|
2095
|
-
if (index < this.args.length) {
|
|
2096
|
-
value = this.args.slice(index);
|
|
2097
|
-
if (declaredArg.parseArg) {
|
|
2098
|
-
value = value.reduce((processed, v) => {
|
|
2099
|
-
return myParseArg(declaredArg, v, processed);
|
|
2100
|
-
}, declaredArg.defaultValue);
|
|
2101
|
-
}
|
|
2102
|
-
} else if (value === void 0) {
|
|
2103
|
-
value = [];
|
|
2104
|
-
}
|
|
2105
|
-
} else if (index < this.args.length) {
|
|
2106
|
-
value = this.args[index];
|
|
2107
|
-
if (declaredArg.parseArg) {
|
|
2108
|
-
value = myParseArg(declaredArg, value, declaredArg.defaultValue);
|
|
2109
|
-
}
|
|
2110
|
-
}
|
|
2111
|
-
processedArgs[index] = value;
|
|
2112
|
-
});
|
|
2113
|
-
this.processedArgs = processedArgs;
|
|
2114
|
-
}
|
|
2115
|
-
/**
|
|
2116
|
-
* Once we have a promise we chain, but call synchronously until then.
|
|
2117
|
-
*
|
|
2118
|
-
* @param {(Promise|undefined)} promise
|
|
2119
|
-
* @param {Function} fn
|
|
2120
|
-
* @return {(Promise|undefined)}
|
|
2121
|
-
* @private
|
|
2122
|
-
*/
|
|
2123
|
-
_chainOrCall(promise, fn) {
|
|
2124
|
-
if (promise && promise.then && typeof promise.then === "function") {
|
|
2125
|
-
return promise.then(() => fn());
|
|
2126
|
-
}
|
|
2127
|
-
return fn();
|
|
2128
|
-
}
|
|
2129
|
-
/**
|
|
2130
|
-
*
|
|
2131
|
-
* @param {(Promise|undefined)} promise
|
|
2132
|
-
* @param {string} event
|
|
2133
|
-
* @return {(Promise|undefined)}
|
|
2134
|
-
* @private
|
|
2135
|
-
*/
|
|
2136
|
-
_chainOrCallHooks(promise, event) {
|
|
2137
|
-
let result = promise;
|
|
2138
|
-
const hooks = [];
|
|
2139
|
-
this._getCommandAndAncestors().reverse().filter((cmd) => cmd._lifeCycleHooks[event] !== void 0).forEach((hookedCommand) => {
|
|
2140
|
-
hookedCommand._lifeCycleHooks[event].forEach((callback) => {
|
|
2141
|
-
hooks.push({ hookedCommand, callback });
|
|
2142
|
-
});
|
|
2143
|
-
});
|
|
2144
|
-
if (event === "postAction") {
|
|
2145
|
-
hooks.reverse();
|
|
2146
|
-
}
|
|
2147
|
-
hooks.forEach((hookDetail) => {
|
|
2148
|
-
result = this._chainOrCall(result, () => {
|
|
2149
|
-
return hookDetail.callback(hookDetail.hookedCommand, this);
|
|
2150
|
-
});
|
|
2151
|
-
});
|
|
2152
|
-
return result;
|
|
2153
|
-
}
|
|
2154
|
-
/**
|
|
2155
|
-
*
|
|
2156
|
-
* @param {(Promise|undefined)} promise
|
|
2157
|
-
* @param {Command} subCommand
|
|
2158
|
-
* @param {string} event
|
|
2159
|
-
* @return {(Promise|undefined)}
|
|
2160
|
-
* @private
|
|
2161
|
-
*/
|
|
2162
|
-
_chainOrCallSubCommandHook(promise, subCommand, event) {
|
|
2163
|
-
let result = promise;
|
|
2164
|
-
if (this._lifeCycleHooks[event] !== void 0) {
|
|
2165
|
-
this._lifeCycleHooks[event].forEach((hook) => {
|
|
2166
|
-
result = this._chainOrCall(result, () => {
|
|
2167
|
-
return hook(this, subCommand);
|
|
2168
|
-
});
|
|
2169
|
-
});
|
|
2170
|
-
}
|
|
2171
|
-
return result;
|
|
2172
|
-
}
|
|
2173
|
-
/**
|
|
2174
|
-
* Process arguments in context of this command.
|
|
2175
|
-
* Returns action result, in case it is a promise.
|
|
2176
|
-
*
|
|
2177
|
-
* @private
|
|
2178
|
-
*/
|
|
2179
|
-
_parseCommand(operands, unknown) {
|
|
2180
|
-
const parsed = this.parseOptions(unknown);
|
|
2181
|
-
this._parseOptionsEnv();
|
|
2182
|
-
this._parseOptionsImplied();
|
|
2183
|
-
operands = operands.concat(parsed.operands);
|
|
2184
|
-
unknown = parsed.unknown;
|
|
2185
|
-
this.args = operands.concat(unknown);
|
|
2186
|
-
if (operands && this._findCommand(operands[0])) {
|
|
2187
|
-
return this._dispatchSubcommand(operands[0], operands.slice(1), unknown);
|
|
2188
|
-
}
|
|
2189
|
-
if (this._getHelpCommand() && operands[0] === this._getHelpCommand().name()) {
|
|
2190
|
-
return this._dispatchHelpCommand(operands[1]);
|
|
2191
|
-
}
|
|
2192
|
-
if (this._defaultCommandName) {
|
|
2193
|
-
this._outputHelpIfRequested(unknown);
|
|
2194
|
-
return this._dispatchSubcommand(
|
|
2195
|
-
this._defaultCommandName,
|
|
2196
|
-
operands,
|
|
2197
|
-
unknown
|
|
2198
|
-
);
|
|
2199
|
-
}
|
|
2200
|
-
if (this.commands.length && this.args.length === 0 && !this._actionHandler && !this._defaultCommandName) {
|
|
2201
|
-
this.help({ error: true });
|
|
2202
|
-
}
|
|
2203
|
-
this._outputHelpIfRequested(parsed.unknown);
|
|
2204
|
-
this._checkForMissingMandatoryOptions();
|
|
2205
|
-
this._checkForConflictingOptions();
|
|
2206
|
-
const checkForUnknownOptions = () => {
|
|
2207
|
-
if (parsed.unknown.length > 0) {
|
|
2208
|
-
this.unknownOption(parsed.unknown[0]);
|
|
2209
|
-
}
|
|
2210
|
-
};
|
|
2211
|
-
const commandEvent = `command:${this.name()}`;
|
|
2212
|
-
if (this._actionHandler) {
|
|
2213
|
-
checkForUnknownOptions();
|
|
2214
|
-
this._processArguments();
|
|
2215
|
-
let promiseChain;
|
|
2216
|
-
promiseChain = this._chainOrCallHooks(promiseChain, "preAction");
|
|
2217
|
-
promiseChain = this._chainOrCall(
|
|
2218
|
-
promiseChain,
|
|
2219
|
-
() => this._actionHandler(this.processedArgs)
|
|
2220
|
-
);
|
|
2221
|
-
if (this.parent) {
|
|
2222
|
-
promiseChain = this._chainOrCall(promiseChain, () => {
|
|
2223
|
-
this.parent.emit(commandEvent, operands, unknown);
|
|
2224
|
-
});
|
|
2225
|
-
}
|
|
2226
|
-
promiseChain = this._chainOrCallHooks(promiseChain, "postAction");
|
|
2227
|
-
return promiseChain;
|
|
2228
|
-
}
|
|
2229
|
-
if (this.parent && this.parent.listenerCount(commandEvent)) {
|
|
2230
|
-
checkForUnknownOptions();
|
|
2231
|
-
this._processArguments();
|
|
2232
|
-
this.parent.emit(commandEvent, operands, unknown);
|
|
2233
|
-
} else if (operands.length) {
|
|
2234
|
-
if (this._findCommand("*")) {
|
|
2235
|
-
return this._dispatchSubcommand("*", operands, unknown);
|
|
2236
|
-
}
|
|
2237
|
-
if (this.listenerCount("command:*")) {
|
|
2238
|
-
this.emit("command:*", operands, unknown);
|
|
2239
|
-
} else if (this.commands.length) {
|
|
2240
|
-
this.unknownCommand();
|
|
2241
|
-
} else {
|
|
2242
|
-
checkForUnknownOptions();
|
|
2243
|
-
this._processArguments();
|
|
2244
|
-
}
|
|
2245
|
-
} else if (this.commands.length) {
|
|
2246
|
-
checkForUnknownOptions();
|
|
2247
|
-
this.help({ error: true });
|
|
2248
|
-
} else {
|
|
2249
|
-
checkForUnknownOptions();
|
|
2250
|
-
this._processArguments();
|
|
2251
|
-
}
|
|
2252
|
-
}
|
|
2253
|
-
/**
|
|
2254
|
-
* Find matching command.
|
|
2255
|
-
*
|
|
2256
|
-
* @private
|
|
2257
|
-
* @return {Command | undefined}
|
|
2258
|
-
*/
|
|
2259
|
-
_findCommand(name) {
|
|
2260
|
-
if (!name) return void 0;
|
|
2261
|
-
return this.commands.find(
|
|
2262
|
-
(cmd) => cmd._name === name || cmd._aliases.includes(name)
|
|
2263
|
-
);
|
|
2264
|
-
}
|
|
2265
|
-
/**
|
|
2266
|
-
* Return an option matching `arg` if any.
|
|
2267
|
-
*
|
|
2268
|
-
* @param {string} arg
|
|
2269
|
-
* @return {Option}
|
|
2270
|
-
* @package
|
|
2271
|
-
*/
|
|
2272
|
-
_findOption(arg) {
|
|
2273
|
-
return this.options.find((option) => option.is(arg));
|
|
2274
|
-
}
|
|
2275
|
-
/**
|
|
2276
|
-
* Display an error message if a mandatory option does not have a value.
|
|
2277
|
-
* Called after checking for help flags in leaf subcommand.
|
|
2278
|
-
*
|
|
2279
|
-
* @private
|
|
2280
|
-
*/
|
|
2281
|
-
_checkForMissingMandatoryOptions() {
|
|
2282
|
-
this._getCommandAndAncestors().forEach((cmd) => {
|
|
2283
|
-
cmd.options.forEach((anOption) => {
|
|
2284
|
-
if (anOption.mandatory && cmd.getOptionValue(anOption.attributeName()) === void 0) {
|
|
2285
|
-
cmd.missingMandatoryOptionValue(anOption);
|
|
2286
|
-
}
|
|
2287
|
-
});
|
|
2288
|
-
});
|
|
2289
|
-
}
|
|
2290
|
-
/**
|
|
2291
|
-
* Display an error message if conflicting options are used together in this.
|
|
2292
|
-
*
|
|
2293
|
-
* @private
|
|
2294
|
-
*/
|
|
2295
|
-
_checkForConflictingLocalOptions() {
|
|
2296
|
-
const definedNonDefaultOptions = this.options.filter((option) => {
|
|
2297
|
-
const optionKey = option.attributeName();
|
|
2298
|
-
if (this.getOptionValue(optionKey) === void 0) {
|
|
2299
|
-
return false;
|
|
2300
|
-
}
|
|
2301
|
-
return this.getOptionValueSource(optionKey) !== "default";
|
|
2302
|
-
});
|
|
2303
|
-
const optionsWithConflicting = definedNonDefaultOptions.filter(
|
|
2304
|
-
(option) => option.conflictsWith.length > 0
|
|
2305
|
-
);
|
|
2306
|
-
optionsWithConflicting.forEach((option) => {
|
|
2307
|
-
const conflictingAndDefined = definedNonDefaultOptions.find(
|
|
2308
|
-
(defined) => option.conflictsWith.includes(defined.attributeName())
|
|
2309
|
-
);
|
|
2310
|
-
if (conflictingAndDefined) {
|
|
2311
|
-
this._conflictingOption(option, conflictingAndDefined);
|
|
2312
|
-
}
|
|
2313
|
-
});
|
|
2314
|
-
}
|
|
2315
|
-
/**
|
|
2316
|
-
* Display an error message if conflicting options are used together.
|
|
2317
|
-
* Called after checking for help flags in leaf subcommand.
|
|
2318
|
-
*
|
|
2319
|
-
* @private
|
|
2320
|
-
*/
|
|
2321
|
-
_checkForConflictingOptions() {
|
|
2322
|
-
this._getCommandAndAncestors().forEach((cmd) => {
|
|
2323
|
-
cmd._checkForConflictingLocalOptions();
|
|
2324
|
-
});
|
|
2325
|
-
}
|
|
2326
|
-
/**
|
|
2327
|
-
* Parse options from `argv` removing known options,
|
|
2328
|
-
* and return argv split into operands and unknown arguments.
|
|
2329
|
-
*
|
|
2330
|
-
* Examples:
|
|
2331
|
-
*
|
|
2332
|
-
* argv => operands, unknown
|
|
2333
|
-
* --known kkk op => [op], []
|
|
2334
|
-
* op --known kkk => [op], []
|
|
2335
|
-
* sub --unknown uuu op => [sub], [--unknown uuu op]
|
|
2336
|
-
* sub -- --unknown uuu op => [sub --unknown uuu op], []
|
|
2337
|
-
*
|
|
2338
|
-
* @param {string[]} argv
|
|
2339
|
-
* @return {{operands: string[], unknown: string[]}}
|
|
2340
|
-
*/
|
|
2341
|
-
parseOptions(argv) {
|
|
2342
|
-
const operands = [];
|
|
2343
|
-
const unknown = [];
|
|
2344
|
-
let dest = operands;
|
|
2345
|
-
const args = argv.slice();
|
|
2346
|
-
function maybeOption(arg) {
|
|
2347
|
-
return arg.length > 1 && arg[0] === "-";
|
|
2348
|
-
}
|
|
2349
|
-
let activeVariadicOption = null;
|
|
2350
|
-
while (args.length) {
|
|
2351
|
-
const arg = args.shift();
|
|
2352
|
-
if (arg === "--") {
|
|
2353
|
-
if (dest === unknown) dest.push(arg);
|
|
2354
|
-
dest.push(...args);
|
|
2355
|
-
break;
|
|
2356
|
-
}
|
|
2357
|
-
if (activeVariadicOption && !maybeOption(arg)) {
|
|
2358
|
-
this.emit(`option:${activeVariadicOption.name()}`, arg);
|
|
2359
|
-
continue;
|
|
2360
|
-
}
|
|
2361
|
-
activeVariadicOption = null;
|
|
2362
|
-
if (maybeOption(arg)) {
|
|
2363
|
-
const option = this._findOption(arg);
|
|
2364
|
-
if (option) {
|
|
2365
|
-
if (option.required) {
|
|
2366
|
-
const value = args.shift();
|
|
2367
|
-
if (value === void 0) this.optionMissingArgument(option);
|
|
2368
|
-
this.emit(`option:${option.name()}`, value);
|
|
2369
|
-
} else if (option.optional) {
|
|
2370
|
-
let value = null;
|
|
2371
|
-
if (args.length > 0 && !maybeOption(args[0])) {
|
|
2372
|
-
value = args.shift();
|
|
2373
|
-
}
|
|
2374
|
-
this.emit(`option:${option.name()}`, value);
|
|
2375
|
-
} else {
|
|
2376
|
-
this.emit(`option:${option.name()}`);
|
|
2377
|
-
}
|
|
2378
|
-
activeVariadicOption = option.variadic ? option : null;
|
|
2379
|
-
continue;
|
|
2380
|
-
}
|
|
2381
|
-
}
|
|
2382
|
-
if (arg.length > 2 && arg[0] === "-" && arg[1] !== "-") {
|
|
2383
|
-
const option = this._findOption(`-${arg[1]}`);
|
|
2384
|
-
if (option) {
|
|
2385
|
-
if (option.required || option.optional && this._combineFlagAndOptionalValue) {
|
|
2386
|
-
this.emit(`option:${option.name()}`, arg.slice(2));
|
|
2387
|
-
} else {
|
|
2388
|
-
this.emit(`option:${option.name()}`);
|
|
2389
|
-
args.unshift(`-${arg.slice(2)}`);
|
|
2390
|
-
}
|
|
2391
|
-
continue;
|
|
2392
|
-
}
|
|
2393
|
-
}
|
|
2394
|
-
if (/^--[^=]+=/.test(arg)) {
|
|
2395
|
-
const index = arg.indexOf("=");
|
|
2396
|
-
const option = this._findOption(arg.slice(0, index));
|
|
2397
|
-
if (option && (option.required || option.optional)) {
|
|
2398
|
-
this.emit(`option:${option.name()}`, arg.slice(index + 1));
|
|
2399
|
-
continue;
|
|
2400
|
-
}
|
|
2401
|
-
}
|
|
2402
|
-
if (maybeOption(arg)) {
|
|
2403
|
-
dest = unknown;
|
|
2404
|
-
}
|
|
2405
|
-
if ((this._enablePositionalOptions || this._passThroughOptions) && operands.length === 0 && unknown.length === 0) {
|
|
2406
|
-
if (this._findCommand(arg)) {
|
|
2407
|
-
operands.push(arg);
|
|
2408
|
-
if (args.length > 0) unknown.push(...args);
|
|
2409
|
-
break;
|
|
2410
|
-
} else if (this._getHelpCommand() && arg === this._getHelpCommand().name()) {
|
|
2411
|
-
operands.push(arg);
|
|
2412
|
-
if (args.length > 0) operands.push(...args);
|
|
2413
|
-
break;
|
|
2414
|
-
} else if (this._defaultCommandName) {
|
|
2415
|
-
unknown.push(arg);
|
|
2416
|
-
if (args.length > 0) unknown.push(...args);
|
|
2417
|
-
break;
|
|
2418
|
-
}
|
|
2419
|
-
}
|
|
2420
|
-
if (this._passThroughOptions) {
|
|
2421
|
-
dest.push(arg);
|
|
2422
|
-
if (args.length > 0) dest.push(...args);
|
|
2423
|
-
break;
|
|
2424
|
-
}
|
|
2425
|
-
dest.push(arg);
|
|
2426
|
-
}
|
|
2427
|
-
return { operands, unknown };
|
|
2428
|
-
}
|
|
2429
|
-
/**
|
|
2430
|
-
* Return an object containing local option values as key-value pairs.
|
|
2431
|
-
*
|
|
2432
|
-
* @return {object}
|
|
2433
|
-
*/
|
|
2434
|
-
opts() {
|
|
2435
|
-
if (this._storeOptionsAsProperties) {
|
|
2436
|
-
const result = {};
|
|
2437
|
-
const len = this.options.length;
|
|
2438
|
-
for (let i = 0; i < len; i++) {
|
|
2439
|
-
const key = this.options[i].attributeName();
|
|
2440
|
-
result[key] = key === this._versionOptionName ? this._version : this[key];
|
|
2441
|
-
}
|
|
2442
|
-
return result;
|
|
2443
|
-
}
|
|
2444
|
-
return this._optionValues;
|
|
2445
|
-
}
|
|
2446
|
-
/**
|
|
2447
|
-
* Return an object containing merged local and global option values as key-value pairs.
|
|
2448
|
-
*
|
|
2449
|
-
* @return {object}
|
|
2450
|
-
*/
|
|
2451
|
-
optsWithGlobals() {
|
|
2452
|
-
return this._getCommandAndAncestors().reduce(
|
|
2453
|
-
(combinedOptions, cmd) => Object.assign(combinedOptions, cmd.opts()),
|
|
2454
|
-
{}
|
|
2455
|
-
);
|
|
2456
|
-
}
|
|
2457
|
-
/**
|
|
2458
|
-
* Display error message and exit (or call exitOverride).
|
|
2459
|
-
*
|
|
2460
|
-
* @param {string} message
|
|
2461
|
-
* @param {object} [errorOptions]
|
|
2462
|
-
* @param {string} [errorOptions.code] - an id string representing the error
|
|
2463
|
-
* @param {number} [errorOptions.exitCode] - used with process.exit
|
|
2464
|
-
*/
|
|
2465
|
-
error(message, errorOptions) {
|
|
2466
|
-
this._outputConfiguration.outputError(
|
|
2467
|
-
`${message}
|
|
2468
|
-
`,
|
|
2469
|
-
this._outputConfiguration.writeErr
|
|
2470
|
-
);
|
|
2471
|
-
if (typeof this._showHelpAfterError === "string") {
|
|
2472
|
-
this._outputConfiguration.writeErr(`${this._showHelpAfterError}
|
|
2473
|
-
`);
|
|
2474
|
-
} else if (this._showHelpAfterError) {
|
|
2475
|
-
this._outputConfiguration.writeErr("\n");
|
|
2476
|
-
this.outputHelp({ error: true });
|
|
2477
|
-
}
|
|
2478
|
-
const config = errorOptions || {};
|
|
2479
|
-
const exitCode = config.exitCode || 1;
|
|
2480
|
-
const code = config.code || "commander.error";
|
|
2481
|
-
this._exit(exitCode, code, message);
|
|
2482
|
-
}
|
|
2483
|
-
/**
|
|
2484
|
-
* Apply any option related environment variables, if option does
|
|
2485
|
-
* not have a value from cli or client code.
|
|
2486
|
-
*
|
|
2487
|
-
* @private
|
|
2488
|
-
*/
|
|
2489
|
-
_parseOptionsEnv() {
|
|
2490
|
-
this.options.forEach((option) => {
|
|
2491
|
-
if (option.envVar && option.envVar in process2.env) {
|
|
2492
|
-
const optionKey = option.attributeName();
|
|
2493
|
-
if (this.getOptionValue(optionKey) === void 0 || ["default", "config", "env"].includes(
|
|
2494
|
-
this.getOptionValueSource(optionKey)
|
|
2495
|
-
)) {
|
|
2496
|
-
if (option.required || option.optional) {
|
|
2497
|
-
this.emit(`optionEnv:${option.name()}`, process2.env[option.envVar]);
|
|
2498
|
-
} else {
|
|
2499
|
-
this.emit(`optionEnv:${option.name()}`);
|
|
2500
|
-
}
|
|
2501
|
-
}
|
|
2502
|
-
}
|
|
2503
|
-
});
|
|
2504
|
-
}
|
|
2505
|
-
/**
|
|
2506
|
-
* Apply any implied option values, if option is undefined or default value.
|
|
2507
|
-
*
|
|
2508
|
-
* @private
|
|
2509
|
-
*/
|
|
2510
|
-
_parseOptionsImplied() {
|
|
2511
|
-
const dualHelper = new DualOptions(this.options);
|
|
2512
|
-
const hasCustomOptionValue = (optionKey) => {
|
|
2513
|
-
return this.getOptionValue(optionKey) !== void 0 && !["default", "implied"].includes(this.getOptionValueSource(optionKey));
|
|
2514
|
-
};
|
|
2515
|
-
this.options.filter(
|
|
2516
|
-
(option) => option.implied !== void 0 && hasCustomOptionValue(option.attributeName()) && dualHelper.valueFromOption(
|
|
2517
|
-
this.getOptionValue(option.attributeName()),
|
|
2518
|
-
option
|
|
2519
|
-
)
|
|
2520
|
-
).forEach((option) => {
|
|
2521
|
-
Object.keys(option.implied).filter((impliedKey) => !hasCustomOptionValue(impliedKey)).forEach((impliedKey) => {
|
|
2522
|
-
this.setOptionValueWithSource(
|
|
2523
|
-
impliedKey,
|
|
2524
|
-
option.implied[impliedKey],
|
|
2525
|
-
"implied"
|
|
2526
|
-
);
|
|
2527
|
-
});
|
|
2528
|
-
});
|
|
2529
|
-
}
|
|
2530
|
-
/**
|
|
2531
|
-
* Argument `name` is missing.
|
|
2532
|
-
*
|
|
2533
|
-
* @param {string} name
|
|
2534
|
-
* @private
|
|
2535
|
-
*/
|
|
2536
|
-
missingArgument(name) {
|
|
2537
|
-
const message = `error: missing required argument '${name}'`;
|
|
2538
|
-
this.error(message, { code: "commander.missingArgument" });
|
|
2539
|
-
}
|
|
2540
|
-
/**
|
|
2541
|
-
* `Option` is missing an argument.
|
|
2542
|
-
*
|
|
2543
|
-
* @param {Option} option
|
|
2544
|
-
* @private
|
|
2545
|
-
*/
|
|
2546
|
-
optionMissingArgument(option) {
|
|
2547
|
-
const message = `error: option '${option.flags}' argument missing`;
|
|
2548
|
-
this.error(message, { code: "commander.optionMissingArgument" });
|
|
2549
|
-
}
|
|
2550
|
-
/**
|
|
2551
|
-
* `Option` does not have a value, and is a mandatory option.
|
|
2552
|
-
*
|
|
2553
|
-
* @param {Option} option
|
|
2554
|
-
* @private
|
|
2555
|
-
*/
|
|
2556
|
-
missingMandatoryOptionValue(option) {
|
|
2557
|
-
const message = `error: required option '${option.flags}' not specified`;
|
|
2558
|
-
this.error(message, { code: "commander.missingMandatoryOptionValue" });
|
|
2559
|
-
}
|
|
2560
|
-
/**
|
|
2561
|
-
* `Option` conflicts with another option.
|
|
2562
|
-
*
|
|
2563
|
-
* @param {Option} option
|
|
2564
|
-
* @param {Option} conflictingOption
|
|
2565
|
-
* @private
|
|
2566
|
-
*/
|
|
2567
|
-
_conflictingOption(option, conflictingOption) {
|
|
2568
|
-
const findBestOptionFromValue = (option2) => {
|
|
2569
|
-
const optionKey = option2.attributeName();
|
|
2570
|
-
const optionValue = this.getOptionValue(optionKey);
|
|
2571
|
-
const negativeOption = this.options.find(
|
|
2572
|
-
(target) => target.negate && optionKey === target.attributeName()
|
|
2573
|
-
);
|
|
2574
|
-
const positiveOption = this.options.find(
|
|
2575
|
-
(target) => !target.negate && optionKey === target.attributeName()
|
|
2576
|
-
);
|
|
2577
|
-
if (negativeOption && (negativeOption.presetArg === void 0 && optionValue === false || negativeOption.presetArg !== void 0 && optionValue === negativeOption.presetArg)) {
|
|
2578
|
-
return negativeOption;
|
|
2579
|
-
}
|
|
2580
|
-
return positiveOption || option2;
|
|
2581
|
-
};
|
|
2582
|
-
const getErrorMessage = (option2) => {
|
|
2583
|
-
const bestOption = findBestOptionFromValue(option2);
|
|
2584
|
-
const optionKey = bestOption.attributeName();
|
|
2585
|
-
const source = this.getOptionValueSource(optionKey);
|
|
2586
|
-
if (source === "env") {
|
|
2587
|
-
return `environment variable '${bestOption.envVar}'`;
|
|
2588
|
-
}
|
|
2589
|
-
return `option '${bestOption.flags}'`;
|
|
2590
|
-
};
|
|
2591
|
-
const message = `error: ${getErrorMessage(option)} cannot be used with ${getErrorMessage(conflictingOption)}`;
|
|
2592
|
-
this.error(message, { code: "commander.conflictingOption" });
|
|
2593
|
-
}
|
|
2594
|
-
/**
|
|
2595
|
-
* Unknown option `flag`.
|
|
2596
|
-
*
|
|
2597
|
-
* @param {string} flag
|
|
2598
|
-
* @private
|
|
2599
|
-
*/
|
|
2600
|
-
unknownOption(flag) {
|
|
2601
|
-
if (this._allowUnknownOption) return;
|
|
2602
|
-
let suggestion = "";
|
|
2603
|
-
if (flag.startsWith("--") && this._showSuggestionAfterError) {
|
|
2604
|
-
let candidateFlags = [];
|
|
2605
|
-
let command = this;
|
|
2606
|
-
do {
|
|
2607
|
-
const moreFlags = command.createHelp().visibleOptions(command).filter((option) => option.long).map((option) => option.long);
|
|
2608
|
-
candidateFlags = candidateFlags.concat(moreFlags);
|
|
2609
|
-
command = command.parent;
|
|
2610
|
-
} while (command && !command._enablePositionalOptions);
|
|
2611
|
-
suggestion = suggestSimilar(flag, candidateFlags);
|
|
2612
|
-
}
|
|
2613
|
-
const message = `error: unknown option '${flag}'${suggestion}`;
|
|
2614
|
-
this.error(message, { code: "commander.unknownOption" });
|
|
2615
|
-
}
|
|
2616
|
-
/**
|
|
2617
|
-
* Excess arguments, more than expected.
|
|
2618
|
-
*
|
|
2619
|
-
* @param {string[]} receivedArgs
|
|
2620
|
-
* @private
|
|
2621
|
-
*/
|
|
2622
|
-
_excessArguments(receivedArgs) {
|
|
2623
|
-
if (this._allowExcessArguments) return;
|
|
2624
|
-
const expected = this.registeredArguments.length;
|
|
2625
|
-
const s = expected === 1 ? "" : "s";
|
|
2626
|
-
const forSubcommand = this.parent ? ` for '${this.name()}'` : "";
|
|
2627
|
-
const message = `error: too many arguments${forSubcommand}. Expected ${expected} argument${s} but got ${receivedArgs.length}.`;
|
|
2628
|
-
this.error(message, { code: "commander.excessArguments" });
|
|
2629
|
-
}
|
|
2630
|
-
/**
|
|
2631
|
-
* Unknown command.
|
|
2632
|
-
*
|
|
2633
|
-
* @private
|
|
2634
|
-
*/
|
|
2635
|
-
unknownCommand() {
|
|
2636
|
-
const unknownName = this.args[0];
|
|
2637
|
-
let suggestion = "";
|
|
2638
|
-
if (this._showSuggestionAfterError) {
|
|
2639
|
-
const candidateNames = [];
|
|
2640
|
-
this.createHelp().visibleCommands(this).forEach((command) => {
|
|
2641
|
-
candidateNames.push(command.name());
|
|
2642
|
-
if (command.alias()) candidateNames.push(command.alias());
|
|
2643
|
-
});
|
|
2644
|
-
suggestion = suggestSimilar(unknownName, candidateNames);
|
|
2645
|
-
}
|
|
2646
|
-
const message = `error: unknown command '${unknownName}'${suggestion}`;
|
|
2647
|
-
this.error(message, { code: "commander.unknownCommand" });
|
|
2648
|
-
}
|
|
2649
|
-
/**
|
|
2650
|
-
* Get or set the program version.
|
|
2651
|
-
*
|
|
2652
|
-
* This method auto-registers the "-V, --version" option which will print the version number.
|
|
2653
|
-
*
|
|
2654
|
-
* You can optionally supply the flags and description to override the defaults.
|
|
2655
|
-
*
|
|
2656
|
-
* @param {string} [str]
|
|
2657
|
-
* @param {string} [flags]
|
|
2658
|
-
* @param {string} [description]
|
|
2659
|
-
* @return {(this | string | undefined)} `this` command for chaining, or version string if no arguments
|
|
2660
|
-
*/
|
|
2661
|
-
version(str, flags, description) {
|
|
2662
|
-
if (str === void 0) return this._version;
|
|
2663
|
-
this._version = str;
|
|
2664
|
-
flags = flags || "-V, --version";
|
|
2665
|
-
description = description || "output the version number";
|
|
2666
|
-
const versionOption = this.createOption(flags, description);
|
|
2667
|
-
this._versionOptionName = versionOption.attributeName();
|
|
2668
|
-
this._registerOption(versionOption);
|
|
2669
|
-
this.on("option:" + versionOption.name(), () => {
|
|
2670
|
-
this._outputConfiguration.writeOut(`${str}
|
|
2671
|
-
`);
|
|
2672
|
-
this._exit(0, "commander.version", str);
|
|
2673
|
-
});
|
|
2674
|
-
return this;
|
|
2675
|
-
}
|
|
2676
|
-
/**
|
|
2677
|
-
* Set the description.
|
|
2678
|
-
*
|
|
2679
|
-
* @param {string} [str]
|
|
2680
|
-
* @param {object} [argsDescription]
|
|
2681
|
-
* @return {(string|Command)}
|
|
2682
|
-
*/
|
|
2683
|
-
description(str, argsDescription) {
|
|
2684
|
-
if (str === void 0 && argsDescription === void 0)
|
|
2685
|
-
return this._description;
|
|
2686
|
-
this._description = str;
|
|
2687
|
-
if (argsDescription) {
|
|
2688
|
-
this._argsDescription = argsDescription;
|
|
2689
|
-
}
|
|
2690
|
-
return this;
|
|
2691
|
-
}
|
|
2692
|
-
/**
|
|
2693
|
-
* Set the summary. Used when listed as subcommand of parent.
|
|
2694
|
-
*
|
|
2695
|
-
* @param {string} [str]
|
|
2696
|
-
* @return {(string|Command)}
|
|
2697
|
-
*/
|
|
2698
|
-
summary(str) {
|
|
2699
|
-
if (str === void 0) return this._summary;
|
|
2700
|
-
this._summary = str;
|
|
2701
|
-
return this;
|
|
2702
|
-
}
|
|
2703
|
-
/**
|
|
2704
|
-
* Set an alias for the command.
|
|
2705
|
-
*
|
|
2706
|
-
* You may call more than once to add multiple aliases. Only the first alias is shown in the auto-generated help.
|
|
2707
|
-
*
|
|
2708
|
-
* @param {string} [alias]
|
|
2709
|
-
* @return {(string|Command)}
|
|
2710
|
-
*/
|
|
2711
|
-
alias(alias) {
|
|
2712
|
-
if (alias === void 0) return this._aliases[0];
|
|
2713
|
-
let command = this;
|
|
2714
|
-
if (this.commands.length !== 0 && this.commands[this.commands.length - 1]._executableHandler) {
|
|
2715
|
-
command = this.commands[this.commands.length - 1];
|
|
2716
|
-
}
|
|
2717
|
-
if (alias === command._name)
|
|
2718
|
-
throw new Error("Command alias can't be the same as its name");
|
|
2719
|
-
const matchingCommand = this.parent?._findCommand(alias);
|
|
2720
|
-
if (matchingCommand) {
|
|
2721
|
-
const existingCmd = [matchingCommand.name()].concat(matchingCommand.aliases()).join("|");
|
|
2722
|
-
throw new Error(
|
|
2723
|
-
`cannot add alias '${alias}' to command '${this.name()}' as already have command '${existingCmd}'`
|
|
2724
|
-
);
|
|
2725
|
-
}
|
|
2726
|
-
command._aliases.push(alias);
|
|
2727
|
-
return this;
|
|
2728
|
-
}
|
|
2729
|
-
/**
|
|
2730
|
-
* Set aliases for the command.
|
|
2731
|
-
*
|
|
2732
|
-
* Only the first alias is shown in the auto-generated help.
|
|
2733
|
-
*
|
|
2734
|
-
* @param {string[]} [aliases]
|
|
2735
|
-
* @return {(string[]|Command)}
|
|
2736
|
-
*/
|
|
2737
|
-
aliases(aliases) {
|
|
2738
|
-
if (aliases === void 0) return this._aliases;
|
|
2739
|
-
aliases.forEach((alias) => this.alias(alias));
|
|
2740
|
-
return this;
|
|
2741
|
-
}
|
|
2742
|
-
/**
|
|
2743
|
-
* Set / get the command usage `str`.
|
|
2744
|
-
*
|
|
2745
|
-
* @param {string} [str]
|
|
2746
|
-
* @return {(string|Command)}
|
|
2747
|
-
*/
|
|
2748
|
-
usage(str) {
|
|
2749
|
-
if (str === void 0) {
|
|
2750
|
-
if (this._usage) return this._usage;
|
|
2751
|
-
const args = this.registeredArguments.map((arg) => {
|
|
2752
|
-
return humanReadableArgName(arg);
|
|
2753
|
-
});
|
|
2754
|
-
return [].concat(
|
|
2755
|
-
this.options.length || this._helpOption !== null ? "[options]" : [],
|
|
2756
|
-
this.commands.length ? "[command]" : [],
|
|
2757
|
-
this.registeredArguments.length ? args : []
|
|
2758
|
-
).join(" ");
|
|
2759
|
-
}
|
|
2760
|
-
this._usage = str;
|
|
2761
|
-
return this;
|
|
2762
|
-
}
|
|
2763
|
-
/**
|
|
2764
|
-
* Get or set the name of the command.
|
|
2765
|
-
*
|
|
2766
|
-
* @param {string} [str]
|
|
2767
|
-
* @return {(string|Command)}
|
|
2768
|
-
*/
|
|
2769
|
-
name(str) {
|
|
2770
|
-
if (str === void 0) return this._name;
|
|
2771
|
-
this._name = str;
|
|
2772
|
-
return this;
|
|
2773
|
-
}
|
|
2774
|
-
/**
|
|
2775
|
-
* Set the name of the command from script filename, such as process.argv[1],
|
|
2776
|
-
* or require.main.filename, or __filename.
|
|
2777
|
-
*
|
|
2778
|
-
* (Used internally and public although not documented in README.)
|
|
2779
|
-
*
|
|
2780
|
-
* @example
|
|
2781
|
-
* program.nameFromFilename(require.main.filename);
|
|
2782
|
-
*
|
|
2783
|
-
* @param {string} filename
|
|
2784
|
-
* @return {Command}
|
|
2785
|
-
*/
|
|
2786
|
-
nameFromFilename(filename) {
|
|
2787
|
-
this._name = path13.basename(filename, path13.extname(filename));
|
|
2788
|
-
return this;
|
|
2789
|
-
}
|
|
2790
|
-
/**
|
|
2791
|
-
* Get or set the directory for searching for executable subcommands of this command.
|
|
2792
|
-
*
|
|
2793
|
-
* @example
|
|
2794
|
-
* program.executableDir(__dirname);
|
|
2795
|
-
* // or
|
|
2796
|
-
* program.executableDir('subcommands');
|
|
2797
|
-
*
|
|
2798
|
-
* @param {string} [path]
|
|
2799
|
-
* @return {(string|null|Command)}
|
|
2800
|
-
*/
|
|
2801
|
-
executableDir(path14) {
|
|
2802
|
-
if (path14 === void 0) return this._executableDir;
|
|
2803
|
-
this._executableDir = path14;
|
|
2804
|
-
return this;
|
|
2805
|
-
}
|
|
2806
|
-
/**
|
|
2807
|
-
* Return program help documentation.
|
|
2808
|
-
*
|
|
2809
|
-
* @param {{ error: boolean }} [contextOptions] - pass {error:true} to wrap for stderr instead of stdout
|
|
2810
|
-
* @return {string}
|
|
2811
|
-
*/
|
|
2812
|
-
helpInformation(contextOptions) {
|
|
2813
|
-
const helper = this.createHelp();
|
|
2814
|
-
if (helper.helpWidth === void 0) {
|
|
2815
|
-
helper.helpWidth = contextOptions && contextOptions.error ? this._outputConfiguration.getErrHelpWidth() : this._outputConfiguration.getOutHelpWidth();
|
|
2816
|
-
}
|
|
2817
|
-
return helper.formatHelp(this, helper);
|
|
2818
|
-
}
|
|
2819
|
-
/**
|
|
2820
|
-
* @private
|
|
2821
|
-
*/
|
|
2822
|
-
_getHelpContext(contextOptions) {
|
|
2823
|
-
contextOptions = contextOptions || {};
|
|
2824
|
-
const context = { error: !!contextOptions.error };
|
|
2825
|
-
let write;
|
|
2826
|
-
if (context.error) {
|
|
2827
|
-
write = (arg) => this._outputConfiguration.writeErr(arg);
|
|
2828
|
-
} else {
|
|
2829
|
-
write = (arg) => this._outputConfiguration.writeOut(arg);
|
|
2830
|
-
}
|
|
2831
|
-
context.write = contextOptions.write || write;
|
|
2832
|
-
context.command = this;
|
|
2833
|
-
return context;
|
|
2834
|
-
}
|
|
2835
|
-
/**
|
|
2836
|
-
* Output help information for this command.
|
|
2837
|
-
*
|
|
2838
|
-
* Outputs built-in help, and custom text added using `.addHelpText()`.
|
|
2839
|
-
*
|
|
2840
|
-
* @param {{ error: boolean } | Function} [contextOptions] - pass {error:true} to write to stderr instead of stdout
|
|
2841
|
-
*/
|
|
2842
|
-
outputHelp(contextOptions) {
|
|
2843
|
-
let deprecatedCallback;
|
|
2844
|
-
if (typeof contextOptions === "function") {
|
|
2845
|
-
deprecatedCallback = contextOptions;
|
|
2846
|
-
contextOptions = void 0;
|
|
2847
|
-
}
|
|
2848
|
-
const context = this._getHelpContext(contextOptions);
|
|
2849
|
-
this._getCommandAndAncestors().reverse().forEach((command) => command.emit("beforeAllHelp", context));
|
|
2850
|
-
this.emit("beforeHelp", context);
|
|
2851
|
-
let helpInformation = this.helpInformation(context);
|
|
2852
|
-
if (deprecatedCallback) {
|
|
2853
|
-
helpInformation = deprecatedCallback(helpInformation);
|
|
2854
|
-
if (typeof helpInformation !== "string" && !Buffer.isBuffer(helpInformation)) {
|
|
2855
|
-
throw new Error("outputHelp callback must return a string or a Buffer");
|
|
2856
|
-
}
|
|
2857
|
-
}
|
|
2858
|
-
context.write(helpInformation);
|
|
2859
|
-
if (this._getHelpOption()?.long) {
|
|
2860
|
-
this.emit(this._getHelpOption().long);
|
|
2861
|
-
}
|
|
2862
|
-
this.emit("afterHelp", context);
|
|
2863
|
-
this._getCommandAndAncestors().forEach(
|
|
2864
|
-
(command) => command.emit("afterAllHelp", context)
|
|
2865
|
-
);
|
|
2866
|
-
}
|
|
2867
|
-
/**
|
|
2868
|
-
* You can pass in flags and a description to customise the built-in help option.
|
|
2869
|
-
* Pass in false to disable the built-in help option.
|
|
2870
|
-
*
|
|
2871
|
-
* @example
|
|
2872
|
-
* program.helpOption('-?, --help' 'show help'); // customise
|
|
2873
|
-
* program.helpOption(false); // disable
|
|
2874
|
-
*
|
|
2875
|
-
* @param {(string | boolean)} flags
|
|
2876
|
-
* @param {string} [description]
|
|
2877
|
-
* @return {Command} `this` command for chaining
|
|
2878
|
-
*/
|
|
2879
|
-
helpOption(flags, description) {
|
|
2880
|
-
if (typeof flags === "boolean") {
|
|
2881
|
-
if (flags) {
|
|
2882
|
-
this._helpOption = this._helpOption ?? void 0;
|
|
2883
|
-
} else {
|
|
2884
|
-
this._helpOption = null;
|
|
2885
|
-
}
|
|
2886
|
-
return this;
|
|
2887
|
-
}
|
|
2888
|
-
flags = flags ?? "-h, --help";
|
|
2889
|
-
description = description ?? "display help for command";
|
|
2890
|
-
this._helpOption = this.createOption(flags, description);
|
|
2891
|
-
return this;
|
|
2892
|
-
}
|
|
2893
|
-
/**
|
|
2894
|
-
* Lazy create help option.
|
|
2895
|
-
* Returns null if has been disabled with .helpOption(false).
|
|
2896
|
-
*
|
|
2897
|
-
* @returns {(Option | null)} the help option
|
|
2898
|
-
* @package
|
|
2899
|
-
*/
|
|
2900
|
-
_getHelpOption() {
|
|
2901
|
-
if (this._helpOption === void 0) {
|
|
2902
|
-
this.helpOption(void 0, void 0);
|
|
2903
|
-
}
|
|
2904
|
-
return this._helpOption;
|
|
2905
|
-
}
|
|
2906
|
-
/**
|
|
2907
|
-
* Supply your own option to use for the built-in help option.
|
|
2908
|
-
* This is an alternative to using helpOption() to customise the flags and description etc.
|
|
2909
|
-
*
|
|
2910
|
-
* @param {Option} option
|
|
2911
|
-
* @return {Command} `this` command for chaining
|
|
2912
|
-
*/
|
|
2913
|
-
addHelpOption(option) {
|
|
2914
|
-
this._helpOption = option;
|
|
2915
|
-
return this;
|
|
2916
|
-
}
|
|
2917
|
-
/**
|
|
2918
|
-
* Output help information and exit.
|
|
2919
|
-
*
|
|
2920
|
-
* Outputs built-in help, and custom text added using `.addHelpText()`.
|
|
2921
|
-
*
|
|
2922
|
-
* @param {{ error: boolean }} [contextOptions] - pass {error:true} to write to stderr instead of stdout
|
|
2923
|
-
*/
|
|
2924
|
-
help(contextOptions) {
|
|
2925
|
-
this.outputHelp(contextOptions);
|
|
2926
|
-
let exitCode = process2.exitCode || 0;
|
|
2927
|
-
if (exitCode === 0 && contextOptions && typeof contextOptions !== "function" && contextOptions.error) {
|
|
2928
|
-
exitCode = 1;
|
|
2929
|
-
}
|
|
2930
|
-
this._exit(exitCode, "commander.help", "(outputHelp)");
|
|
2931
|
-
}
|
|
2932
|
-
/**
|
|
2933
|
-
* Add additional text to be displayed with the built-in help.
|
|
2934
|
-
*
|
|
2935
|
-
* Position is 'before' or 'after' to affect just this command,
|
|
2936
|
-
* and 'beforeAll' or 'afterAll' to affect this command and all its subcommands.
|
|
2937
|
-
*
|
|
2938
|
-
* @param {string} position - before or after built-in help
|
|
2939
|
-
* @param {(string | Function)} text - string to add, or a function returning a string
|
|
2940
|
-
* @return {Command} `this` command for chaining
|
|
2941
|
-
*/
|
|
2942
|
-
addHelpText(position, text) {
|
|
2943
|
-
const allowedValues = ["beforeAll", "before", "after", "afterAll"];
|
|
2944
|
-
if (!allowedValues.includes(position)) {
|
|
2945
|
-
throw new Error(`Unexpected value for position to addHelpText.
|
|
2946
|
-
Expecting one of '${allowedValues.join("', '")}'`);
|
|
2947
|
-
}
|
|
2948
|
-
const helpEvent = `${position}Help`;
|
|
2949
|
-
this.on(helpEvent, (context) => {
|
|
2950
|
-
let helpStr;
|
|
2951
|
-
if (typeof text === "function") {
|
|
2952
|
-
helpStr = text({ error: context.error, command: context.command });
|
|
2953
|
-
} else {
|
|
2954
|
-
helpStr = text;
|
|
2955
|
-
}
|
|
2956
|
-
if (helpStr) {
|
|
2957
|
-
context.write(`${helpStr}
|
|
2958
|
-
`);
|
|
2959
|
-
}
|
|
2960
|
-
});
|
|
2961
|
-
return this;
|
|
2962
|
-
}
|
|
2963
|
-
/**
|
|
2964
|
-
* Output help information if help flags specified
|
|
2965
|
-
*
|
|
2966
|
-
* @param {Array} args - array of options to search for help flags
|
|
2967
|
-
* @private
|
|
2968
|
-
*/
|
|
2969
|
-
_outputHelpIfRequested(args) {
|
|
2970
|
-
const helpOption = this._getHelpOption();
|
|
2971
|
-
const helpRequested = helpOption && args.find((arg) => helpOption.is(arg));
|
|
2972
|
-
if (helpRequested) {
|
|
2973
|
-
this.outputHelp();
|
|
2974
|
-
this._exit(0, "commander.helpDisplayed", "(outputHelp)");
|
|
2975
|
-
}
|
|
2976
|
-
}
|
|
2977
|
-
};
|
|
2978
|
-
function incrementNodeInspectorPort(args) {
|
|
2979
|
-
return args.map((arg) => {
|
|
2980
|
-
if (!arg.startsWith("--inspect")) {
|
|
2981
|
-
return arg;
|
|
2982
|
-
}
|
|
2983
|
-
let debugOption;
|
|
2984
|
-
let debugHost = "127.0.0.1";
|
|
2985
|
-
let debugPort = "9229";
|
|
2986
|
-
let match;
|
|
2987
|
-
if ((match = arg.match(/^(--inspect(-brk)?)$/)) !== null) {
|
|
2988
|
-
debugOption = match[1];
|
|
2989
|
-
} else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+)$/)) !== null) {
|
|
2990
|
-
debugOption = match[1];
|
|
2991
|
-
if (/^\d+$/.test(match[3])) {
|
|
2992
|
-
debugPort = match[3];
|
|
2993
|
-
} else {
|
|
2994
|
-
debugHost = match[3];
|
|
2995
|
-
}
|
|
2996
|
-
} else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+):(\d+)$/)) !== null) {
|
|
2997
|
-
debugOption = match[1];
|
|
2998
|
-
debugHost = match[3];
|
|
2999
|
-
debugPort = match[4];
|
|
3000
|
-
}
|
|
3001
|
-
if (debugOption && debugPort !== "0") {
|
|
3002
|
-
return `${debugOption}=${debugHost}:${parseInt(debugPort) + 1}`;
|
|
3003
|
-
}
|
|
3004
|
-
return arg;
|
|
3005
|
-
});
|
|
3006
|
-
}
|
|
3007
|
-
exports.Command = Command2;
|
|
3008
|
-
}
|
|
3009
|
-
});
|
|
3010
|
-
|
|
3011
|
-
// node_modules/commander/index.js
|
|
3012
|
-
var require_commander = __commonJS({
|
|
3013
|
-
"node_modules/commander/index.js"(exports) {
|
|
3014
|
-
"use strict";
|
|
3015
|
-
var { Argument: Argument2 } = require_argument();
|
|
3016
|
-
var { Command: Command2 } = require_command();
|
|
3017
|
-
var { CommanderError: CommanderError2, InvalidArgumentError: InvalidArgumentError2 } = require_error();
|
|
3018
|
-
var { Help: Help2 } = require_help();
|
|
3019
|
-
var { Option: Option2 } = require_option();
|
|
3020
|
-
exports.program = new Command2();
|
|
3021
|
-
exports.createCommand = (name) => new Command2(name);
|
|
3022
|
-
exports.createOption = (flags, description) => new Option2(flags, description);
|
|
3023
|
-
exports.createArgument = (name, description) => new Argument2(name, description);
|
|
3024
|
-
exports.Command = Command2;
|
|
3025
|
-
exports.Option = Option2;
|
|
3026
|
-
exports.Argument = Argument2;
|
|
3027
|
-
exports.Help = Help2;
|
|
3028
|
-
exports.CommanderError = CommanderError2;
|
|
3029
|
-
exports.InvalidArgumentError = InvalidArgumentError2;
|
|
3030
|
-
exports.InvalidOptionArgumentError = InvalidArgumentError2;
|
|
3031
|
-
}
|
|
3032
|
-
});
|
|
3033
8
|
|
|
3034
9
|
// src/adapters/cli/cli.ts
|
|
3035
10
|
import fs9 from "fs";
|
|
3036
|
-
import
|
|
11
|
+
import path13 from "path";
|
|
3037
12
|
import { createRequire as createRequire3 } from "module";
|
|
3038
|
-
|
|
3039
|
-
// node_modules/commander/esm.mjs
|
|
3040
|
-
var import_index = __toESM(require_commander(), 1);
|
|
3041
|
-
var {
|
|
3042
|
-
program,
|
|
3043
|
-
createCommand,
|
|
3044
|
-
createArgument,
|
|
3045
|
-
createOption,
|
|
3046
|
-
CommanderError,
|
|
3047
|
-
InvalidArgumentError,
|
|
3048
|
-
InvalidOptionArgumentError,
|
|
3049
|
-
// deprecated old name
|
|
3050
|
-
Command,
|
|
3051
|
-
Argument,
|
|
3052
|
-
Option,
|
|
3053
|
-
Help
|
|
3054
|
-
} = import_index.default;
|
|
13
|
+
import { Command } from "commander";
|
|
3055
14
|
|
|
3056
15
|
// src/infrastructure/SqliteGraphRepository.ts
|
|
3057
16
|
import fs from "fs";
|
|
@@ -3515,8 +474,14 @@ import crypto from "crypto";
|
|
|
3515
474
|
import fs2 from "fs";
|
|
3516
475
|
import { createRequire } from "module";
|
|
3517
476
|
import path2 from "path";
|
|
3518
|
-
import Parser from "tree-sitter";
|
|
3519
477
|
var _require = createRequire(import.meta.url);
|
|
478
|
+
var _ParserClass;
|
|
479
|
+
function getParserClass() {
|
|
480
|
+
if (!_ParserClass) {
|
|
481
|
+
_ParserClass = _require("tree-sitter");
|
|
482
|
+
}
|
|
483
|
+
return _ParserClass;
|
|
484
|
+
}
|
|
3520
485
|
function loadLanguage(pkg) {
|
|
3521
486
|
try {
|
|
3522
487
|
const mod = _require(pkg);
|
|
@@ -3699,8 +664,10 @@ function isTestFunction(name, filePath) {
|
|
|
3699
664
|
var MAX_AST_DEPTH = 180;
|
|
3700
665
|
var MODULE_CACHE_MAX = 15e3;
|
|
3701
666
|
var CodeParser = class {
|
|
667
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
3702
668
|
parsers = /* @__PURE__ */ new Map();
|
|
3703
669
|
moduleFileCache = /* @__PURE__ */ new Map();
|
|
670
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
3704
671
|
getParser(language) {
|
|
3705
672
|
if (this.parsers.has(language)) {
|
|
3706
673
|
return this.parsers.get(language) ?? null;
|
|
@@ -3710,7 +677,7 @@ var CodeParser = class {
|
|
|
3710
677
|
if (!lang) {
|
|
3711
678
|
return null;
|
|
3712
679
|
}
|
|
3713
|
-
const p = new
|
|
680
|
+
const p = new (getParserClass())();
|
|
3714
681
|
p.setLanguage(lang);
|
|
3715
682
|
this.parsers.set(language, p);
|
|
3716
683
|
return p;
|
|
@@ -4892,13 +1859,13 @@ function collectAllFiles(repoRoot) {
|
|
|
4892
1859
|
import crypto2 from "crypto";
|
|
4893
1860
|
import fs4 from "fs";
|
|
4894
1861
|
import path4 from "path";
|
|
4895
|
-
function fullBuild(repoRoot,
|
|
1862
|
+
function fullBuild(repoRoot, repo2, parser) {
|
|
4896
1863
|
const files = collectAllFiles(repoRoot);
|
|
4897
|
-
const existingFiles = new Set(
|
|
1864
|
+
const existingFiles = new Set(repo2.getAllFiles());
|
|
4898
1865
|
const currentAbs = new Set(files.map((f) => path4.join(repoRoot, f)));
|
|
4899
1866
|
for (const stale of existingFiles) {
|
|
4900
1867
|
if (!currentAbs.has(stale)) {
|
|
4901
|
-
|
|
1868
|
+
repo2.removeFileData(stale);
|
|
4902
1869
|
}
|
|
4903
1870
|
}
|
|
4904
1871
|
let totalNodes = 0;
|
|
@@ -4911,7 +1878,7 @@ function fullBuild(repoRoot, repo, parser) {
|
|
|
4911
1878
|
const source = fs4.readFileSync(fullPath);
|
|
4912
1879
|
const fhash = crypto2.createHash("sha256").update(source).digest("hex");
|
|
4913
1880
|
const [nodes, edges] = parser.parseBytes(fullPath, source);
|
|
4914
|
-
|
|
1881
|
+
repo2.storeFileNodesEdges(fullPath, nodes, edges, fhash);
|
|
4915
1882
|
totalNodes += nodes.length;
|
|
4916
1883
|
totalEdges += edges.length;
|
|
4917
1884
|
} catch (err) {
|
|
@@ -4922,8 +1889,8 @@ function fullBuild(repoRoot, repo, parser) {
|
|
|
4922
1889
|
`);
|
|
4923
1890
|
}
|
|
4924
1891
|
}
|
|
4925
|
-
|
|
4926
|
-
|
|
1892
|
+
repo2.setMetadata("last_updated", (/* @__PURE__ */ new Date()).toISOString().replace(/\..+$/, ""));
|
|
1893
|
+
repo2.setMetadata("last_build_type", "full");
|
|
4927
1894
|
return {
|
|
4928
1895
|
buildType: "full",
|
|
4929
1896
|
filesUpdated: files.length,
|
|
@@ -4934,7 +1901,7 @@ function fullBuild(repoRoot, repo, parser) {
|
|
|
4934
1901
|
errors
|
|
4935
1902
|
};
|
|
4936
1903
|
}
|
|
4937
|
-
function incrementalUpdate(repoRoot,
|
|
1904
|
+
function incrementalUpdate(repoRoot, repo2, parser, base = "HEAD~1", changedFiles) {
|
|
4938
1905
|
const ignorePatterns = loadIgnorePatterns(repoRoot);
|
|
4939
1906
|
const changed = changedFiles ?? getChangedFiles(repoRoot, base);
|
|
4940
1907
|
if (changed.length === 0) {
|
|
@@ -4951,7 +1918,7 @@ function incrementalUpdate(repoRoot, repo, parser, base = "HEAD~1", changedFiles
|
|
|
4951
1918
|
const dependentFiles = /* @__PURE__ */ new Set();
|
|
4952
1919
|
for (const relPath of changed) {
|
|
4953
1920
|
const fullPath = path4.join(repoRoot, relPath);
|
|
4954
|
-
const deps = findDependents(
|
|
1921
|
+
const deps = findDependents(repo2, fullPath);
|
|
4955
1922
|
for (const d of deps) {
|
|
4956
1923
|
try {
|
|
4957
1924
|
dependentFiles.add(path4.relative(repoRoot, d));
|
|
@@ -4970,7 +1937,7 @@ function incrementalUpdate(repoRoot, repo, parser, base = "HEAD~1", changedFiles
|
|
|
4970
1937
|
}
|
|
4971
1938
|
const absPath = path4.join(repoRoot, relPath);
|
|
4972
1939
|
if (!fs4.existsSync(absPath)) {
|
|
4973
|
-
|
|
1940
|
+
repo2.removeFileData(absPath);
|
|
4974
1941
|
continue;
|
|
4975
1942
|
}
|
|
4976
1943
|
if (!parser.detectLanguage(absPath)) {
|
|
@@ -4979,20 +1946,20 @@ function incrementalUpdate(repoRoot, repo, parser, base = "HEAD~1", changedFiles
|
|
|
4979
1946
|
try {
|
|
4980
1947
|
const source = fs4.readFileSync(absPath);
|
|
4981
1948
|
const fhash = crypto2.createHash("sha256").update(source).digest("hex");
|
|
4982
|
-
const existingNodes =
|
|
1949
|
+
const existingNodes = repo2.getNodesByFile(absPath);
|
|
4983
1950
|
if (existingNodes.length > 0 && existingNodes[0].fileHash === fhash) {
|
|
4984
1951
|
continue;
|
|
4985
1952
|
}
|
|
4986
1953
|
const [nodes, edges] = parser.parseBytes(absPath, source);
|
|
4987
|
-
|
|
1954
|
+
repo2.storeFileNodesEdges(absPath, nodes, edges, fhash);
|
|
4988
1955
|
totalNodes += nodes.length;
|
|
4989
1956
|
totalEdges += edges.length;
|
|
4990
1957
|
} catch (err) {
|
|
4991
1958
|
errors.push(`${relPath}: ${String(err)}`);
|
|
4992
1959
|
}
|
|
4993
1960
|
}
|
|
4994
|
-
|
|
4995
|
-
|
|
1961
|
+
repo2.setMetadata("last_updated", (/* @__PURE__ */ new Date()).toISOString().replace(/\..+$/, ""));
|
|
1962
|
+
repo2.setMetadata("last_build_type", "incremental");
|
|
4996
1963
|
return {
|
|
4997
1964
|
buildType: "incremental",
|
|
4998
1965
|
filesUpdated: allFiles.size,
|
|
@@ -5003,15 +1970,15 @@ function incrementalUpdate(repoRoot, repo, parser, base = "HEAD~1", changedFiles
|
|
|
5003
1970
|
errors
|
|
5004
1971
|
};
|
|
5005
1972
|
}
|
|
5006
|
-
function findDependents(
|
|
1973
|
+
function findDependents(repo2, filePath) {
|
|
5007
1974
|
const dependents = /* @__PURE__ */ new Set();
|
|
5008
|
-
for (const e of
|
|
1975
|
+
for (const e of repo2.getEdgesByTarget(filePath)) {
|
|
5009
1976
|
if (e.kind === "IMPORTS_FROM") {
|
|
5010
1977
|
dependents.add(e.filePath);
|
|
5011
1978
|
}
|
|
5012
1979
|
}
|
|
5013
|
-
for (const node of
|
|
5014
|
-
for (const e of
|
|
1980
|
+
for (const node of repo2.getNodesByFile(filePath)) {
|
|
1981
|
+
for (const e of repo2.getEdgesByTarget(node.qualifiedName)) {
|
|
5015
1982
|
if (["CALLS", "IMPORTS_FROM", "INHERITS", "IMPLEMENTS"].includes(e.kind)) {
|
|
5016
1983
|
dependents.add(e.filePath);
|
|
5017
1984
|
}
|
|
@@ -5025,7 +1992,7 @@ function findDependents(repo, filePath) {
|
|
|
5025
1992
|
import crypto3 from "crypto";
|
|
5026
1993
|
import fs5 from "fs";
|
|
5027
1994
|
import path5 from "path";
|
|
5028
|
-
async function watch(repoRoot,
|
|
1995
|
+
async function watch(repoRoot, repo2, parser) {
|
|
5029
1996
|
const chokidar = __require("chokidar");
|
|
5030
1997
|
const ignorePatterns = loadIgnorePatterns(repoRoot);
|
|
5031
1998
|
const pending = /* @__PURE__ */ new Set();
|
|
@@ -5082,8 +2049,8 @@ async function watch(repoRoot, repo, parser) {
|
|
|
5082
2049
|
const source = fs5.readFileSync(absPath);
|
|
5083
2050
|
const fhash = crypto3.createHash("sha256").update(source).digest("hex");
|
|
5084
2051
|
const [nodes, edges] = parser.parseBytes(absPath, source);
|
|
5085
|
-
|
|
5086
|
-
|
|
2052
|
+
repo2.storeFileNodesEdges(absPath, nodes, edges, fhash);
|
|
2053
|
+
repo2.setMetadata("last_updated", (/* @__PURE__ */ new Date()).toISOString().replace(/\..+$/, ""));
|
|
5087
2054
|
const rel = path5.relative(repoRoot, absPath);
|
|
5088
2055
|
process.stderr.write(`Updated: ${rel} (${nodes.length} nodes, ${edges.length} edges)
|
|
5089
2056
|
`);
|
|
@@ -5108,7 +2075,7 @@ async function watch(repoRoot, repo, parser) {
|
|
|
5108
2075
|
}).on("unlink", (p) => {
|
|
5109
2076
|
const rel = path5.relative(repoRoot, p);
|
|
5110
2077
|
if (!shouldIgnore(rel, ignorePatterns)) {
|
|
5111
|
-
|
|
2078
|
+
repo2.removeFileData(p);
|
|
5112
2079
|
}
|
|
5113
2080
|
});
|
|
5114
2081
|
process.stderr.write(`Watching ${repoRoot} for changes... (Ctrl+C to stop)
|
|
@@ -5373,11 +2340,11 @@ var SqliteEmbeddingStore = class {
|
|
|
5373
2340
|
};
|
|
5374
2341
|
|
|
5375
2342
|
// src/usecases/getImpactRadius.ts
|
|
5376
|
-
function computeImpactRadius(changedFiles,
|
|
5377
|
-
const adj =
|
|
2343
|
+
function computeImpactRadius(changedFiles, repo2, maxDepth = 2, maxNodes = 500) {
|
|
2344
|
+
const adj = repo2.getAdjacency();
|
|
5378
2345
|
const seeds = /* @__PURE__ */ new Set();
|
|
5379
2346
|
for (const f of changedFiles) {
|
|
5380
|
-
for (const node of
|
|
2347
|
+
for (const node of repo2.getNodesByFile(f)) {
|
|
5381
2348
|
seeds.add(node.qualifiedName);
|
|
5382
2349
|
}
|
|
5383
2350
|
}
|
|
@@ -5416,7 +2383,7 @@ function computeImpactRadius(changedFiles, repo, maxDepth = 2, maxNodes = 500) {
|
|
|
5416
2383
|
}
|
|
5417
2384
|
const changedNodes = [];
|
|
5418
2385
|
for (const qn of seeds) {
|
|
5419
|
-
const n =
|
|
2386
|
+
const n = repo2.getNode(qn);
|
|
5420
2387
|
if (n) {
|
|
5421
2388
|
changedNodes.push(n);
|
|
5422
2389
|
}
|
|
@@ -5426,7 +2393,7 @@ function computeImpactRadius(changedFiles, repo, maxDepth = 2, maxNodes = 500) {
|
|
|
5426
2393
|
if (seeds.has(qn)) {
|
|
5427
2394
|
continue;
|
|
5428
2395
|
}
|
|
5429
|
-
const n =
|
|
2396
|
+
const n = repo2.getNode(qn);
|
|
5430
2397
|
if (n) {
|
|
5431
2398
|
impactedNodes.push(n);
|
|
5432
2399
|
}
|
|
@@ -5438,7 +2405,7 @@ function computeImpactRadius(changedFiles, repo, maxDepth = 2, maxNodes = 500) {
|
|
|
5438
2405
|
}
|
|
5439
2406
|
const impactedFiles = [...new Set(impactedNodes.map((n) => n.filePath))];
|
|
5440
2407
|
const allQns = /* @__PURE__ */ new Set([...seeds, ...impactedNodes.map((n) => n.qualifiedName)]);
|
|
5441
|
-
const edges = allQns.size > 0 ?
|
|
2408
|
+
const edges = allQns.size > 0 ? repo2.getEdgesAmong(allQns) : [];
|
|
5442
2409
|
return { changedNodes, impactedNodes, impactedFiles, edges, truncated, totalImpacted };
|
|
5443
2410
|
}
|
|
5444
2411
|
|
|
@@ -5624,7 +2591,7 @@ var QUERY_PATTERNS = {
|
|
|
5624
2591
|
file_summary: "Get a summary of all nodes in a file"
|
|
5625
2592
|
};
|
|
5626
2593
|
function queryGraph(args) {
|
|
5627
|
-
const { pattern, target, repo, repoRoot } = args;
|
|
2594
|
+
const { pattern, target, repo: repo2, repoRoot } = args;
|
|
5628
2595
|
if (!QUERY_PATTERNS[pattern]) {
|
|
5629
2596
|
return {
|
|
5630
2597
|
status: "error",
|
|
@@ -5644,17 +2611,17 @@ function queryGraph(args) {
|
|
|
5644
2611
|
edges: []
|
|
5645
2612
|
};
|
|
5646
2613
|
}
|
|
5647
|
-
let node =
|
|
2614
|
+
let node = repo2.getNode(target);
|
|
5648
2615
|
let resolvedTarget = target;
|
|
5649
2616
|
if (!node) {
|
|
5650
2617
|
const absTarget = path6.join(repoRoot, target);
|
|
5651
|
-
node =
|
|
2618
|
+
node = repo2.getNode(absTarget);
|
|
5652
2619
|
if (node) {
|
|
5653
2620
|
resolvedTarget = absTarget;
|
|
5654
2621
|
}
|
|
5655
2622
|
}
|
|
5656
2623
|
if (!node) {
|
|
5657
|
-
const candidates =
|
|
2624
|
+
const candidates = repo2.searchNodes(target, 5);
|
|
5658
2625
|
if (candidates.length === 1) {
|
|
5659
2626
|
node = candidates[0];
|
|
5660
2627
|
resolvedTarget = node.qualifiedName;
|
|
@@ -5671,9 +2638,9 @@ function queryGraph(args) {
|
|
|
5671
2638
|
}
|
|
5672
2639
|
const qn = node?.qualifiedName ?? resolvedTarget;
|
|
5673
2640
|
if (pattern === "callers_of") {
|
|
5674
|
-
for (const e of
|
|
2641
|
+
for (const e of repo2.getEdgesByTarget(qn)) {
|
|
5675
2642
|
if (e.kind === "CALLS") {
|
|
5676
|
-
const caller =
|
|
2643
|
+
const caller = repo2.getNode(e.sourceQualified);
|
|
5677
2644
|
if (caller) {
|
|
5678
2645
|
results.push(nodeToDict(caller));
|
|
5679
2646
|
}
|
|
@@ -5681,8 +2648,8 @@ function queryGraph(args) {
|
|
|
5681
2648
|
}
|
|
5682
2649
|
}
|
|
5683
2650
|
if (results.length === 0 && node) {
|
|
5684
|
-
for (const e of
|
|
5685
|
-
const caller =
|
|
2651
|
+
for (const e of repo2.searchEdgesByTargetName(node.name)) {
|
|
2652
|
+
const caller = repo2.getNode(e.sourceQualified);
|
|
5686
2653
|
if (caller) {
|
|
5687
2654
|
results.push(nodeToDict(caller));
|
|
5688
2655
|
}
|
|
@@ -5690,9 +2657,9 @@ function queryGraph(args) {
|
|
|
5690
2657
|
}
|
|
5691
2658
|
}
|
|
5692
2659
|
} else if (pattern === "callees_of") {
|
|
5693
|
-
for (const e of
|
|
2660
|
+
for (const e of repo2.getEdgesBySource(qn)) {
|
|
5694
2661
|
if (e.kind === "CALLS") {
|
|
5695
|
-
const callee =
|
|
2662
|
+
const callee = repo2.getNode(e.targetQualified);
|
|
5696
2663
|
if (callee) {
|
|
5697
2664
|
results.push(nodeToDict(callee));
|
|
5698
2665
|
}
|
|
@@ -5700,7 +2667,7 @@ function queryGraph(args) {
|
|
|
5700
2667
|
}
|
|
5701
2668
|
}
|
|
5702
2669
|
} else if (pattern === "imports_of") {
|
|
5703
|
-
for (const e of
|
|
2670
|
+
for (const e of repo2.getEdgesBySource(qn)) {
|
|
5704
2671
|
if (e.kind === "IMPORTS_FROM") {
|
|
5705
2672
|
results.push({ import_target: e.targetQualified });
|
|
5706
2673
|
edgesOut.push(edgeToDict(e));
|
|
@@ -5708,25 +2675,25 @@ function queryGraph(args) {
|
|
|
5708
2675
|
}
|
|
5709
2676
|
} else if (pattern === "importers_of") {
|
|
5710
2677
|
const absTarget = node ? node.filePath : path6.join(repoRoot, target);
|
|
5711
|
-
for (const e of
|
|
2678
|
+
for (const e of repo2.getEdgesByTarget(absTarget)) {
|
|
5712
2679
|
if (e.kind === "IMPORTS_FROM") {
|
|
5713
2680
|
results.push({ importer: e.sourceQualified, file: e.filePath });
|
|
5714
2681
|
edgesOut.push(edgeToDict(e));
|
|
5715
2682
|
}
|
|
5716
2683
|
}
|
|
5717
2684
|
} else if (pattern === "children_of") {
|
|
5718
|
-
for (const e of
|
|
2685
|
+
for (const e of repo2.getEdgesBySource(qn)) {
|
|
5719
2686
|
if (e.kind === "CONTAINS") {
|
|
5720
|
-
const child =
|
|
2687
|
+
const child = repo2.getNode(e.targetQualified);
|
|
5721
2688
|
if (child) {
|
|
5722
2689
|
results.push(nodeToDict(child));
|
|
5723
2690
|
}
|
|
5724
2691
|
}
|
|
5725
2692
|
}
|
|
5726
2693
|
} else if (pattern === "tests_for") {
|
|
5727
|
-
for (const e of
|
|
2694
|
+
for (const e of repo2.getEdgesByTarget(qn)) {
|
|
5728
2695
|
if (e.kind === "TESTED_BY") {
|
|
5729
|
-
const testNode =
|
|
2696
|
+
const testNode = repo2.getNode(e.sourceQualified);
|
|
5730
2697
|
if (testNode) {
|
|
5731
2698
|
results.push(nodeToDict(testNode));
|
|
5732
2699
|
}
|
|
@@ -5735,17 +2702,17 @@ function queryGraph(args) {
|
|
|
5735
2702
|
const name = node?.name ?? target;
|
|
5736
2703
|
const seenQns = new Set(results.map((r) => r["qualified_name"]));
|
|
5737
2704
|
for (const t of [
|
|
5738
|
-
...
|
|
5739
|
-
...
|
|
2705
|
+
...repo2.searchNodes(`test_${name}`, 10),
|
|
2706
|
+
...repo2.searchNodes(`Test${name}`, 10)
|
|
5740
2707
|
]) {
|
|
5741
2708
|
if (!seenQns.has(t.qualifiedName) && t.isTest) {
|
|
5742
2709
|
results.push(nodeToDict(t));
|
|
5743
2710
|
}
|
|
5744
2711
|
}
|
|
5745
2712
|
} else if (pattern === "inheritors_of") {
|
|
5746
|
-
for (const e of
|
|
2713
|
+
for (const e of repo2.getEdgesByTarget(qn)) {
|
|
5747
2714
|
if (e.kind === "INHERITS" || e.kind === "IMPLEMENTS") {
|
|
5748
|
-
const child =
|
|
2715
|
+
const child = repo2.getNode(e.sourceQualified);
|
|
5749
2716
|
if (child) {
|
|
5750
2717
|
results.push(nodeToDict(child));
|
|
5751
2718
|
}
|
|
@@ -5754,7 +2721,7 @@ function queryGraph(args) {
|
|
|
5754
2721
|
}
|
|
5755
2722
|
} else if (pattern === "file_summary") {
|
|
5756
2723
|
const absPath = path6.join(repoRoot, target);
|
|
5757
|
-
for (const n of
|
|
2724
|
+
for (const n of repo2.getNodesByFile(absPath)) {
|
|
5758
2725
|
results.push(nodeToDict(n));
|
|
5759
2726
|
}
|
|
5760
2727
|
}
|
|
@@ -5775,7 +2742,7 @@ import path7 from "path";
|
|
|
5775
2742
|
function getReviewContext(args) {
|
|
5776
2743
|
const {
|
|
5777
2744
|
changedFiles,
|
|
5778
|
-
repo,
|
|
2745
|
+
repo: repo2,
|
|
5779
2746
|
repoRoot,
|
|
5780
2747
|
maxDepth = 2,
|
|
5781
2748
|
includeSource = true,
|
|
@@ -5785,7 +2752,7 @@ function getReviewContext(args) {
|
|
|
5785
2752
|
return { status: "ok", summary: "No changes detected. Nothing to review.", context: {} };
|
|
5786
2753
|
}
|
|
5787
2754
|
const absFiles = changedFiles.map((f) => path7.join(repoRoot, f));
|
|
5788
|
-
const impact = computeImpactRadius(absFiles,
|
|
2755
|
+
const impact = computeImpactRadius(absFiles, repo2, maxDepth);
|
|
5789
2756
|
const context = {
|
|
5790
2757
|
changed_files: changedFiles,
|
|
5791
2758
|
impacted_files: impact.impactedFiles,
|
|
@@ -5900,12 +2867,12 @@ function generateReviewGuidance(impact, _changedFiles) {
|
|
|
5900
2867
|
|
|
5901
2868
|
// src/usecases/semanticSearch.ts
|
|
5902
2869
|
async function semanticSearchNodes(args) {
|
|
5903
|
-
const { query, kind = null, limit = 20, repo, embStore } = args;
|
|
2870
|
+
const { query, kind = null, limit = 20, repo: repo2, embStore } = args;
|
|
5904
2871
|
let searchMode = "keyword";
|
|
5905
2872
|
try {
|
|
5906
2873
|
if (embStore.available && embStore.count() > 0) {
|
|
5907
2874
|
searchMode = "semantic";
|
|
5908
|
-
let raw = await semanticSearch(query,
|
|
2875
|
+
let raw = await semanticSearch(query, repo2, embStore, limit * 2);
|
|
5909
2876
|
if (kind) {
|
|
5910
2877
|
raw = raw.filter((r) => r["kind"] === kind);
|
|
5911
2878
|
}
|
|
@@ -5921,7 +2888,7 @@ async function semanticSearchNodes(args) {
|
|
|
5921
2888
|
} catch {
|
|
5922
2889
|
searchMode = "keyword";
|
|
5923
2890
|
}
|
|
5924
|
-
let results =
|
|
2891
|
+
let results = repo2.searchNodes(query, limit * 2);
|
|
5925
2892
|
if (kind) {
|
|
5926
2893
|
results = results.filter((r) => r.kind === kind);
|
|
5927
2894
|
}
|
|
@@ -5942,12 +2909,12 @@ async function semanticSearchNodes(args) {
|
|
|
5942
2909
|
results: results.map(nodeToDict)
|
|
5943
2910
|
};
|
|
5944
2911
|
}
|
|
5945
|
-
async function semanticSearch(query,
|
|
2912
|
+
async function semanticSearch(query, repo2, embStore, limit = 20) {
|
|
5946
2913
|
if (embStore.available && embStore.count() > 0) {
|
|
5947
2914
|
const results = await embStore.search(query, limit);
|
|
5948
2915
|
const output = [];
|
|
5949
2916
|
for (const { qualifiedName, score } of results) {
|
|
5950
|
-
const node =
|
|
2917
|
+
const node = repo2.getNode(qualifiedName);
|
|
5951
2918
|
if (node) {
|
|
5952
2919
|
const d = nodeToDict(node);
|
|
5953
2920
|
d["similarity_score"] = Math.round(score * 1e4) / 1e4;
|
|
@@ -5956,14 +2923,14 @@ async function semanticSearch(query, repo, embStore, limit = 20) {
|
|
|
5956
2923
|
}
|
|
5957
2924
|
return output;
|
|
5958
2925
|
}
|
|
5959
|
-
return
|
|
2926
|
+
return repo2.searchNodes(query, limit).map((n) => nodeToDict(n));
|
|
5960
2927
|
}
|
|
5961
2928
|
|
|
5962
2929
|
// src/usecases/listStats.ts
|
|
5963
2930
|
import path8 from "path";
|
|
5964
2931
|
function listStats(args) {
|
|
5965
|
-
const { repo, embStore, repoRoot } = args;
|
|
5966
|
-
const stats =
|
|
2932
|
+
const { repo: repo2, embStore, repoRoot } = args;
|
|
2933
|
+
const stats = repo2.getStats();
|
|
5967
2934
|
const rootName = path8.basename(repoRoot);
|
|
5968
2935
|
const summaryParts = [
|
|
5969
2936
|
`Graph statistics for ${rootName}:`,
|
|
@@ -6003,23 +2970,23 @@ function listStats(args) {
|
|
|
6003
2970
|
}
|
|
6004
2971
|
|
|
6005
2972
|
// src/usecases/embedGraph.ts
|
|
6006
|
-
async function embedAllNodes(
|
|
2973
|
+
async function embedAllNodes(repo2, embStore) {
|
|
6007
2974
|
if (!embStore.available) return 0;
|
|
6008
2975
|
const allNodes = [];
|
|
6009
|
-
for (const f of
|
|
6010
|
-
allNodes.push(...
|
|
2976
|
+
for (const f of repo2.getAllFiles()) {
|
|
2977
|
+
allNodes.push(...repo2.getNodesByFile(f));
|
|
6011
2978
|
}
|
|
6012
2979
|
return embStore.embedNodes(allNodes);
|
|
6013
2980
|
}
|
|
6014
2981
|
async function embedGraph(args) {
|
|
6015
|
-
const { repo, embStore } = args;
|
|
2982
|
+
const { repo: repo2, embStore } = args;
|
|
6016
2983
|
if (!embStore.available) {
|
|
6017
2984
|
return {
|
|
6018
2985
|
status: "error",
|
|
6019
2986
|
error: "@huggingface/transformers is not installed. Install with: npm install codeorbit (with optional deps)"
|
|
6020
2987
|
};
|
|
6021
2988
|
}
|
|
6022
|
-
const newlyEmbedded = await embedAllNodes(
|
|
2989
|
+
const newlyEmbedded = await embedAllNodes(repo2, embStore);
|
|
6023
2990
|
const total = embStore.count();
|
|
6024
2991
|
return {
|
|
6025
2992
|
status: "ok",
|
|
@@ -6068,8 +3035,8 @@ function getDocsSection(args) {
|
|
|
6068
3035
|
// src/usecases/findLargeFunctions.ts
|
|
6069
3036
|
import path10 from "path";
|
|
6070
3037
|
function findLargeFunctions(args) {
|
|
6071
|
-
const { minLines = 50, kind = null, filePathPattern = null, limit = 50, repo, repoRoot } = args;
|
|
6072
|
-
const nodes =
|
|
3038
|
+
const { minLines = 50, kind = null, filePathPattern = null, limit = 50, repo: repo2, repoRoot } = args;
|
|
3039
|
+
const nodes = repo2.getNodesBySize(
|
|
6073
3040
|
minLines,
|
|
6074
3041
|
void 0,
|
|
6075
3042
|
kind ?? void 0,
|
|
@@ -6125,11 +3092,11 @@ function resolveRoot(repoRoot) {
|
|
|
6125
3092
|
function buildOrUpdateGraph(args) {
|
|
6126
3093
|
const { fullRebuild = false, repoRoot = null, base = "HEAD~1" } = args;
|
|
6127
3094
|
const root = resolveRoot(repoRoot);
|
|
6128
|
-
const
|
|
3095
|
+
const repo2 = new SqliteGraphRepository(getDbPath(root));
|
|
6129
3096
|
const parser = new CodeParser();
|
|
6130
3097
|
try {
|
|
6131
3098
|
if (fullRebuild) {
|
|
6132
|
-
const result = fullBuild(root,
|
|
3099
|
+
const result = fullBuild(root, repo2, parser);
|
|
6133
3100
|
return {
|
|
6134
3101
|
status: "ok",
|
|
6135
3102
|
build_type: "full",
|
|
@@ -6140,7 +3107,7 @@ function buildOrUpdateGraph(args) {
|
|
|
6140
3107
|
errors: result.errors
|
|
6141
3108
|
};
|
|
6142
3109
|
} else {
|
|
6143
|
-
const result = incrementalUpdate(root,
|
|
3110
|
+
const result = incrementalUpdate(root, repo2, parser, base);
|
|
6144
3111
|
if (result.filesUpdated === 0) {
|
|
6145
3112
|
return {
|
|
6146
3113
|
status: "ok",
|
|
@@ -6167,7 +3134,7 @@ function buildOrUpdateGraph(args) {
|
|
|
6167
3134
|
};
|
|
6168
3135
|
}
|
|
6169
3136
|
} finally {
|
|
6170
|
-
|
|
3137
|
+
repo2.close();
|
|
6171
3138
|
}
|
|
6172
3139
|
}
|
|
6173
3140
|
function getImpactRadius(args) {
|
|
@@ -6179,7 +3146,7 @@ function getImpactRadius(args) {
|
|
|
6179
3146
|
base = "HEAD~1"
|
|
6180
3147
|
} = args;
|
|
6181
3148
|
const root = resolveRoot(repoRoot);
|
|
6182
|
-
const
|
|
3149
|
+
const repo2 = new SqliteGraphRepository(getDbPath(root));
|
|
6183
3150
|
try {
|
|
6184
3151
|
let changed = changedFiles;
|
|
6185
3152
|
if (!changed) {
|
|
@@ -6200,7 +3167,7 @@ function getImpactRadius(args) {
|
|
|
6200
3167
|
};
|
|
6201
3168
|
}
|
|
6202
3169
|
const absFiles = changed.map((f) => path11.join(root, f));
|
|
6203
|
-
const result = computeImpactRadius(absFiles,
|
|
3170
|
+
const result = computeImpactRadius(absFiles, repo2, maxDepth, maxResults);
|
|
6204
3171
|
const summaryParts = [
|
|
6205
3172
|
`Blast radius for ${changed.length} changed file(s):`,
|
|
6206
3173
|
` - ${result.changedNodes.length} nodes directly changed`,
|
|
@@ -6224,17 +3191,17 @@ function getImpactRadius(args) {
|
|
|
6224
3191
|
total_impacted: result.totalImpacted
|
|
6225
3192
|
};
|
|
6226
3193
|
} finally {
|
|
6227
|
-
|
|
3194
|
+
repo2.close();
|
|
6228
3195
|
}
|
|
6229
3196
|
}
|
|
6230
3197
|
function queryGraph2(args) {
|
|
6231
3198
|
const { pattern, target, repoRoot = null } = args;
|
|
6232
3199
|
const root = resolveRoot(repoRoot);
|
|
6233
|
-
const
|
|
3200
|
+
const repo2 = new SqliteGraphRepository(getDbPath(root));
|
|
6234
3201
|
try {
|
|
6235
|
-
return queryGraph({ pattern, target, repo, repoRoot: root });
|
|
3202
|
+
return queryGraph({ pattern, target, repo: repo2, repoRoot: root });
|
|
6236
3203
|
} finally {
|
|
6237
|
-
|
|
3204
|
+
repo2.close();
|
|
6238
3205
|
}
|
|
6239
3206
|
}
|
|
6240
3207
|
function getReviewContext2(args) {
|
|
@@ -6247,7 +3214,7 @@ function getReviewContext2(args) {
|
|
|
6247
3214
|
base = "HEAD~1"
|
|
6248
3215
|
} = args;
|
|
6249
3216
|
const root = resolveRoot(repoRoot);
|
|
6250
|
-
const
|
|
3217
|
+
const repo2 = new SqliteGraphRepository(getDbPath(root));
|
|
6251
3218
|
try {
|
|
6252
3219
|
let changed = changedFiles;
|
|
6253
3220
|
if (!changed) {
|
|
@@ -6265,53 +3232,53 @@ function getReviewContext2(args) {
|
|
|
6265
3232
|
}
|
|
6266
3233
|
return getReviewContext({
|
|
6267
3234
|
changedFiles: changed,
|
|
6268
|
-
repo,
|
|
3235
|
+
repo: repo2,
|
|
6269
3236
|
repoRoot: root,
|
|
6270
3237
|
maxDepth,
|
|
6271
3238
|
includeSource,
|
|
6272
3239
|
maxLinesPerFile
|
|
6273
3240
|
});
|
|
6274
3241
|
} finally {
|
|
6275
|
-
|
|
3242
|
+
repo2.close();
|
|
6276
3243
|
}
|
|
6277
3244
|
}
|
|
6278
3245
|
async function semanticSearchNodes2(args) {
|
|
6279
3246
|
const { query, kind = null, limit = 20, repoRoot = null } = args;
|
|
6280
3247
|
const root = resolveRoot(repoRoot);
|
|
6281
3248
|
const dbPath = getDbPath(root);
|
|
6282
|
-
const
|
|
3249
|
+
const repo2 = new SqliteGraphRepository(dbPath);
|
|
6283
3250
|
const embStore = new SqliteEmbeddingStore(dbPath);
|
|
6284
3251
|
try {
|
|
6285
|
-
return await semanticSearchNodes({ query, kind, limit, repo, embStore });
|
|
3252
|
+
return await semanticSearchNodes({ query, kind, limit, repo: repo2, embStore });
|
|
6286
3253
|
} finally {
|
|
6287
3254
|
embStore.close();
|
|
6288
|
-
|
|
3255
|
+
repo2.close();
|
|
6289
3256
|
}
|
|
6290
3257
|
}
|
|
6291
3258
|
function listGraphStats(args) {
|
|
6292
3259
|
const { repoRoot = null } = args;
|
|
6293
3260
|
const root = resolveRoot(repoRoot);
|
|
6294
3261
|
const dbPath = getDbPath(root);
|
|
6295
|
-
const
|
|
3262
|
+
const repo2 = new SqliteGraphRepository(dbPath);
|
|
6296
3263
|
const embStore = new SqliteEmbeddingStore(dbPath);
|
|
6297
3264
|
try {
|
|
6298
|
-
return listStats({ repo, embStore, repoRoot: root });
|
|
3265
|
+
return listStats({ repo: repo2, embStore, repoRoot: root });
|
|
6299
3266
|
} finally {
|
|
6300
3267
|
embStore.close();
|
|
6301
|
-
|
|
3268
|
+
repo2.close();
|
|
6302
3269
|
}
|
|
6303
3270
|
}
|
|
6304
3271
|
async function embedGraph2(args) {
|
|
6305
3272
|
const { repoRoot = null } = args;
|
|
6306
3273
|
const root = resolveRoot(repoRoot);
|
|
6307
3274
|
const dbPath = getDbPath(root);
|
|
6308
|
-
const
|
|
3275
|
+
const repo2 = new SqliteGraphRepository(dbPath);
|
|
6309
3276
|
const embStore = new SqliteEmbeddingStore(dbPath);
|
|
6310
3277
|
try {
|
|
6311
|
-
return await embedGraph({ repo, embStore });
|
|
3278
|
+
return await embedGraph({ repo: repo2, embStore });
|
|
6312
3279
|
} finally {
|
|
6313
3280
|
embStore.close();
|
|
6314
|
-
|
|
3281
|
+
repo2.close();
|
|
6315
3282
|
}
|
|
6316
3283
|
}
|
|
6317
3284
|
function getDocsSection2(args) {
|
|
@@ -6338,11 +3305,11 @@ function findLargeFunctions2(args) {
|
|
|
6338
3305
|
repoRoot = null
|
|
6339
3306
|
} = args;
|
|
6340
3307
|
const root = resolveRoot(repoRoot);
|
|
6341
|
-
const
|
|
3308
|
+
const repo2 = new SqliteGraphRepository(getDbPath(root));
|
|
6342
3309
|
try {
|
|
6343
|
-
return findLargeFunctions({ minLines, kind, filePathPattern, limit, repo, repoRoot: root });
|
|
3310
|
+
return findLargeFunctions({ minLines, kind, filePathPattern, limit, repo: repo2, repoRoot: root });
|
|
6344
3311
|
} finally {
|
|
6345
|
-
|
|
3312
|
+
repo2.close();
|
|
6346
3313
|
}
|
|
6347
3314
|
}
|
|
6348
3315
|
|
|
@@ -6561,6 +3528,262 @@ async function runMcpServer() {
|
|
|
6561
3528
|
await server.connect(transport);
|
|
6562
3529
|
}
|
|
6563
3530
|
|
|
3531
|
+
// src/adapters/cli/ipcWorker.ts
|
|
3532
|
+
import * as path12 from "path";
|
|
3533
|
+
import * as readline from "readline";
|
|
3534
|
+
var repo = null;
|
|
3535
|
+
var currentDbPath = null;
|
|
3536
|
+
function openRepo(dbPath) {
|
|
3537
|
+
if (currentDbPath === dbPath && repo !== null) return;
|
|
3538
|
+
try {
|
|
3539
|
+
repo?.close();
|
|
3540
|
+
} catch {
|
|
3541
|
+
}
|
|
3542
|
+
repo = new SqliteGraphRepository(dbPath);
|
|
3543
|
+
currentDbPath = dbPath;
|
|
3544
|
+
}
|
|
3545
|
+
function getRepo() {
|
|
3546
|
+
if (!repo) throw new Error("No database open. Call open first.");
|
|
3547
|
+
return repo;
|
|
3548
|
+
}
|
|
3549
|
+
function toGraphNode(n) {
|
|
3550
|
+
return {
|
|
3551
|
+
id: n.id,
|
|
3552
|
+
kind: n.kind,
|
|
3553
|
+
name: n.name,
|
|
3554
|
+
qualifiedName: n.qualifiedName,
|
|
3555
|
+
filePath: n.filePath,
|
|
3556
|
+
lineStart: n.lineStart,
|
|
3557
|
+
lineEnd: n.lineEnd,
|
|
3558
|
+
language: n.language,
|
|
3559
|
+
parentName: n.parentName,
|
|
3560
|
+
params: n.params,
|
|
3561
|
+
returnType: n.returnType,
|
|
3562
|
+
modifiers: n.modifiers,
|
|
3563
|
+
isTest: n.isTest,
|
|
3564
|
+
fileHash: n.fileHash
|
|
3565
|
+
};
|
|
3566
|
+
}
|
|
3567
|
+
function toGraphEdge(e) {
|
|
3568
|
+
return {
|
|
3569
|
+
id: e.id,
|
|
3570
|
+
kind: e.kind,
|
|
3571
|
+
sourceQualified: e.sourceQualified,
|
|
3572
|
+
targetQualified: e.targetQualified,
|
|
3573
|
+
filePath: e.filePath,
|
|
3574
|
+
line: e.line
|
|
3575
|
+
};
|
|
3576
|
+
}
|
|
3577
|
+
function computeImpactRadius2(changedFiles, maxDepth) {
|
|
3578
|
+
const r = getRepo();
|
|
3579
|
+
const seeds = /* @__PURE__ */ new Set();
|
|
3580
|
+
for (const f of changedFiles) {
|
|
3581
|
+
for (const node of r.getNodesByFile(f)) {
|
|
3582
|
+
seeds.add(node.qualifiedName);
|
|
3583
|
+
}
|
|
3584
|
+
}
|
|
3585
|
+
const visited = /* @__PURE__ */ new Set();
|
|
3586
|
+
let frontier = new Set(seeds);
|
|
3587
|
+
const impacted = /* @__PURE__ */ new Set();
|
|
3588
|
+
let depth = 0;
|
|
3589
|
+
while (frontier.size > 0 && depth < maxDepth) {
|
|
3590
|
+
const nextFrontier = /* @__PURE__ */ new Set();
|
|
3591
|
+
for (const qn of frontier) {
|
|
3592
|
+
visited.add(qn);
|
|
3593
|
+
for (const e of r.getEdgesBySource(qn)) {
|
|
3594
|
+
if (!visited.has(e.targetQualified)) {
|
|
3595
|
+
nextFrontier.add(e.targetQualified);
|
|
3596
|
+
impacted.add(e.targetQualified);
|
|
3597
|
+
}
|
|
3598
|
+
}
|
|
3599
|
+
for (const e of r.getEdgesByTarget(qn)) {
|
|
3600
|
+
if (!visited.has(e.sourceQualified)) {
|
|
3601
|
+
nextFrontier.add(e.sourceQualified);
|
|
3602
|
+
impacted.add(e.sourceQualified);
|
|
3603
|
+
}
|
|
3604
|
+
}
|
|
3605
|
+
}
|
|
3606
|
+
frontier = nextFrontier;
|
|
3607
|
+
depth++;
|
|
3608
|
+
}
|
|
3609
|
+
const changedNodes = [];
|
|
3610
|
+
for (const qn of seeds) {
|
|
3611
|
+
const node = r.getNode(qn);
|
|
3612
|
+
if (node) changedNodes.push(toGraphNode(node));
|
|
3613
|
+
}
|
|
3614
|
+
const impactedNodes = [];
|
|
3615
|
+
for (const qn of impacted) {
|
|
3616
|
+
if (seeds.has(qn)) continue;
|
|
3617
|
+
const node = r.getNode(qn);
|
|
3618
|
+
if (node) impactedNodes.push(toGraphNode(node));
|
|
3619
|
+
}
|
|
3620
|
+
const impactedFiles = [...new Set(impactedNodes.map((n) => n.filePath))];
|
|
3621
|
+
const allQns = /* @__PURE__ */ new Set([...seeds, ...impacted]);
|
|
3622
|
+
const edges = [];
|
|
3623
|
+
if (allQns.size > 0) {
|
|
3624
|
+
for (const e of r.getEdgesAmong(allQns)) {
|
|
3625
|
+
edges.push(toGraphEdge(e));
|
|
3626
|
+
}
|
|
3627
|
+
}
|
|
3628
|
+
return { changedNodes, impactedNodes, impactedFiles, edges };
|
|
3629
|
+
}
|
|
3630
|
+
async function dispatch(method, params) {
|
|
3631
|
+
if (method === "open") {
|
|
3632
|
+
const dbPath = params["dbPath"];
|
|
3633
|
+
openRepo(dbPath);
|
|
3634
|
+
const schemaErr = checkSchema();
|
|
3635
|
+
return { ok: true, schemaError: schemaErr ?? null };
|
|
3636
|
+
}
|
|
3637
|
+
if (method === "ping") return { ok: true };
|
|
3638
|
+
if (method === "getStats") {
|
|
3639
|
+
const r = getRepo();
|
|
3640
|
+
const s = r.getStats();
|
|
3641
|
+
return s;
|
|
3642
|
+
}
|
|
3643
|
+
if (method === "checkSchemaCompatibility") {
|
|
3644
|
+
return checkSchema() ?? null;
|
|
3645
|
+
}
|
|
3646
|
+
if (method === "getAllFiles") {
|
|
3647
|
+
return getRepo().getAllFiles();
|
|
3648
|
+
}
|
|
3649
|
+
if (method === "getNodesByFile") {
|
|
3650
|
+
const filePath = params["filePath"];
|
|
3651
|
+
return getRepo().getNodesByFile(filePath).map(toGraphNode);
|
|
3652
|
+
}
|
|
3653
|
+
if (method === "getNode") {
|
|
3654
|
+
const qn = params["qualifiedName"];
|
|
3655
|
+
const node = getRepo().getNode(qn);
|
|
3656
|
+
return node ? toGraphNode(node) : null;
|
|
3657
|
+
}
|
|
3658
|
+
if (method === "getNodeAtCursor") {
|
|
3659
|
+
const filePath = params["filePath"];
|
|
3660
|
+
const line = params["line"];
|
|
3661
|
+
const nodes = getRepo().getNodesByFile(filePath).filter(
|
|
3662
|
+
(n) => n.lineStart !== null && n.lineEnd !== null && n.lineStart <= line && n.lineEnd >= line
|
|
3663
|
+
);
|
|
3664
|
+
if (nodes.length === 0) return null;
|
|
3665
|
+
nodes.sort((a, b) => a.lineEnd - a.lineStart - (b.lineEnd - b.lineStart));
|
|
3666
|
+
return toGraphNode(nodes[0]);
|
|
3667
|
+
}
|
|
3668
|
+
if (method === "searchNodes") {
|
|
3669
|
+
const query = params["query"];
|
|
3670
|
+
const limit = params["limit"] ?? 20;
|
|
3671
|
+
return getRepo().searchNodes(query, limit).map(toGraphNode);
|
|
3672
|
+
}
|
|
3673
|
+
if (method === "getEdgesBySource") {
|
|
3674
|
+
const qn = params["qualifiedName"];
|
|
3675
|
+
return getRepo().getEdgesBySource(qn).map(toGraphEdge);
|
|
3676
|
+
}
|
|
3677
|
+
if (method === "getEdgesByTarget") {
|
|
3678
|
+
const qn = params["qualifiedName"];
|
|
3679
|
+
return getRepo().getEdgesByTarget(qn).map(toGraphEdge);
|
|
3680
|
+
}
|
|
3681
|
+
if (method === "getEdgesAmong") {
|
|
3682
|
+
const qns = new Set(params["qualifiedNames"]);
|
|
3683
|
+
return getRepo().getEdgesAmong(qns).map(toGraphEdge);
|
|
3684
|
+
}
|
|
3685
|
+
if (method === "getImpactRadius") {
|
|
3686
|
+
const changedFiles = params["changedFiles"];
|
|
3687
|
+
const maxDepth = params["maxDepth"] ?? 2;
|
|
3688
|
+
return computeImpactRadius2(changedFiles, maxDepth);
|
|
3689
|
+
}
|
|
3690
|
+
if (method === "getNodesBySize") {
|
|
3691
|
+
const minLines = params["minLines"] ?? 50;
|
|
3692
|
+
const maxLines = params["maxLines"];
|
|
3693
|
+
const kind = params["kind"];
|
|
3694
|
+
const filePathPattern = params["filePathPattern"];
|
|
3695
|
+
const limit = params["limit"] ?? 50;
|
|
3696
|
+
return getRepo().getNodesBySize(minLines, maxLines, kind, filePathPattern, limit).map((n) => ({
|
|
3697
|
+
...toGraphNode(n),
|
|
3698
|
+
lineCount: n.lineCount
|
|
3699
|
+
}));
|
|
3700
|
+
}
|
|
3701
|
+
if (method === "buildGraph") {
|
|
3702
|
+
const workspaceRoot = params["workspaceRoot"];
|
|
3703
|
+
const fullRebuild = params["fullRebuild"] ?? false;
|
|
3704
|
+
const dbPath = getDbPath(workspaceRoot);
|
|
3705
|
+
const buildRepo = new SqliteGraphRepository(dbPath);
|
|
3706
|
+
const parser = new CodeParser();
|
|
3707
|
+
try {
|
|
3708
|
+
const result = fullRebuild ? fullBuild(workspaceRoot, buildRepo, parser) : incrementalUpdate(workspaceRoot, buildRepo, parser);
|
|
3709
|
+
const verb = fullRebuild ? "Full build" : "Incremental update";
|
|
3710
|
+
const msg = `${verb}: ${result.filesUpdated} files, ${result.totalNodes} nodes, ${result.totalEdges} edges.`;
|
|
3711
|
+
openRepo(dbPath);
|
|
3712
|
+
return { success: true, message: msg };
|
|
3713
|
+
} catch (err) {
|
|
3714
|
+
return { success: false, message: String(err) };
|
|
3715
|
+
} finally {
|
|
3716
|
+
buildRepo.close();
|
|
3717
|
+
}
|
|
3718
|
+
}
|
|
3719
|
+
if (method === "updateGraph") {
|
|
3720
|
+
return dispatch("buildGraph", { ...params, fullRebuild: false });
|
|
3721
|
+
}
|
|
3722
|
+
if (method === "embedGraph") {
|
|
3723
|
+
const workspaceRoot = params["workspaceRoot"];
|
|
3724
|
+
const dbPath = getDbPath(workspaceRoot);
|
|
3725
|
+
const embRepo = new SqliteGraphRepository(dbPath);
|
|
3726
|
+
const embStore = new SqliteEmbeddingStore(dbPath);
|
|
3727
|
+
try {
|
|
3728
|
+
const result = await embedGraph({ repo: embRepo, embStore });
|
|
3729
|
+
if (result["status"] === "error") {
|
|
3730
|
+
return { success: false, message: String(result["error"]) };
|
|
3731
|
+
}
|
|
3732
|
+
return { success: true, message: String(result["summary"] ?? "Embeddings complete.") };
|
|
3733
|
+
} catch (err) {
|
|
3734
|
+
return { success: false, message: String(err) };
|
|
3735
|
+
} finally {
|
|
3736
|
+
embStore.close();
|
|
3737
|
+
embRepo.close();
|
|
3738
|
+
}
|
|
3739
|
+
}
|
|
3740
|
+
throw new Error(`Unknown method: ${method}`);
|
|
3741
|
+
}
|
|
3742
|
+
function checkSchema() {
|
|
3743
|
+
if (!repo) return "Database is not open";
|
|
3744
|
+
try {
|
|
3745
|
+
const stats = repo.getStats();
|
|
3746
|
+
void stats;
|
|
3747
|
+
return void 0;
|
|
3748
|
+
} catch (err) {
|
|
3749
|
+
return String(err);
|
|
3750
|
+
}
|
|
3751
|
+
}
|
|
3752
|
+
function send(response) {
|
|
3753
|
+
process.stdout.write(JSON.stringify(response) + "\n");
|
|
3754
|
+
}
|
|
3755
|
+
async function runIpcWorker() {
|
|
3756
|
+
const repoArgIdx = process.argv.indexOf("--repo");
|
|
3757
|
+
if (repoArgIdx !== -1 && process.argv[repoArgIdx + 1]) {
|
|
3758
|
+
const workspaceRoot = path12.resolve(process.argv[repoArgIdx + 1]);
|
|
3759
|
+
const dbPath = getDbPath(workspaceRoot);
|
|
3760
|
+
try {
|
|
3761
|
+
openRepo(dbPath);
|
|
3762
|
+
} catch {
|
|
3763
|
+
}
|
|
3764
|
+
}
|
|
3765
|
+
const rl = readline.createInterface({ input: process.stdin, crlfDelay: Infinity });
|
|
3766
|
+
rl.on("line", (line) => {
|
|
3767
|
+
const trimmed = line.trim();
|
|
3768
|
+
if (!trimmed) return;
|
|
3769
|
+
let req;
|
|
3770
|
+
try {
|
|
3771
|
+
req = JSON.parse(trimmed);
|
|
3772
|
+
} catch {
|
|
3773
|
+
return;
|
|
3774
|
+
}
|
|
3775
|
+
void dispatch(req.method, req.params ?? {}).then((result) => send({ id: req.id, result })).catch((err) => send({ id: req.id, error: String(err) }));
|
|
3776
|
+
});
|
|
3777
|
+
rl.on("close", () => {
|
|
3778
|
+
try {
|
|
3779
|
+
repo?.close();
|
|
3780
|
+
} catch {
|
|
3781
|
+
}
|
|
3782
|
+
process.exit(0);
|
|
3783
|
+
});
|
|
3784
|
+
send({ id: 0, result: { ready: true } });
|
|
3785
|
+
}
|
|
3786
|
+
|
|
6564
3787
|
// src/adapters/cli/cli.ts
|
|
6565
3788
|
function getVersion() {
|
|
6566
3789
|
try {
|
|
@@ -6607,8 +3830,8 @@ ${c} \u25CF\u2500\u2500\u25CF\u2500\u2500\u25CF${r} ${d}smarter code revi
|
|
|
6607
3830
|
`);
|
|
6608
3831
|
}
|
|
6609
3832
|
function handleInit(repoArg, dryRun) {
|
|
6610
|
-
const repoRoot = repoArg ?
|
|
6611
|
-
const mcpPath =
|
|
3833
|
+
const repoRoot = repoArg ? path13.resolve(repoArg) : findRepoRoot() ?? process.cwd();
|
|
3834
|
+
const mcpPath = path13.join(repoRoot, ".mcp.json");
|
|
6612
3835
|
const newEntry = {
|
|
6613
3836
|
command: "npx",
|
|
6614
3837
|
args: ["codeorbit", "serve"]
|
|
@@ -6648,23 +3871,23 @@ function handleInit(repoArg, dryRun) {
|
|
|
6648
3871
|
console.log(" 1. codeorbit build # build the knowledge graph");
|
|
6649
3872
|
console.log(" 2. Restart Claude Code # to pick up the new MCP server");
|
|
6650
3873
|
}
|
|
6651
|
-
var
|
|
6652
|
-
|
|
3874
|
+
var program = new Command();
|
|
3875
|
+
program.name("codeorbit").description("Persistent incremental knowledge graph for code reviews").version(getVersion(), "-v, --version").action(() => {
|
|
6653
3876
|
printBanner();
|
|
6654
3877
|
});
|
|
6655
|
-
|
|
3878
|
+
program.command("install").description("Register MCP server with Claude Code (creates .mcp.json)").option("--repo <path>", "Repository root (auto-detected)").option("--dry-run", "Show what would be done without writing files", false).action((opts) => {
|
|
6656
3879
|
handleInit(opts.repo, opts.dryRun);
|
|
6657
3880
|
});
|
|
6658
|
-
|
|
3881
|
+
program.command("init").description("Alias for install").option("--repo <path>", "Repository root (auto-detected)").option("--dry-run", "Show what would be done without writing files", false).action((opts) => {
|
|
6659
3882
|
handleInit(opts.repo, opts.dryRun);
|
|
6660
3883
|
});
|
|
6661
|
-
|
|
6662
|
-
const repoRoot = opts.repo ?
|
|
3884
|
+
program.command("build").description("Full graph build (re-parse all files)").option("--repo <path>", "Repository root (auto-detected)").action((opts) => {
|
|
3885
|
+
const repoRoot = opts.repo ? path13.resolve(opts.repo) : findProjectRoot();
|
|
6663
3886
|
const dbPath = getDbPath(repoRoot);
|
|
6664
|
-
const
|
|
3887
|
+
const repo2 = new SqliteGraphRepository(dbPath);
|
|
6665
3888
|
const parser = new CodeParser();
|
|
6666
3889
|
try {
|
|
6667
|
-
const result = fullBuild(repoRoot,
|
|
3890
|
+
const result = fullBuild(repoRoot, repo2, parser);
|
|
6668
3891
|
console.log(
|
|
6669
3892
|
`Full build: ${result.filesUpdated} files, ${result.totalNodes} nodes, ${result.totalEdges} edges`
|
|
6670
3893
|
);
|
|
@@ -6675,11 +3898,11 @@ program2.command("build").description("Full graph build (re-parse all files)").o
|
|
|
6675
3898
|
}
|
|
6676
3899
|
}
|
|
6677
3900
|
} finally {
|
|
6678
|
-
|
|
3901
|
+
repo2.close();
|
|
6679
3902
|
}
|
|
6680
3903
|
});
|
|
6681
|
-
|
|
6682
|
-
const repoRoot = opts.repo ?
|
|
3904
|
+
program.command("update").description("Incremental update (only changed files)").option("--base <ref>", "Git diff base", "HEAD~1").option("--repo <path>", "Repository root (auto-detected)").action((opts) => {
|
|
3905
|
+
const repoRoot = opts.repo ? path13.resolve(opts.repo) : findRepoRoot();
|
|
6683
3906
|
if (!repoRoot) {
|
|
6684
3907
|
console.error(
|
|
6685
3908
|
"Not in a git repository. 'update' requires git for diffing."
|
|
@@ -6688,10 +3911,10 @@ program2.command("update").description("Incremental update (only changed files)"
|
|
|
6688
3911
|
process.exit(1);
|
|
6689
3912
|
}
|
|
6690
3913
|
const dbPath = getDbPath(repoRoot);
|
|
6691
|
-
const
|
|
3914
|
+
const repo2 = new SqliteGraphRepository(dbPath);
|
|
6692
3915
|
const parser = new CodeParser();
|
|
6693
3916
|
try {
|
|
6694
|
-
const result = incrementalUpdate(repoRoot,
|
|
3917
|
+
const result = incrementalUpdate(repoRoot, repo2, parser, opts.base);
|
|
6695
3918
|
console.log(
|
|
6696
3919
|
`Incremental: ${result.filesUpdated} files updated, ${result.totalNodes} nodes, ${result.totalEdges} edges`
|
|
6697
3920
|
);
|
|
@@ -6702,23 +3925,23 @@ program2.command("update").description("Incremental update (only changed files)"
|
|
|
6702
3925
|
}
|
|
6703
3926
|
}
|
|
6704
3927
|
} finally {
|
|
6705
|
-
|
|
3928
|
+
repo2.close();
|
|
6706
3929
|
}
|
|
6707
3930
|
});
|
|
6708
|
-
|
|
6709
|
-
const repoRoot = opts.repo ?
|
|
3931
|
+
program.command("watch").description("Watch for changes and auto-update").option("--repo <path>", "Repository root (auto-detected)").action(async (opts) => {
|
|
3932
|
+
const repoRoot = opts.repo ? path13.resolve(opts.repo) : findProjectRoot();
|
|
6710
3933
|
const dbPath = getDbPath(repoRoot);
|
|
6711
|
-
const
|
|
3934
|
+
const repo2 = new SqliteGraphRepository(dbPath);
|
|
6712
3935
|
const parser = new CodeParser();
|
|
6713
|
-
await watch(repoRoot,
|
|
6714
|
-
|
|
3936
|
+
await watch(repoRoot, repo2, parser);
|
|
3937
|
+
repo2.close();
|
|
6715
3938
|
});
|
|
6716
|
-
|
|
6717
|
-
const repoRoot = opts.repo ?
|
|
3939
|
+
program.command("status").description("Show graph statistics").option("--repo <path>", "Repository root (auto-detected)").action((opts) => {
|
|
3940
|
+
const repoRoot = opts.repo ? path13.resolve(opts.repo) : findProjectRoot();
|
|
6718
3941
|
const dbPath = getDbPath(repoRoot);
|
|
6719
|
-
const
|
|
3942
|
+
const repo2 = new SqliteGraphRepository(dbPath);
|
|
6720
3943
|
try {
|
|
6721
|
-
const stats =
|
|
3944
|
+
const stats = repo2.getStats();
|
|
6722
3945
|
console.log(`codeorbit v${getVersion()}`);
|
|
6723
3946
|
console.log(`Repo: ${repoRoot}`);
|
|
6724
3947
|
console.log(`DB: ${dbPath}`);
|
|
@@ -6728,12 +3951,12 @@ program2.command("status").description("Show graph statistics").option("--repo <
|
|
|
6728
3951
|
console.log(`Languages: ${stats.languages.join(", ")}`);
|
|
6729
3952
|
console.log(`Last updated: ${stats.lastUpdated ?? "never"}`);
|
|
6730
3953
|
} finally {
|
|
6731
|
-
|
|
3954
|
+
repo2.close();
|
|
6732
3955
|
}
|
|
6733
3956
|
});
|
|
6734
|
-
|
|
6735
|
-
const repoRoot = opts.repo ?
|
|
6736
|
-
const htmlPath =
|
|
3957
|
+
program.command("visualize").description("Generate interactive HTML graph visualization").option("--repo <path>", "Repository root (auto-detected)").action((opts) => {
|
|
3958
|
+
const repoRoot = opts.repo ? path13.resolve(opts.repo) : findProjectRoot();
|
|
3959
|
+
const htmlPath = path13.join(repoRoot, ".codeorbit", "graph.html");
|
|
6737
3960
|
console.log("Visualization not yet implemented in the TypeScript port.");
|
|
6738
3961
|
console.log(`Would write to: ${htmlPath}`);
|
|
6739
3962
|
console.log(
|
|
@@ -6741,8 +3964,11 @@ program2.command("visualize").description("Generate interactive HTML graph visua
|
|
|
6741
3964
|
);
|
|
6742
3965
|
process.exit(1);
|
|
6743
3966
|
});
|
|
6744
|
-
|
|
3967
|
+
program.command("serve").description("Start MCP server (stdio transport)").action(async () => {
|
|
6745
3968
|
await runMcpServer();
|
|
6746
3969
|
});
|
|
6747
|
-
|
|
3970
|
+
program.command("ipc").description("Start persistent IPC worker for the VS Code extension (internal)").option("--repo <path>", "Repository root (auto-detected)").action(async () => {
|
|
3971
|
+
await runIpcWorker();
|
|
3972
|
+
});
|
|
3973
|
+
program.parse();
|
|
6748
3974
|
//# sourceMappingURL=cli.js.map
|