typescript-language-server 4.4.1 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/lib/cli.mjs +472 -238
- package/lib/cli.mjs.map +1 -1
- package/package.json +18 -18
package/lib/cli.mjs
CHANGED
|
@@ -116,7 +116,7 @@ function requireArgument() {
|
|
|
116
116
|
this._name = name;
|
|
117
117
|
break;
|
|
118
118
|
}
|
|
119
|
-
if (this._name.
|
|
119
|
+
if (this._name.endsWith('...')) {
|
|
120
120
|
this.variadic = true;
|
|
121
121
|
this._name = this._name.slice(0, -3);
|
|
122
122
|
}
|
|
@@ -124,11 +124,12 @@ function requireArgument() {
|
|
|
124
124
|
name() {
|
|
125
125
|
return this._name;
|
|
126
126
|
}
|
|
127
|
-
|
|
127
|
+
_collectValue(value, previous) {
|
|
128
128
|
if (previous === this.defaultValue || !Array.isArray(previous)) {
|
|
129
129
|
return [ value ];
|
|
130
130
|
}
|
|
131
|
-
|
|
131
|
+
previous.push(value);
|
|
132
|
+
return previous;
|
|
132
133
|
}
|
|
133
134
|
default(value, description) {
|
|
134
135
|
this.defaultValue = value;
|
|
@@ -146,7 +147,7 @@ function requireArgument() {
|
|
|
146
147
|
throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(', ')}.`);
|
|
147
148
|
}
|
|
148
149
|
if (this.variadic) {
|
|
149
|
-
return this.
|
|
150
|
+
return this._collectValue(arg, previous);
|
|
150
151
|
}
|
|
151
152
|
return arg;
|
|
152
153
|
};
|
|
@@ -183,10 +184,14 @@ function requireHelp() {
|
|
|
183
184
|
class Help {
|
|
184
185
|
constructor() {
|
|
185
186
|
this.helpWidth = undefined;
|
|
187
|
+
this.minWidthToWrap = 40;
|
|
186
188
|
this.sortSubcommands = false;
|
|
187
189
|
this.sortOptions = false;
|
|
188
190
|
this.showGlobalOptions = false;
|
|
189
191
|
}
|
|
192
|
+
prepareContext(contextOptions) {
|
|
193
|
+
this.helpWidth = this.helpWidth ?? contextOptions.helpWidth ?? 80;
|
|
194
|
+
}
|
|
190
195
|
visibleCommands(cmd) {
|
|
191
196
|
const visibleCommands = cmd.commands.filter(cmd => !cmd._hidden);
|
|
192
197
|
const helpCommand = cmd._getHelpCommand();
|
|
@@ -255,16 +260,16 @@ function requireHelp() {
|
|
|
255
260
|
return argument.name();
|
|
256
261
|
}
|
|
257
262
|
longestSubcommandTermLength(cmd, helper) {
|
|
258
|
-
return helper.visibleCommands(cmd).reduce((max, command) => Math.max(max, helper.subcommandTerm(command)
|
|
263
|
+
return helper.visibleCommands(cmd).reduce((max, command) => Math.max(max, this.displayWidth(helper.styleSubcommandTerm(helper.subcommandTerm(command)))), 0);
|
|
259
264
|
}
|
|
260
265
|
longestOptionTermLength(cmd, helper) {
|
|
261
|
-
return helper.visibleOptions(cmd).reduce((max, option) => Math.max(max, helper.optionTerm(option)
|
|
266
|
+
return helper.visibleOptions(cmd).reduce((max, option) => Math.max(max, this.displayWidth(helper.styleOptionTerm(helper.optionTerm(option)))), 0);
|
|
262
267
|
}
|
|
263
268
|
longestGlobalOptionTermLength(cmd, helper) {
|
|
264
|
-
return helper.visibleGlobalOptions(cmd).reduce((max, option) => Math.max(max, helper.optionTerm(option)
|
|
269
|
+
return helper.visibleGlobalOptions(cmd).reduce((max, option) => Math.max(max, this.displayWidth(helper.styleOptionTerm(helper.optionTerm(option)))), 0);
|
|
265
270
|
}
|
|
266
271
|
longestArgumentTermLength(cmd, helper) {
|
|
267
|
-
return helper.visibleArguments(cmd).reduce((max, argument) => Math.max(max, helper.argumentTerm(argument)
|
|
272
|
+
return helper.visibleArguments(cmd).reduce((max, argument) => Math.max(max, this.displayWidth(helper.styleArgumentTerm(helper.argumentTerm(argument)))), 0);
|
|
268
273
|
}
|
|
269
274
|
commandUsage(cmd) {
|
|
270
275
|
let cmdName = cmd._name;
|
|
@@ -301,7 +306,11 @@ function requireHelp() {
|
|
|
301
306
|
extraInfo.push(`env: ${option.envVar}`);
|
|
302
307
|
}
|
|
303
308
|
if (extraInfo.length > 0) {
|
|
304
|
-
|
|
309
|
+
const extraDescription = `(${extraInfo.join(', ')})`;
|
|
310
|
+
if (option.description) {
|
|
311
|
+
return `${option.description} ${extraDescription}`;
|
|
312
|
+
}
|
|
313
|
+
return extraDescription;
|
|
305
314
|
}
|
|
306
315
|
return option.description;
|
|
307
316
|
}
|
|
@@ -314,77 +323,175 @@ function requireHelp() {
|
|
|
314
323
|
extraInfo.push(`default: ${argument.defaultValueDescription || JSON.stringify(argument.defaultValue)}`);
|
|
315
324
|
}
|
|
316
325
|
if (extraInfo.length > 0) {
|
|
317
|
-
const
|
|
326
|
+
const extraDescription = `(${extraInfo.join(', ')})`;
|
|
318
327
|
if (argument.description) {
|
|
319
|
-
return `${argument.description} ${
|
|
328
|
+
return `${argument.description} ${extraDescription}`;
|
|
320
329
|
}
|
|
321
|
-
return
|
|
330
|
+
return extraDescription;
|
|
322
331
|
}
|
|
323
332
|
return argument.description;
|
|
324
333
|
}
|
|
334
|
+
formatItemList(heading, items, helper) {
|
|
335
|
+
if (items.length === 0) return [];
|
|
336
|
+
return [ helper.styleTitle(heading), ...items, '' ];
|
|
337
|
+
}
|
|
338
|
+
groupItems(unsortedItems, visibleItems, getGroup) {
|
|
339
|
+
const result = new Map;
|
|
340
|
+
unsortedItems.forEach(item => {
|
|
341
|
+
const group = getGroup(item);
|
|
342
|
+
if (!result.has(group)) result.set(group, []);
|
|
343
|
+
});
|
|
344
|
+
visibleItems.forEach(item => {
|
|
345
|
+
const group = getGroup(item);
|
|
346
|
+
if (!result.has(group)) {
|
|
347
|
+
result.set(group, []);
|
|
348
|
+
}
|
|
349
|
+
result.get(group).push(item);
|
|
350
|
+
});
|
|
351
|
+
return result;
|
|
352
|
+
}
|
|
325
353
|
formatHelp(cmd, helper) {
|
|
326
354
|
const termWidth = helper.padWidth(cmd, helper);
|
|
327
|
-
const helpWidth = helper.helpWidth
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
function formatItem(term, description) {
|
|
331
|
-
if (description) {
|
|
332
|
-
const fullText = `${term.padEnd(termWidth + itemSeparatorWidth)}${description}`;
|
|
333
|
-
return helper.wrap(fullText, helpWidth - itemIndentWidth, termWidth + itemSeparatorWidth);
|
|
334
|
-
}
|
|
335
|
-
return term;
|
|
355
|
+
const helpWidth = helper.helpWidth ?? 80;
|
|
356
|
+
function callFormatItem(term, description) {
|
|
357
|
+
return helper.formatItem(term, termWidth, description, helper);
|
|
336
358
|
}
|
|
337
|
-
|
|
338
|
-
return textArray.join('\n').replace(/^/gm, ' '.repeat(itemIndentWidth));
|
|
339
|
-
}
|
|
340
|
-
let output = [ `Usage: ${helper.commandUsage(cmd)}`, '' ];
|
|
359
|
+
let output = [ `${helper.styleTitle('Usage:')} ${helper.styleUsage(helper.commandUsage(cmd))}`, '' ];
|
|
341
360
|
const commandDescription = helper.commandDescription(cmd);
|
|
342
361
|
if (commandDescription.length > 0) {
|
|
343
|
-
output = output.concat([ helper.
|
|
344
|
-
}
|
|
345
|
-
const argumentList = helper.visibleArguments(cmd).map(argument =>
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
output = output.concat([ 'Commands:', formatList(commandList), '' ]);
|
|
362
|
-
}
|
|
362
|
+
output = output.concat([ helper.boxWrap(helper.styleCommandDescription(commandDescription), helpWidth), '' ]);
|
|
363
|
+
}
|
|
364
|
+
const argumentList = helper.visibleArguments(cmd).map(argument => callFormatItem(helper.styleArgumentTerm(helper.argumentTerm(argument)), helper.styleArgumentDescription(helper.argumentDescription(argument))));
|
|
365
|
+
output = output.concat(this.formatItemList('Arguments:', argumentList, helper));
|
|
366
|
+
const optionGroups = this.groupItems(cmd.options, helper.visibleOptions(cmd), option => option.helpGroupHeading ?? 'Options:');
|
|
367
|
+
optionGroups.forEach((options, group) => {
|
|
368
|
+
const optionList = options.map(option => callFormatItem(helper.styleOptionTerm(helper.optionTerm(option)), helper.styleOptionDescription(helper.optionDescription(option))));
|
|
369
|
+
output = output.concat(this.formatItemList(group, optionList, helper));
|
|
370
|
+
});
|
|
371
|
+
if (helper.showGlobalOptions) {
|
|
372
|
+
const globalOptionList = helper.visibleGlobalOptions(cmd).map(option => callFormatItem(helper.styleOptionTerm(helper.optionTerm(option)), helper.styleOptionDescription(helper.optionDescription(option))));
|
|
373
|
+
output = output.concat(this.formatItemList('Global Options:', globalOptionList, helper));
|
|
374
|
+
}
|
|
375
|
+
const commandGroups = this.groupItems(cmd.commands, helper.visibleCommands(cmd), sub => sub.helpGroup() || 'Commands:');
|
|
376
|
+
commandGroups.forEach((commands, group) => {
|
|
377
|
+
const commandList = commands.map(sub => callFormatItem(helper.styleSubcommandTerm(helper.subcommandTerm(sub)), helper.styleSubcommandDescription(helper.subcommandDescription(sub))));
|
|
378
|
+
output = output.concat(this.formatItemList(group, commandList, helper));
|
|
379
|
+
});
|
|
363
380
|
return output.join('\n');
|
|
364
381
|
}
|
|
382
|
+
displayWidth(str) {
|
|
383
|
+
return stripColor(str).length;
|
|
384
|
+
}
|
|
385
|
+
styleTitle(str) {
|
|
386
|
+
return str;
|
|
387
|
+
}
|
|
388
|
+
styleUsage(str) {
|
|
389
|
+
return str.split(' ').map(word => {
|
|
390
|
+
if (word === '[options]') return this.styleOptionText(word);
|
|
391
|
+
if (word === '[command]') return this.styleSubcommandText(word);
|
|
392
|
+
if (word[0] === '[' || word[0] === '<') return this.styleArgumentText(word);
|
|
393
|
+
return this.styleCommandText(word);
|
|
394
|
+
}).join(' ');
|
|
395
|
+
}
|
|
396
|
+
styleCommandDescription(str) {
|
|
397
|
+
return this.styleDescriptionText(str);
|
|
398
|
+
}
|
|
399
|
+
styleOptionDescription(str) {
|
|
400
|
+
return this.styleDescriptionText(str);
|
|
401
|
+
}
|
|
402
|
+
styleSubcommandDescription(str) {
|
|
403
|
+
return this.styleDescriptionText(str);
|
|
404
|
+
}
|
|
405
|
+
styleArgumentDescription(str) {
|
|
406
|
+
return this.styleDescriptionText(str);
|
|
407
|
+
}
|
|
408
|
+
styleDescriptionText(str) {
|
|
409
|
+
return str;
|
|
410
|
+
}
|
|
411
|
+
styleOptionTerm(str) {
|
|
412
|
+
return this.styleOptionText(str);
|
|
413
|
+
}
|
|
414
|
+
styleSubcommandTerm(str) {
|
|
415
|
+
return str.split(' ').map(word => {
|
|
416
|
+
if (word === '[options]') return this.styleOptionText(word);
|
|
417
|
+
if (word[0] === '[' || word[0] === '<') return this.styleArgumentText(word);
|
|
418
|
+
return this.styleSubcommandText(word);
|
|
419
|
+
}).join(' ');
|
|
420
|
+
}
|
|
421
|
+
styleArgumentTerm(str) {
|
|
422
|
+
return this.styleArgumentText(str);
|
|
423
|
+
}
|
|
424
|
+
styleOptionText(str) {
|
|
425
|
+
return str;
|
|
426
|
+
}
|
|
427
|
+
styleArgumentText(str) {
|
|
428
|
+
return str;
|
|
429
|
+
}
|
|
430
|
+
styleSubcommandText(str) {
|
|
431
|
+
return str;
|
|
432
|
+
}
|
|
433
|
+
styleCommandText(str) {
|
|
434
|
+
return str;
|
|
435
|
+
}
|
|
365
436
|
padWidth(cmd, helper) {
|
|
366
437
|
return Math.max(helper.longestOptionTermLength(cmd, helper), helper.longestGlobalOptionTermLength(cmd, helper), helper.longestSubcommandTermLength(cmd, helper), helper.longestArgumentTermLength(cmd, helper));
|
|
367
438
|
}
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
const
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
const
|
|
376
|
-
const
|
|
377
|
-
const
|
|
378
|
-
const
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
439
|
+
preformatted(str) {
|
|
440
|
+
return /\n[^\S\r\n]/.test(str);
|
|
441
|
+
}
|
|
442
|
+
formatItem(term, termWidth, description, helper) {
|
|
443
|
+
const itemIndent = 2;
|
|
444
|
+
const itemIndentStr = ' '.repeat(itemIndent);
|
|
445
|
+
if (!description) return itemIndentStr + term;
|
|
446
|
+
const paddedTerm = term.padEnd(termWidth + term.length - helper.displayWidth(term));
|
|
447
|
+
const spacerWidth = 2;
|
|
448
|
+
const helpWidth = this.helpWidth ?? 80;
|
|
449
|
+
const remainingWidth = helpWidth - termWidth - spacerWidth - itemIndent;
|
|
450
|
+
let formattedDescription;
|
|
451
|
+
if (remainingWidth < this.minWidthToWrap || helper.preformatted(description)) {
|
|
452
|
+
formattedDescription = description;
|
|
453
|
+
} else {
|
|
454
|
+
const wrappedDescription = helper.boxWrap(description, remainingWidth);
|
|
455
|
+
formattedDescription = wrappedDescription.replace(/\n/g, '\n' + ' '.repeat(termWidth + spacerWidth));
|
|
456
|
+
}
|
|
457
|
+
return itemIndentStr + paddedTerm + ' '.repeat(spacerWidth) + formattedDescription.replace(/\n/g, `\n${itemIndentStr}`);
|
|
458
|
+
}
|
|
459
|
+
boxWrap(str, width) {
|
|
460
|
+
if (width < this.minWidthToWrap) return str;
|
|
461
|
+
const rawLines = str.split(/\r\n|\n/);
|
|
462
|
+
const chunkPattern = /[\s]*[^\s]+/g;
|
|
463
|
+
const wrappedLines = [];
|
|
464
|
+
rawLines.forEach(line => {
|
|
465
|
+
const chunks = line.match(chunkPattern);
|
|
466
|
+
if (chunks === null) {
|
|
467
|
+
wrappedLines.push('');
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
let sumChunks = [ chunks.shift() ];
|
|
471
|
+
let sumWidth = this.displayWidth(sumChunks[0]);
|
|
472
|
+
chunks.forEach(chunk => {
|
|
473
|
+
const visibleWidth = this.displayWidth(chunk);
|
|
474
|
+
if (sumWidth + visibleWidth <= width) {
|
|
475
|
+
sumChunks.push(chunk);
|
|
476
|
+
sumWidth += visibleWidth;
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
wrappedLines.push(sumChunks.join(''));
|
|
480
|
+
const nextChunk = chunk.trimStart();
|
|
481
|
+
sumChunks = [ nextChunk ];
|
|
482
|
+
sumWidth = this.displayWidth(nextChunk);
|
|
483
|
+
});
|
|
484
|
+
wrappedLines.push(sumChunks.join(''));
|
|
485
|
+
});
|
|
486
|
+
return wrappedLines.join('\n');
|
|
385
487
|
}
|
|
386
488
|
}
|
|
489
|
+
function stripColor(str) {
|
|
490
|
+
const sgrPattern = /\x1b\[\d*(;\d*)*m/g;
|
|
491
|
+
return str.replace(sgrPattern, '');
|
|
492
|
+
}
|
|
387
493
|
help.Help = Help;
|
|
494
|
+
help.stripColor = stripColor;
|
|
388
495
|
return help;
|
|
389
496
|
}
|
|
390
497
|
|
|
@@ -420,6 +527,7 @@ function requireOption() {
|
|
|
420
527
|
this.argChoices = undefined;
|
|
421
528
|
this.conflictsWith = [];
|
|
422
529
|
this.implied = undefined;
|
|
530
|
+
this.helpGroupHeading = undefined;
|
|
423
531
|
}
|
|
424
532
|
default(value, description) {
|
|
425
533
|
this.defaultValue = value;
|
|
@@ -460,11 +568,12 @@ function requireOption() {
|
|
|
460
568
|
this.hidden = !!hide;
|
|
461
569
|
return this;
|
|
462
570
|
}
|
|
463
|
-
|
|
571
|
+
_collectValue(value, previous) {
|
|
464
572
|
if (previous === this.defaultValue || !Array.isArray(previous)) {
|
|
465
573
|
return [ value ];
|
|
466
574
|
}
|
|
467
|
-
|
|
575
|
+
previous.push(value);
|
|
576
|
+
return previous;
|
|
468
577
|
}
|
|
469
578
|
choices(values) {
|
|
470
579
|
this.argChoices = values.slice();
|
|
@@ -473,7 +582,7 @@ function requireOption() {
|
|
|
473
582
|
throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(', ')}.`);
|
|
474
583
|
}
|
|
475
584
|
if (this.variadic) {
|
|
476
|
-
return this.
|
|
585
|
+
return this._collectValue(arg, previous);
|
|
477
586
|
}
|
|
478
587
|
return arg;
|
|
479
588
|
};
|
|
@@ -486,7 +595,14 @@ function requireOption() {
|
|
|
486
595
|
return this.short.replace(/^-/, '');
|
|
487
596
|
}
|
|
488
597
|
attributeName() {
|
|
489
|
-
|
|
598
|
+
if (this.negate) {
|
|
599
|
+
return camelcase(this.name().replace(/^no-/, ''));
|
|
600
|
+
}
|
|
601
|
+
return camelcase(this.name());
|
|
602
|
+
}
|
|
603
|
+
helpGroup(heading) {
|
|
604
|
+
this.helpGroupHeading = heading;
|
|
605
|
+
return this;
|
|
490
606
|
}
|
|
491
607
|
is(arg) {
|
|
492
608
|
return this.short === arg || this.long === arg;
|
|
@@ -527,13 +643,25 @@ function requireOption() {
|
|
|
527
643
|
function splitOptionFlags(flags) {
|
|
528
644
|
let shortFlag;
|
|
529
645
|
let longFlag;
|
|
530
|
-
const
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
if (
|
|
646
|
+
const shortFlagExp = /^-[^-]$/;
|
|
647
|
+
const longFlagExp = /^--[^-]/;
|
|
648
|
+
const flagParts = flags.split(/[ |,]+/).concat('guard');
|
|
649
|
+
if (shortFlagExp.test(flagParts[0])) shortFlag = flagParts.shift();
|
|
650
|
+
if (longFlagExp.test(flagParts[0])) longFlag = flagParts.shift();
|
|
651
|
+
if (!shortFlag && shortFlagExp.test(flagParts[0])) shortFlag = flagParts.shift();
|
|
652
|
+
if (!shortFlag && longFlagExp.test(flagParts[0])) {
|
|
534
653
|
shortFlag = longFlag;
|
|
535
|
-
longFlag =
|
|
654
|
+
longFlag = flagParts.shift();
|
|
655
|
+
}
|
|
656
|
+
if (flagParts[0].startsWith('-')) {
|
|
657
|
+
const unsupportedFlag = flagParts[0];
|
|
658
|
+
const baseError = `option creation failed due to '${unsupportedFlag}' in option flags '${flags}'`;
|
|
659
|
+
if (/^-[^-][^-]/.test(unsupportedFlag)) throw new Error(`${baseError}\n- a short flag is a single dash and a single character\n - either use a single dash and a single character (for a short flag)\n - or use a double dash for a long option (and can have two, like '--ws, --workspace')`);
|
|
660
|
+
if (shortFlagExp.test(unsupportedFlag)) throw new Error(`${baseError}\n- too many short flags`);
|
|
661
|
+
if (longFlagExp.test(unsupportedFlag)) throw new Error(`${baseError}\n- too many long flags`);
|
|
662
|
+
throw new Error(`${baseError}\n- unrecognised flag format`);
|
|
536
663
|
}
|
|
664
|
+
if (shortFlag === undefined && longFlag === undefined) throw new Error(`option creation failed due to no flags found in '${flags}'.`);
|
|
537
665
|
return {
|
|
538
666
|
shortFlag: shortFlag,
|
|
539
667
|
longFlag: longFlag
|
|
@@ -630,7 +758,7 @@ function requireCommand() {
|
|
|
630
758
|
const process = process$1;
|
|
631
759
|
const {Argument: Argument, humanReadableArgName: humanReadableArgName} = requireArgument();
|
|
632
760
|
const {CommanderError: CommanderError} = requireError();
|
|
633
|
-
const {Help: Help} = requireHelp();
|
|
761
|
+
const {Help: Help, stripColor: stripColor} = requireHelp();
|
|
634
762
|
const {Option: Option, DualOptions: DualOptions} = requireOption();
|
|
635
763
|
const {suggestSimilar: suggestSimilar} = requireSuggestSimilar();
|
|
636
764
|
class Command extends EventEmitter {
|
|
@@ -640,7 +768,7 @@ function requireCommand() {
|
|
|
640
768
|
this.options = [];
|
|
641
769
|
this.parent = null;
|
|
642
770
|
this._allowUnknownOption = false;
|
|
643
|
-
this._allowExcessArguments =
|
|
771
|
+
this._allowExcessArguments = false;
|
|
644
772
|
this.registeredArguments = [];
|
|
645
773
|
this._args = this.registeredArguments;
|
|
646
774
|
this.args = [];
|
|
@@ -667,18 +795,25 @@ function requireCommand() {
|
|
|
667
795
|
this._lifeCycleHooks = {};
|
|
668
796
|
this._showHelpAfterError = false;
|
|
669
797
|
this._showSuggestionAfterError = true;
|
|
798
|
+
this._savedState = null;
|
|
670
799
|
this._outputConfiguration = {
|
|
671
800
|
writeOut: str => process.stdout.write(str),
|
|
672
801
|
writeErr: str => process.stderr.write(str),
|
|
802
|
+
outputError: (str, write) => write(str),
|
|
673
803
|
getOutHelpWidth: () => process.stdout.isTTY ? process.stdout.columns : undefined,
|
|
674
804
|
getErrHelpWidth: () => process.stderr.isTTY ? process.stderr.columns : undefined,
|
|
675
|
-
|
|
805
|
+
getOutHasColors: () => useColor() ?? (process.stdout.isTTY && process.stdout.hasColors?.()),
|
|
806
|
+
getErrHasColors: () => useColor() ?? (process.stderr.isTTY && process.stderr.hasColors?.()),
|
|
807
|
+
stripColor: str => stripColor(str)
|
|
676
808
|
};
|
|
677
809
|
this._hidden = false;
|
|
678
810
|
this._helpOption = undefined;
|
|
679
811
|
this._addImplicitHelpCommand = undefined;
|
|
680
812
|
this._helpCommand = undefined;
|
|
681
813
|
this._helpConfiguration = {};
|
|
814
|
+
this._helpGroupHeading = undefined;
|
|
815
|
+
this._defaultCommandGroup = undefined;
|
|
816
|
+
this._defaultOptionGroup = undefined;
|
|
682
817
|
}
|
|
683
818
|
copyInheritedSettings(sourceCommand) {
|
|
684
819
|
this._outputConfiguration = sourceCommand._outputConfiguration;
|
|
@@ -738,7 +873,10 @@ function requireCommand() {
|
|
|
738
873
|
}
|
|
739
874
|
configureOutput(configuration) {
|
|
740
875
|
if (configuration === undefined) return this._outputConfiguration;
|
|
741
|
-
|
|
876
|
+
this._outputConfiguration = {
|
|
877
|
+
...this._outputConfiguration,
|
|
878
|
+
...configuration
|
|
879
|
+
};
|
|
742
880
|
return this;
|
|
743
881
|
}
|
|
744
882
|
showHelpAfterError(displayHelp = true) {
|
|
@@ -765,12 +903,12 @@ function requireCommand() {
|
|
|
765
903
|
createArgument(name, description) {
|
|
766
904
|
return new Argument(name, description);
|
|
767
905
|
}
|
|
768
|
-
argument(name, description,
|
|
906
|
+
argument(name, description, parseArg, defaultValue) {
|
|
769
907
|
const argument = this.createArgument(name, description);
|
|
770
|
-
if (typeof
|
|
771
|
-
argument.default(defaultValue).argParser(
|
|
908
|
+
if (typeof parseArg === 'function') {
|
|
909
|
+
argument.default(defaultValue).argParser(parseArg);
|
|
772
910
|
} else {
|
|
773
|
-
argument.default(
|
|
911
|
+
argument.default(parseArg);
|
|
774
912
|
}
|
|
775
913
|
this.addArgument(argument);
|
|
776
914
|
return this;
|
|
@@ -783,7 +921,7 @@ function requireCommand() {
|
|
|
783
921
|
}
|
|
784
922
|
addArgument(argument) {
|
|
785
923
|
const previousArgument = this.registeredArguments.slice(-1)[0];
|
|
786
|
-
if (previousArgument
|
|
924
|
+
if (previousArgument?.variadic) {
|
|
787
925
|
throw new Error(`only the last argument can be variadic '${previousArgument.name()}'`);
|
|
788
926
|
}
|
|
789
927
|
if (argument.required && argument.defaultValue !== undefined && argument.parseArg === undefined) {
|
|
@@ -795,10 +933,13 @@ function requireCommand() {
|
|
|
795
933
|
helpCommand(enableOrNameAndArgs, description) {
|
|
796
934
|
if (typeof enableOrNameAndArgs === 'boolean') {
|
|
797
935
|
this._addImplicitHelpCommand = enableOrNameAndArgs;
|
|
936
|
+
if (enableOrNameAndArgs && this._defaultCommandGroup) {
|
|
937
|
+
this._initCommandGroup(this._getHelpCommand());
|
|
938
|
+
}
|
|
798
939
|
return this;
|
|
799
940
|
}
|
|
800
|
-
|
|
801
|
-
const [, helpName, helpArgs] =
|
|
941
|
+
const nameAndArgs = enableOrNameAndArgs ?? 'help [command]';
|
|
942
|
+
const [, helpName, helpArgs] = nameAndArgs.match(/([^ ]+) *(.*)/);
|
|
802
943
|
const helpDescription = description ?? 'display help for command';
|
|
803
944
|
const helpCommand = this.createCommand(helpName);
|
|
804
945
|
helpCommand.helpOption(false);
|
|
@@ -806,6 +947,7 @@ function requireCommand() {
|
|
|
806
947
|
if (helpDescription) helpCommand.description(helpDescription);
|
|
807
948
|
this._addImplicitHelpCommand = true;
|
|
808
949
|
this._helpCommand = helpCommand;
|
|
950
|
+
if (enableOrNameAndArgs || description) this._initCommandGroup(helpCommand);
|
|
809
951
|
return this;
|
|
810
952
|
}
|
|
811
953
|
addHelpCommand(helpCommand, deprecatedDescription) {
|
|
@@ -815,6 +957,7 @@ function requireCommand() {
|
|
|
815
957
|
}
|
|
816
958
|
this._addImplicitHelpCommand = true;
|
|
817
959
|
this._helpCommand = helpCommand;
|
|
960
|
+
this._initCommandGroup(helpCommand);
|
|
818
961
|
return this;
|
|
819
962
|
}
|
|
820
963
|
_getHelpCommand() {
|
|
@@ -895,6 +1038,7 @@ function requireCommand() {
|
|
|
895
1038
|
const matchingFlag = option.long && this._findOption(option.long) ? option.long : option.short;
|
|
896
1039
|
throw new Error(`Cannot add option '${option.flags}'${this._name && ` to command '${this._name}'`} due to conflicting flag '${matchingFlag}'\n- already used by option '${matchingOption.flags}'`);
|
|
897
1040
|
}
|
|
1041
|
+
this._initOptionGroup(option);
|
|
898
1042
|
this.options.push(option);
|
|
899
1043
|
}
|
|
900
1044
|
_registerCommand(command) {
|
|
@@ -905,6 +1049,7 @@ function requireCommand() {
|
|
|
905
1049
|
const newCmd = knownBy(command).join('|');
|
|
906
1050
|
throw new Error(`cannot add command '${newCmd}' as already have command '${existingCmd}'`);
|
|
907
1051
|
}
|
|
1052
|
+
this._initCommandGroup(command);
|
|
908
1053
|
this.commands.push(command);
|
|
909
1054
|
}
|
|
910
1055
|
addOption(option) {
|
|
@@ -927,7 +1072,7 @@ function requireCommand() {
|
|
|
927
1072
|
if (val !== null && option.parseArg) {
|
|
928
1073
|
val = this._callParseArg(option, val, oldValue, invalidValueMessage);
|
|
929
1074
|
} else if (val !== null && option.variadic) {
|
|
930
|
-
val = option.
|
|
1075
|
+
val = option._collectValue(val, oldValue);
|
|
931
1076
|
}
|
|
932
1077
|
if (val == null) {
|
|
933
1078
|
if (option.negate) {
|
|
@@ -1097,15 +1242,55 @@ function requireCommand() {
|
|
|
1097
1242
|
return userArgs;
|
|
1098
1243
|
}
|
|
1099
1244
|
parse(argv, parseOptions) {
|
|
1245
|
+
this._prepareForParse();
|
|
1100
1246
|
const userArgs = this._prepareUserArgs(argv, parseOptions);
|
|
1101
1247
|
this._parseCommand([], userArgs);
|
|
1102
1248
|
return this;
|
|
1103
1249
|
}
|
|
1104
1250
|
async parseAsync(argv, parseOptions) {
|
|
1251
|
+
this._prepareForParse();
|
|
1105
1252
|
const userArgs = this._prepareUserArgs(argv, parseOptions);
|
|
1106
1253
|
await this._parseCommand([], userArgs);
|
|
1107
1254
|
return this;
|
|
1108
1255
|
}
|
|
1256
|
+
_prepareForParse() {
|
|
1257
|
+
if (this._savedState === null) {
|
|
1258
|
+
this.saveStateBeforeParse();
|
|
1259
|
+
} else {
|
|
1260
|
+
this.restoreStateBeforeParse();
|
|
1261
|
+
}
|
|
1262
|
+
}
|
|
1263
|
+
saveStateBeforeParse() {
|
|
1264
|
+
this._savedState = {
|
|
1265
|
+
_name: this._name,
|
|
1266
|
+
_optionValues: {
|
|
1267
|
+
...this._optionValues
|
|
1268
|
+
},
|
|
1269
|
+
_optionValueSources: {
|
|
1270
|
+
...this._optionValueSources
|
|
1271
|
+
}
|
|
1272
|
+
};
|
|
1273
|
+
}
|
|
1274
|
+
restoreStateBeforeParse() {
|
|
1275
|
+
if (this._storeOptionsAsProperties) throw new Error(`Can not call parse again when storeOptionsAsProperties is true.\n- either make a new Command for each call to parse, or stop storing options as properties`);
|
|
1276
|
+
this._name = this._savedState._name;
|
|
1277
|
+
this._scriptPath = null;
|
|
1278
|
+
this.rawArgs = [];
|
|
1279
|
+
this._optionValues = {
|
|
1280
|
+
...this._savedState._optionValues
|
|
1281
|
+
};
|
|
1282
|
+
this._optionValueSources = {
|
|
1283
|
+
...this._savedState._optionValueSources
|
|
1284
|
+
};
|
|
1285
|
+
this.args = [];
|
|
1286
|
+
this.processedArgs = [];
|
|
1287
|
+
}
|
|
1288
|
+
_checkForMissingExecutable(executableFile, executableDir, subcommandName) {
|
|
1289
|
+
if (fs.existsSync(executableFile)) return;
|
|
1290
|
+
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';
|
|
1291
|
+
const executableMissing = `'${executableFile}' does not exist\n - if '${subcommandName}' is not meant to be an executable command, remove description parameter from '.command()' and use '.description()' instead\n - if the default executable name is not suitable, use the executableFile option to supply a custom name or path\n - ${executableDirMessage}`;
|
|
1292
|
+
throw new Error(executableMissing);
|
|
1293
|
+
}
|
|
1109
1294
|
_executeSubCommand(subcommand, args) {
|
|
1110
1295
|
args = args.slice();
|
|
1111
1296
|
let launchWithNode = false;
|
|
@@ -1126,7 +1311,7 @@ function requireCommand() {
|
|
|
1126
1311
|
let resolvedScriptPath;
|
|
1127
1312
|
try {
|
|
1128
1313
|
resolvedScriptPath = fs.realpathSync(this._scriptPath);
|
|
1129
|
-
} catch
|
|
1314
|
+
} catch {
|
|
1130
1315
|
resolvedScriptPath = this._scriptPath;
|
|
1131
1316
|
}
|
|
1132
1317
|
executableDir = path.resolve(path.dirname(resolvedScriptPath), executableDir);
|
|
@@ -1156,6 +1341,7 @@ function requireCommand() {
|
|
|
1156
1341
|
});
|
|
1157
1342
|
}
|
|
1158
1343
|
} else {
|
|
1344
|
+
this._checkForMissingExecutable(executableFile, executableDir, subcommand._name);
|
|
1159
1345
|
args.unshift(executableFile);
|
|
1160
1346
|
args = incrementNodeInspectorPort(process.execArgv).concat(args);
|
|
1161
1347
|
proc = childProcess.spawn(process.execPath, args, {
|
|
@@ -1183,9 +1369,7 @@ function requireCommand() {
|
|
|
1183
1369
|
});
|
|
1184
1370
|
proc.on('error', err => {
|
|
1185
1371
|
if (err.code === 'ENOENT') {
|
|
1186
|
-
|
|
1187
|
-
const executableMissing = `'${executableFile}' does not exist\n - if '${subcommand._name}' is not meant to be an executable command, remove description parameter from '.command()' and use '.description()' instead\n - if the default executable name is not suitable, use the executableFile option to supply a custom name or path\n - ${executableDirMessage}`;
|
|
1188
|
-
throw new Error(executableMissing);
|
|
1372
|
+
this._checkForMissingExecutable(executableFile, executableDir, subcommand._name);
|
|
1189
1373
|
} else if (err.code === 'EACCES') {
|
|
1190
1374
|
throw new Error(`'${executableFile}' not executable`);
|
|
1191
1375
|
}
|
|
@@ -1204,6 +1388,7 @@ function requireCommand() {
|
|
|
1204
1388
|
if (!subCommand) this.help({
|
|
1205
1389
|
error: true
|
|
1206
1390
|
});
|
|
1391
|
+
subCommand._prepareForParse();
|
|
1207
1392
|
let promiseChain;
|
|
1208
1393
|
promiseChain = this._chainOrCallSubCommandHook(promiseChain, subCommand, 'preSubcommand');
|
|
1209
1394
|
promiseChain = this._chainOrCall(promiseChain, () => {
|
|
@@ -1271,7 +1456,7 @@ function requireCommand() {
|
|
|
1271
1456
|
this.processedArgs = processedArgs;
|
|
1272
1457
|
}
|
|
1273
1458
|
_chainOrCall(promise, fn) {
|
|
1274
|
-
if (promise
|
|
1459
|
+
if (promise?.then && typeof promise.then === 'function') {
|
|
1275
1460
|
return promise.then(() => fn());
|
|
1276
1461
|
}
|
|
1277
1462
|
return fn();
|
|
@@ -1349,7 +1534,7 @@ function requireCommand() {
|
|
|
1349
1534
|
promiseChain = this._chainOrCallHooks(promiseChain, 'postAction');
|
|
1350
1535
|
return promiseChain;
|
|
1351
1536
|
}
|
|
1352
|
-
if (this.parent
|
|
1537
|
+
if (this.parent?.listenerCount(commandEvent)) {
|
|
1353
1538
|
checkForUnknownOptions();
|
|
1354
1539
|
this._processArguments();
|
|
1355
1540
|
this.parent.emit(commandEvent, operands, unknown);
|
|
@@ -1412,23 +1597,29 @@ function requireCommand() {
|
|
|
1412
1597
|
cmd._checkForConflictingLocalOptions();
|
|
1413
1598
|
});
|
|
1414
1599
|
}
|
|
1415
|
-
parseOptions(
|
|
1600
|
+
parseOptions(args) {
|
|
1416
1601
|
const operands = [];
|
|
1417
1602
|
const unknown = [];
|
|
1418
1603
|
let dest = operands;
|
|
1419
|
-
const args = argv.slice();
|
|
1420
1604
|
function maybeOption(arg) {
|
|
1421
1605
|
return arg.length > 1 && arg[0] === '-';
|
|
1422
1606
|
}
|
|
1607
|
+
const negativeNumberArg = arg => {
|
|
1608
|
+
if (!/^-\d*\.?\d+(e[+-]?\d+)?$/.test(arg)) return false;
|
|
1609
|
+
return !this._getCommandAndAncestors().some(cmd => cmd.options.map(opt => opt.short).some(short => /^-\d$/.test(short)));
|
|
1610
|
+
};
|
|
1423
1611
|
let activeVariadicOption = null;
|
|
1424
|
-
|
|
1425
|
-
|
|
1612
|
+
let activeGroup = null;
|
|
1613
|
+
let i = 0;
|
|
1614
|
+
while (i < args.length || activeGroup) {
|
|
1615
|
+
const arg = activeGroup ?? args[i++];
|
|
1616
|
+
activeGroup = null;
|
|
1426
1617
|
if (arg === '--') {
|
|
1427
1618
|
if (dest === unknown) dest.push(arg);
|
|
1428
|
-
dest.push(...args);
|
|
1619
|
+
dest.push(...args.slice(i));
|
|
1429
1620
|
break;
|
|
1430
1621
|
}
|
|
1431
|
-
if (activeVariadicOption && !maybeOption(arg)) {
|
|
1622
|
+
if (activeVariadicOption && (!maybeOption(arg) || negativeNumberArg(arg))) {
|
|
1432
1623
|
this.emit(`option:${activeVariadicOption.name()}`, arg);
|
|
1433
1624
|
continue;
|
|
1434
1625
|
}
|
|
@@ -1437,13 +1628,13 @@ function requireCommand() {
|
|
|
1437
1628
|
const option = this._findOption(arg);
|
|
1438
1629
|
if (option) {
|
|
1439
1630
|
if (option.required) {
|
|
1440
|
-
const value = args
|
|
1631
|
+
const value = args[i++];
|
|
1441
1632
|
if (value === undefined) this.optionMissingArgument(option);
|
|
1442
1633
|
this.emit(`option:${option.name()}`, value);
|
|
1443
1634
|
} else if (option.optional) {
|
|
1444
1635
|
let value = null;
|
|
1445
|
-
if (args.length
|
|
1446
|
-
value = args
|
|
1636
|
+
if (i < args.length && (!maybeOption(args[i]) || negativeNumberArg(args[i]))) {
|
|
1637
|
+
value = args[i++];
|
|
1447
1638
|
}
|
|
1448
1639
|
this.emit(`option:${option.name()}`, value);
|
|
1449
1640
|
} else {
|
|
@@ -1460,7 +1651,7 @@ function requireCommand() {
|
|
|
1460
1651
|
this.emit(`option:${option.name()}`, arg.slice(2));
|
|
1461
1652
|
} else {
|
|
1462
1653
|
this.emit(`option:${option.name()}`);
|
|
1463
|
-
|
|
1654
|
+
activeGroup = `-${arg.slice(2)}`;
|
|
1464
1655
|
}
|
|
1465
1656
|
continue;
|
|
1466
1657
|
}
|
|
@@ -1473,27 +1664,24 @@ function requireCommand() {
|
|
|
1473
1664
|
continue;
|
|
1474
1665
|
}
|
|
1475
1666
|
}
|
|
1476
|
-
if (maybeOption(arg)) {
|
|
1667
|
+
if (dest === operands && maybeOption(arg) && !(this.commands.length === 0 && negativeNumberArg(arg))) {
|
|
1477
1668
|
dest = unknown;
|
|
1478
1669
|
}
|
|
1479
1670
|
if ((this._enablePositionalOptions || this._passThroughOptions) && operands.length === 0 && unknown.length === 0) {
|
|
1480
1671
|
if (this._findCommand(arg)) {
|
|
1481
1672
|
operands.push(arg);
|
|
1482
|
-
|
|
1673
|
+
unknown.push(...args.slice(i));
|
|
1483
1674
|
break;
|
|
1484
1675
|
} else if (this._getHelpCommand() && arg === this._getHelpCommand().name()) {
|
|
1485
|
-
operands.push(arg);
|
|
1486
|
-
if (args.length > 0) operands.push(...args);
|
|
1676
|
+
operands.push(arg, ...args.slice(i));
|
|
1487
1677
|
break;
|
|
1488
1678
|
} else if (this._defaultCommandName) {
|
|
1489
|
-
unknown.push(arg);
|
|
1490
|
-
if (args.length > 0) unknown.push(...args);
|
|
1679
|
+
unknown.push(arg, ...args.slice(i));
|
|
1491
1680
|
break;
|
|
1492
1681
|
}
|
|
1493
1682
|
}
|
|
1494
1683
|
if (this._passThroughOptions) {
|
|
1495
|
-
dest.push(arg);
|
|
1496
|
-
if (args.length > 0) dest.push(...args);
|
|
1684
|
+
dest.push(arg, ...args.slice(i));
|
|
1497
1685
|
break;
|
|
1498
1686
|
}
|
|
1499
1687
|
dest.push(arg);
|
|
@@ -1704,6 +1892,27 @@ function requireCommand() {
|
|
|
1704
1892
|
this._name = str;
|
|
1705
1893
|
return this;
|
|
1706
1894
|
}
|
|
1895
|
+
helpGroup(heading) {
|
|
1896
|
+
if (heading === undefined) return this._helpGroupHeading ?? '';
|
|
1897
|
+
this._helpGroupHeading = heading;
|
|
1898
|
+
return this;
|
|
1899
|
+
}
|
|
1900
|
+
commandsGroup(heading) {
|
|
1901
|
+
if (heading === undefined) return this._defaultCommandGroup ?? '';
|
|
1902
|
+
this._defaultCommandGroup = heading;
|
|
1903
|
+
return this;
|
|
1904
|
+
}
|
|
1905
|
+
optionsGroup(heading) {
|
|
1906
|
+
if (heading === undefined) return this._defaultOptionGroup ?? '';
|
|
1907
|
+
this._defaultOptionGroup = heading;
|
|
1908
|
+
return this;
|
|
1909
|
+
}
|
|
1910
|
+
_initOptionGroup(option) {
|
|
1911
|
+
if (this._defaultOptionGroup && !option.helpGroupHeading) option.helpGroup(this._defaultOptionGroup);
|
|
1912
|
+
}
|
|
1913
|
+
_initCommandGroup(cmd) {
|
|
1914
|
+
if (this._defaultCommandGroup && !cmd.helpGroup()) cmd.helpGroup(this._defaultCommandGroup);
|
|
1915
|
+
}
|
|
1707
1916
|
nameFromFilename(filename) {
|
|
1708
1917
|
this._name = path.basename(filename, path.extname(filename));
|
|
1709
1918
|
return this;
|
|
@@ -1715,25 +1924,41 @@ function requireCommand() {
|
|
|
1715
1924
|
}
|
|
1716
1925
|
helpInformation(contextOptions) {
|
|
1717
1926
|
const helper = this.createHelp();
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1927
|
+
const context = this._getOutputContext(contextOptions);
|
|
1928
|
+
helper.prepareContext({
|
|
1929
|
+
error: context.error,
|
|
1930
|
+
helpWidth: context.helpWidth,
|
|
1931
|
+
outputHasColors: context.hasColors
|
|
1932
|
+
});
|
|
1933
|
+
const text = helper.formatHelp(this, helper);
|
|
1934
|
+
if (context.hasColors) return text;
|
|
1935
|
+
return this._outputConfiguration.stripColor(text);
|
|
1722
1936
|
}
|
|
1723
|
-
|
|
1937
|
+
_getOutputContext(contextOptions) {
|
|
1724
1938
|
contextOptions = contextOptions || {};
|
|
1725
|
-
const
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
let
|
|
1729
|
-
if (
|
|
1730
|
-
|
|
1939
|
+
const error = !!contextOptions.error;
|
|
1940
|
+
let baseWrite;
|
|
1941
|
+
let hasColors;
|
|
1942
|
+
let helpWidth;
|
|
1943
|
+
if (error) {
|
|
1944
|
+
baseWrite = str => this._outputConfiguration.writeErr(str);
|
|
1945
|
+
hasColors = this._outputConfiguration.getErrHasColors();
|
|
1946
|
+
helpWidth = this._outputConfiguration.getErrHelpWidth();
|
|
1731
1947
|
} else {
|
|
1732
|
-
|
|
1948
|
+
baseWrite = str => this._outputConfiguration.writeOut(str);
|
|
1949
|
+
hasColors = this._outputConfiguration.getOutHasColors();
|
|
1950
|
+
helpWidth = this._outputConfiguration.getOutHelpWidth();
|
|
1733
1951
|
}
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1952
|
+
const write = str => {
|
|
1953
|
+
if (!hasColors) str = this._outputConfiguration.stripColor(str);
|
|
1954
|
+
return baseWrite(str);
|
|
1955
|
+
};
|
|
1956
|
+
return {
|
|
1957
|
+
error: error,
|
|
1958
|
+
write: write,
|
|
1959
|
+
hasColors: hasColors,
|
|
1960
|
+
helpWidth: helpWidth
|
|
1961
|
+
};
|
|
1737
1962
|
}
|
|
1738
1963
|
outputHelp(contextOptions) {
|
|
1739
1964
|
let deprecatedCallback;
|
|
@@ -1741,35 +1966,44 @@ function requireCommand() {
|
|
|
1741
1966
|
deprecatedCallback = contextOptions;
|
|
1742
1967
|
contextOptions = undefined;
|
|
1743
1968
|
}
|
|
1744
|
-
const
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1969
|
+
const outputContext = this._getOutputContext(contextOptions);
|
|
1970
|
+
const eventContext = {
|
|
1971
|
+
error: outputContext.error,
|
|
1972
|
+
write: outputContext.write,
|
|
1973
|
+
command: this
|
|
1974
|
+
};
|
|
1975
|
+
this._getCommandAndAncestors().reverse().forEach(command => command.emit('beforeAllHelp', eventContext));
|
|
1976
|
+
this.emit('beforeHelp', eventContext);
|
|
1977
|
+
let helpInformation = this.helpInformation({
|
|
1978
|
+
error: outputContext.error
|
|
1979
|
+
});
|
|
1748
1980
|
if (deprecatedCallback) {
|
|
1749
1981
|
helpInformation = deprecatedCallback(helpInformation);
|
|
1750
1982
|
if (typeof helpInformation !== 'string' && !Buffer.isBuffer(helpInformation)) {
|
|
1751
1983
|
throw new Error('outputHelp callback must return a string or a Buffer');
|
|
1752
1984
|
}
|
|
1753
1985
|
}
|
|
1754
|
-
|
|
1986
|
+
outputContext.write(helpInformation);
|
|
1755
1987
|
if (this._getHelpOption()?.long) {
|
|
1756
1988
|
this.emit(this._getHelpOption().long);
|
|
1757
1989
|
}
|
|
1758
|
-
this.emit('afterHelp',
|
|
1759
|
-
this._getCommandAndAncestors().forEach(command => command.emit('afterAllHelp',
|
|
1990
|
+
this.emit('afterHelp', eventContext);
|
|
1991
|
+
this._getCommandAndAncestors().forEach(command => command.emit('afterAllHelp', eventContext));
|
|
1760
1992
|
}
|
|
1761
1993
|
helpOption(flags, description) {
|
|
1762
1994
|
if (typeof flags === 'boolean') {
|
|
1763
1995
|
if (flags) {
|
|
1764
|
-
this._helpOption
|
|
1996
|
+
if (this._helpOption === null) this._helpOption = undefined;
|
|
1997
|
+
if (this._defaultOptionGroup) {
|
|
1998
|
+
this._initOptionGroup(this._getHelpOption());
|
|
1999
|
+
}
|
|
1765
2000
|
} else {
|
|
1766
2001
|
this._helpOption = null;
|
|
1767
2002
|
}
|
|
1768
2003
|
return this;
|
|
1769
2004
|
}
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
this._helpOption = this.createOption(flags, description);
|
|
2005
|
+
this._helpOption = this.createOption(flags ?? '-h, --help', description ?? 'display help for command');
|
|
2006
|
+
if (flags || description) this._initOptionGroup(this._helpOption);
|
|
1773
2007
|
return this;
|
|
1774
2008
|
}
|
|
1775
2009
|
_getHelpOption() {
|
|
@@ -1780,11 +2014,12 @@ function requireCommand() {
|
|
|
1780
2014
|
}
|
|
1781
2015
|
addHelpOption(option) {
|
|
1782
2016
|
this._helpOption = option;
|
|
2017
|
+
this._initOptionGroup(option);
|
|
1783
2018
|
return this;
|
|
1784
2019
|
}
|
|
1785
2020
|
help(contextOptions) {
|
|
1786
2021
|
this.outputHelp(contextOptions);
|
|
1787
|
-
let exitCode = process.exitCode
|
|
2022
|
+
let exitCode = Number(process.exitCode ?? 0);
|
|
1788
2023
|
if (exitCode === 0 && contextOptions && typeof contextOptions !== 'function' && contextOptions.error) {
|
|
1789
2024
|
exitCode = 1;
|
|
1790
2025
|
}
|
|
@@ -1850,7 +2085,13 @@ function requireCommand() {
|
|
|
1850
2085
|
return arg;
|
|
1851
2086
|
});
|
|
1852
2087
|
}
|
|
2088
|
+
function useColor() {
|
|
2089
|
+
if (process.env.NO_COLOR || process.env.FORCE_COLOR === '0' || process.env.FORCE_COLOR === 'false') return false;
|
|
2090
|
+
if (process.env.FORCE_COLOR || process.env.CLICOLOR_FORCE !== undefined) return true;
|
|
2091
|
+
return undefined;
|
|
2092
|
+
}
|
|
1853
2093
|
command.Command = Command;
|
|
2094
|
+
command.useColor = useColor;
|
|
1854
2095
|
return command;
|
|
1855
2096
|
}
|
|
1856
2097
|
|
|
@@ -12728,7 +12969,7 @@ function requireStat() {
|
|
|
12728
12969
|
return checkParentPathsSync(src, srcStat, destParent, funcName);
|
|
12729
12970
|
}
|
|
12730
12971
|
function areIdentical(srcStat, destStat) {
|
|
12731
|
-
return destStat.ino && destStat.dev && destStat.ino === srcStat.ino && destStat.dev === srcStat.dev;
|
|
12972
|
+
return destStat.ino !== undefined && destStat.dev !== undefined && destStat.ino === srcStat.ino && destStat.dev === srcStat.dev;
|
|
12732
12973
|
}
|
|
12733
12974
|
function isSrcSubdir(src, dest) {
|
|
12734
12975
|
const srcArr = path.resolve(src).split(path.sep).filter(i => i);
|
|
@@ -13431,12 +13672,12 @@ function requireUtils() {
|
|
|
13431
13672
|
return utils;
|
|
13432
13673
|
}
|
|
13433
13674
|
|
|
13434
|
-
var
|
|
13675
|
+
var jsonfile$1;
|
|
13435
13676
|
|
|
13436
13677
|
var hasRequiredJsonfile$1;
|
|
13437
13678
|
|
|
13438
13679
|
function requireJsonfile$1() {
|
|
13439
|
-
if (hasRequiredJsonfile$1) return
|
|
13680
|
+
if (hasRequiredJsonfile$1) return jsonfile$1;
|
|
13440
13681
|
hasRequiredJsonfile$1 = 1;
|
|
13441
13682
|
let _fs;
|
|
13442
13683
|
try {
|
|
@@ -13502,14 +13743,13 @@ function requireJsonfile$1() {
|
|
|
13502
13743
|
const str = stringify(obj, options);
|
|
13503
13744
|
return fs.writeFileSync(file, str, options);
|
|
13504
13745
|
}
|
|
13505
|
-
|
|
13746
|
+
jsonfile$1 = {
|
|
13506
13747
|
readFile: readFile,
|
|
13507
13748
|
readFileSync: readFileSync,
|
|
13508
13749
|
writeFile: writeFile,
|
|
13509
13750
|
writeFileSync: writeFileSync
|
|
13510
13751
|
};
|
|
13511
|
-
|
|
13512
|
-
return jsonfile_1;
|
|
13752
|
+
return jsonfile$1;
|
|
13513
13753
|
}
|
|
13514
13754
|
|
|
13515
13755
|
var jsonfile;
|
|
@@ -16908,6 +17148,8 @@ API.v500 = API.fromSimpleString('5.0.0');
|
|
|
16908
17148
|
|
|
16909
17149
|
API.v510 = API.fromSimpleString('5.1.0');
|
|
16910
17150
|
|
|
17151
|
+
API.v520 = API.fromSimpleString('5.2.0');
|
|
17152
|
+
|
|
16911
17153
|
API.v540 = API.fromSimpleString('5.4.0');
|
|
16912
17154
|
|
|
16913
17155
|
function equals$1(a, b, itemEquals = (a, b) => a === b) {
|
|
@@ -20453,30 +20695,41 @@ function provideQuickFix(response, client) {
|
|
|
20453
20695
|
}, mainExports$2.CodeActionKind.QuickFix));
|
|
20454
20696
|
}
|
|
20455
20697
|
|
|
20456
|
-
|
|
20457
|
-
|
|
20458
|
-
|
|
20459
|
-
|
|
20698
|
+
var CodeActionKind$1;
|
|
20699
|
+
|
|
20700
|
+
(function(CodeActionKind) {
|
|
20701
|
+
CodeActionKind.RefactorMove = 'refactor.move';
|
|
20702
|
+
})(CodeActionKind$1 || (CodeActionKind$1 = {}));
|
|
20703
|
+
|
|
20704
|
+
function provideRefactors(refactors, args, features) {
|
|
20460
20705
|
const actions = [];
|
|
20461
|
-
for (const
|
|
20462
|
-
if (
|
|
20463
|
-
actions.push(asSelectRefactoring(
|
|
20706
|
+
for (const refactor of refactors) {
|
|
20707
|
+
if (refactor.inlineable === false) {
|
|
20708
|
+
actions.push(asSelectRefactoring(refactor, args));
|
|
20464
20709
|
} else {
|
|
20465
|
-
const relevantActions =
|
|
20710
|
+
const relevantActions = refactor.actions.filter(action => {
|
|
20711
|
+
if (action.notApplicableReason && !features.codeActionDisabledSupport) {
|
|
20712
|
+
return false;
|
|
20713
|
+
}
|
|
20714
|
+
if (action.isInteractive && (!features.moveToFileCodeActionSupport || action.name !== 'Move to file')) {
|
|
20715
|
+
return false;
|
|
20716
|
+
}
|
|
20717
|
+
return true;
|
|
20718
|
+
});
|
|
20466
20719
|
for (const action of relevantActions) {
|
|
20467
|
-
actions.push(asApplyRefactoring(action,
|
|
20720
|
+
actions.push(asApplyRefactoring(action, refactor, args));
|
|
20468
20721
|
}
|
|
20469
20722
|
}
|
|
20470
20723
|
}
|
|
20471
20724
|
return actions;
|
|
20472
20725
|
}
|
|
20473
20726
|
|
|
20474
|
-
function asSelectRefactoring(
|
|
20475
|
-
return mainExports$2.CodeAction.create(
|
|
20727
|
+
function asSelectRefactoring(refactor, args) {
|
|
20728
|
+
return mainExports$2.CodeAction.create(refactor.description, mainExports$2.Command.create(refactor.description, Commands.SELECT_REFACTORING, refactor, args), mainExports$2.CodeActionKind.Refactor);
|
|
20476
20729
|
}
|
|
20477
20730
|
|
|
20478
|
-
function asApplyRefactoring(action,
|
|
20479
|
-
const codeAction = mainExports$2.CodeAction.create(action.description, asKind(
|
|
20731
|
+
function asApplyRefactoring(action, refactor, args) {
|
|
20732
|
+
const codeAction = mainExports$2.CodeAction.create(action.description, asKind(action));
|
|
20480
20733
|
if (action.notApplicableReason) {
|
|
20481
20734
|
codeAction.disabled = {
|
|
20482
20735
|
reason: action.notApplicableReason
|
|
@@ -20484,20 +20737,46 @@ function asApplyRefactoring(action, info, args) {
|
|
|
20484
20737
|
} else {
|
|
20485
20738
|
codeAction.command = mainExports$2.Command.create(action.description, Commands.APPLY_REFACTORING, {
|
|
20486
20739
|
...args,
|
|
20487
|
-
refactor:
|
|
20740
|
+
refactor: refactor.name,
|
|
20488
20741
|
action: action.name
|
|
20489
20742
|
});
|
|
20490
20743
|
}
|
|
20491
20744
|
return codeAction;
|
|
20492
20745
|
}
|
|
20493
20746
|
|
|
20494
|
-
function asKind(
|
|
20495
|
-
if (
|
|
20747
|
+
function asKind(action) {
|
|
20748
|
+
if (action.kind) {
|
|
20749
|
+
return action.kind;
|
|
20750
|
+
}
|
|
20751
|
+
if (action.name.startsWith('function_')) {
|
|
20496
20752
|
return `${mainExports$2.CodeActionKind.RefactorExtract}.function`;
|
|
20497
|
-
}
|
|
20753
|
+
}
|
|
20754
|
+
if (action.name.startsWith('constant_')) {
|
|
20498
20755
|
return `${mainExports$2.CodeActionKind.RefactorExtract}.constant`;
|
|
20499
|
-
}
|
|
20500
|
-
|
|
20756
|
+
}
|
|
20757
|
+
if (action.name.startsWith('Extract to type alias')) {
|
|
20758
|
+
return `${mainExports$2.CodeActionKind.RefactorExtract}.type`;
|
|
20759
|
+
}
|
|
20760
|
+
if (action.name.startsWith('Extract to interface')) {
|
|
20761
|
+
return `${mainExports$2.CodeActionKind.RefactorExtract}.interface`;
|
|
20762
|
+
}
|
|
20763
|
+
if (action.name.startsWith('Move to file')) {
|
|
20764
|
+
return `${CodeActionKind$1.RefactorMove}.file`;
|
|
20765
|
+
}
|
|
20766
|
+
if (action.name.startsWith('Move to a new file')) {
|
|
20767
|
+
return `${CodeActionKind$1.RefactorMove}.newFile`;
|
|
20768
|
+
}
|
|
20769
|
+
if (action.name.startsWith('Convert namespace import') || action.name.startsWith('Convert named imports')) {
|
|
20770
|
+
return `${mainExports$2.CodeActionKind.RefactorRewrite}.import`;
|
|
20771
|
+
}
|
|
20772
|
+
if (action.name.startsWith('Convert default export') || action.name.startsWith('Convert named export')) {
|
|
20773
|
+
return `${mainExports$2.CodeActionKind.RefactorRewrite}.export`;
|
|
20774
|
+
}
|
|
20775
|
+
if (action.name.startsWith('Convert parameters to destructured object')) {
|
|
20776
|
+
return `${mainExports$2.CodeActionKind.RefactorRewrite}.parameters.toDestructured`;
|
|
20777
|
+
}
|
|
20778
|
+
if (action.name.startsWith('Generate \'get\' and \'set\' accessors')) {
|
|
20779
|
+
return `${mainExports$2.CodeActionKind.RefactorRewrite}.property.generateAccessors`;
|
|
20501
20780
|
}
|
|
20502
20781
|
return mainExports$2.CodeActionKind.Refactor;
|
|
20503
20782
|
}
|
|
@@ -21998,85 +22277,31 @@ var libExports = requireLib();
|
|
|
21998
22277
|
|
|
21999
22278
|
const which = getDefaultExportFromCjs(libExports);
|
|
22000
22279
|
|
|
22001
|
-
const
|
|
22002
|
-
directory: 'isDirectory',
|
|
22003
|
-
file: 'isFile'
|
|
22004
|
-
};
|
|
22005
|
-
|
|
22006
|
-
function checkType(type) {
|
|
22007
|
-
if (Object.hasOwnProperty.call(typeMappings, type)) {
|
|
22008
|
-
return;
|
|
22009
|
-
}
|
|
22010
|
-
throw new Error(`Invalid type specified: ${type}`);
|
|
22011
|
-
}
|
|
22012
|
-
|
|
22013
|
-
const matchType = (type, stat) => stat[typeMappings[type]]();
|
|
22014
|
-
|
|
22015
|
-
const toPath$1 = urlOrPath => urlOrPath instanceof URL ? fileURLToPath(urlOrPath) : urlOrPath;
|
|
22280
|
+
const toPath = urlOrPath => urlOrPath instanceof URL ? fileURLToPath(urlOrPath) : urlOrPath;
|
|
22016
22281
|
|
|
22017
|
-
function
|
|
22018
|
-
|
|
22019
|
-
|
|
22020
|
-
|
|
22021
|
-
|
|
22282
|
+
function findUpSync(name, {cwd: cwd = process$1.cwd(), type: type = 'file', stopAt: stopAt} = {}) {
|
|
22283
|
+
let directory = path__default.resolve(toPath(cwd) ?? '');
|
|
22284
|
+
const {root: root} = path__default.parse(directory);
|
|
22285
|
+
stopAt = path__default.resolve(directory, toPath(stopAt) ?? root);
|
|
22286
|
+
const isAbsoluteName = path__default.isAbsolute(name);
|
|
22287
|
+
while (directory) {
|
|
22288
|
+
const filePath = isAbsoluteName ? name : path__default.join(directory, name);
|
|
22022
22289
|
try {
|
|
22023
|
-
const
|
|
22290
|
+
const stats = fs$2.statSync(filePath, {
|
|
22024
22291
|
throwIfNoEntry: false
|
|
22025
22292
|
});
|
|
22026
|
-
if (
|
|
22027
|
-
|
|
22028
|
-
}
|
|
22029
|
-
if (matchType(type, stat)) {
|
|
22030
|
-
return path_;
|
|
22293
|
+
if (type === 'file' && stats?.isFile() || type === 'directory' && stats?.isDirectory()) {
|
|
22294
|
+
return filePath;
|
|
22031
22295
|
}
|
|
22032
22296
|
} catch {}
|
|
22033
|
-
|
|
22034
|
-
}
|
|
22035
|
-
|
|
22036
|
-
const toPath = urlOrPath => urlOrPath instanceof URL ? fileURLToPath(urlOrPath) : urlOrPath;
|
|
22037
|
-
|
|
22038
|
-
const findUpStop = Symbol('findUpStop');
|
|
22039
|
-
|
|
22040
|
-
function findUpMultipleSync(name, options = {}) {
|
|
22041
|
-
let directory = path__default.resolve(toPath(options.cwd) || '');
|
|
22042
|
-
const {root: root} = path__default.parse(directory);
|
|
22043
|
-
const stopAt = options.stopAt || root;
|
|
22044
|
-
const limit = options.limit || Number.POSITIVE_INFINITY;
|
|
22045
|
-
const paths = [ name ].flat();
|
|
22046
|
-
const runMatcher = locateOptions => {
|
|
22047
|
-
{
|
|
22048
|
-
return locatePathSync(paths, locateOptions);
|
|
22049
|
-
}
|
|
22050
|
-
};
|
|
22051
|
-
const matches = [];
|
|
22052
|
-
while (true) {
|
|
22053
|
-
const foundPath = runMatcher({
|
|
22054
|
-
...options,
|
|
22055
|
-
cwd: directory
|
|
22056
|
-
});
|
|
22057
|
-
if (foundPath === findUpStop) {
|
|
22058
|
-
break;
|
|
22059
|
-
}
|
|
22060
|
-
if (foundPath) {
|
|
22061
|
-
matches.push(path__default.resolve(directory, foundPath));
|
|
22062
|
-
}
|
|
22063
|
-
if (directory === stopAt || matches.length >= limit) {
|
|
22297
|
+
if (directory === stopAt || directory === root) {
|
|
22064
22298
|
break;
|
|
22065
22299
|
}
|
|
22066
22300
|
directory = path__default.dirname(directory);
|
|
22067
22301
|
}
|
|
22068
|
-
return matches;
|
|
22069
|
-
}
|
|
22070
|
-
|
|
22071
|
-
function findUpSync(name, options = {}) {
|
|
22072
|
-
const matches = findUpMultipleSync(name, {
|
|
22073
|
-
...options,
|
|
22074
|
-
limit: 1
|
|
22075
|
-
});
|
|
22076
|
-
return matches[0];
|
|
22077
22302
|
}
|
|
22078
22303
|
|
|
22079
|
-
function
|
|
22304
|
+
function packageUpSync({cwd: cwd = process$1.cwd()} = {}) {
|
|
22080
22305
|
return findUpSync('package.json', {
|
|
22081
22306
|
cwd: cwd
|
|
22082
22307
|
});
|
|
@@ -22203,7 +22428,7 @@ class TypeScriptVersionProvider {
|
|
|
22203
22428
|
this.logger.log(`Resolved directory path from a file path: ${resolvedPath}`);
|
|
22204
22429
|
}
|
|
22205
22430
|
try {
|
|
22206
|
-
const packageJsonPath =
|
|
22431
|
+
const packageJsonPath = packageUpSync({
|
|
22207
22432
|
cwd: resolvedPath
|
|
22208
22433
|
});
|
|
22209
22434
|
this.logger.log(`Resolved package.json location: "${packageJsonPath}"`);
|
|
@@ -22287,6 +22512,7 @@ class LspServer {
|
|
|
22287
22512
|
}
|
|
22288
22513
|
this.fileConfigurationManager.mergeTsPreferences(userInitializationOptions.preferences || {});
|
|
22289
22514
|
this.features.completionDisableFilterText = userInitializationOptions.completionDisableFilterText ?? false;
|
|
22515
|
+
this.features.moveToFileCodeActionSupport = userInitializationOptions.supportsMoveToFileCodeAction && typescriptVersion.version?.gte(API.v520);
|
|
22290
22516
|
const {textDocument: textDocument} = clientCapabilities;
|
|
22291
22517
|
if (textDocument) {
|
|
22292
22518
|
const {codeAction: codeAction, completion: completion, definition: definition, publishDiagnostics: publishDiagnostics} = textDocument;
|
|
@@ -22826,11 +23052,11 @@ class LspServer {
|
|
|
22826
23052
|
const fileRangeArgs = Range.toFileRangeRequestArgs(document.filepath, params.range);
|
|
22827
23053
|
const actions = [];
|
|
22828
23054
|
const kinds = params.context.only?.map(kind => new CodeActionKind(kind));
|
|
22829
|
-
if (!kinds || kinds.some(kind =>
|
|
23055
|
+
if (!kinds || kinds.some(kind => CodeActionKind.QuickFix.contains(kind))) {
|
|
22830
23056
|
actions.push(...provideQuickFix(await this.getCodeFixes(fileRangeArgs, params.context, token), this.tsClient));
|
|
22831
23057
|
}
|
|
22832
|
-
if (!kinds || kinds.some(kind =>
|
|
22833
|
-
actions.push(...provideRefactors(await this.getRefactors(fileRangeArgs, params.context, token), fileRangeArgs, this.features));
|
|
23058
|
+
if (!kinds || kinds.some(kind => CodeActionKind.Refactor.contains(kind))) {
|
|
23059
|
+
actions.push(...provideRefactors(await this.getRefactors(fileRangeArgs, params.context, this.features, token), fileRangeArgs, this.features));
|
|
22834
23060
|
}
|
|
22835
23061
|
for (const kind of kinds || []) {
|
|
22836
23062
|
for (const command of organizeImportsCommands) {
|
|
@@ -22875,14 +23101,19 @@ class LspServer {
|
|
|
22875
23101
|
const response = await this.tsClient.execute(CommandTypes.GetCodeFixes, args, token);
|
|
22876
23102
|
return response.type === 'response' ? response : undefined;
|
|
22877
23103
|
}
|
|
22878
|
-
async getRefactors(fileRangeArgs, context, token) {
|
|
22879
|
-
const
|
|
22880
|
-
|
|
22881
|
-
|
|
22882
|
-
|
|
22883
|
-
|
|
22884
|
-
|
|
22885
|
-
|
|
23104
|
+
async getRefactors(fileRangeArgs, context, features, token) {
|
|
23105
|
+
const kinds = context.only || [ undefined ];
|
|
23106
|
+
const responses = await Promise.all(kinds.map(async kind => {
|
|
23107
|
+
const args = {
|
|
23108
|
+
...fileRangeArgs,
|
|
23109
|
+
triggerReason: context.triggerKind === mainExports$2.CodeActionTriggerKind.Invoked ? 'invoked' : undefined,
|
|
23110
|
+
kind: kind,
|
|
23111
|
+
includeInteractiveActions: features.moveToFileCodeActionSupport
|
|
23112
|
+
};
|
|
23113
|
+
const response = await this.tsClient.execute(CommandTypes.GetApplicableRefactors, args, token);
|
|
23114
|
+
return response.type === 'response' && response.body ? response.body : [];
|
|
23115
|
+
}));
|
|
23116
|
+
return responses.flat();
|
|
22886
23117
|
}
|
|
22887
23118
|
async executeCommand(params, token, workDoneProgress) {
|
|
22888
23119
|
if (params.command === Commands.APPLY_WORKSPACE_EDIT && params.arguments) {
|
|
@@ -22909,6 +23140,9 @@ class LspServer {
|
|
|
22909
23140
|
return;
|
|
22910
23141
|
}
|
|
22911
23142
|
const {body: body} = response;
|
|
23143
|
+
if (body?.notApplicableReason) {
|
|
23144
|
+
throw new Error(body.notApplicableReason);
|
|
23145
|
+
}
|
|
22912
23146
|
if (!body?.edits.length) {
|
|
22913
23147
|
return;
|
|
22914
23148
|
}
|