js-style-kit 0.4.0 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/bin/index.js CHANGED
@@ -475,7 +475,11 @@ var require_help = __commonJS({
475
475
  extraInfo.push(`env: ${option.envVar}`);
476
476
  }
477
477
  if (extraInfo.length > 0) {
478
- return `${option.description} (${extraInfo.join(", ")})`;
478
+ const extraDescription = `(${extraInfo.join(", ")})`;
479
+ if (option.description) {
480
+ return `${option.description} ${extraDescription}`;
481
+ }
482
+ return extraDescription;
479
483
  }
480
484
  return option.description;
481
485
  }
@@ -507,6 +511,41 @@ var require_help = __commonJS({
507
511
  }
508
512
  return argument.description;
509
513
  }
514
+ /**
515
+ * Format a list of items, given a heading and an array of formatted items.
516
+ *
517
+ * @param {string} heading
518
+ * @param {string[]} items
519
+ * @param {Help} helper
520
+ * @returns string[]
521
+ */
522
+ formatItemList(heading, items, helper) {
523
+ if (items.length === 0) return [];
524
+ return [helper.styleTitle(heading), ...items, ""];
525
+ }
526
+ /**
527
+ * Group items by their help group heading.
528
+ *
529
+ * @param {Command[] | Option[]} unsortedItems
530
+ * @param {Command[] | Option[]} visibleItems
531
+ * @param {Function} getGroup
532
+ * @returns {Map<string, Command[] | Option[]>}
533
+ */
534
+ groupItems(unsortedItems, visibleItems, getGroup) {
535
+ const result = /* @__PURE__ */ new Map();
536
+ unsortedItems.forEach((item) => {
537
+ const group = getGroup(item);
538
+ if (!result.has(group)) result.set(group, []);
539
+ });
540
+ visibleItems.forEach((item) => {
541
+ const group = getGroup(item);
542
+ if (!result.has(group)) {
543
+ result.set(group, []);
544
+ }
545
+ result.get(group).push(item);
546
+ });
547
+ return result;
548
+ }
510
549
  /**
511
550
  * Generate the built-in help text.
512
551
  *
@@ -540,26 +579,23 @@ var require_help = __commonJS({
540
579
  helper.styleArgumentDescription(helper.argumentDescription(argument))
541
580
  );
542
581
  });
543
- if (argumentList.length > 0) {
544
- output = output.concat([
545
- helper.styleTitle("Arguments:"),
546
- ...argumentList,
547
- ""
548
- ]);
549
- }
550
- const optionList = helper.visibleOptions(cmd).map((option) => {
551
- return callFormatItem(
552
- helper.styleOptionTerm(helper.optionTerm(option)),
553
- helper.styleOptionDescription(helper.optionDescription(option))
554
- );
582
+ output = output.concat(
583
+ this.formatItemList("Arguments:", argumentList, helper)
584
+ );
585
+ const optionGroups = this.groupItems(
586
+ cmd.options,
587
+ helper.visibleOptions(cmd),
588
+ (option) => option.helpGroupHeading ?? "Options:"
589
+ );
590
+ optionGroups.forEach((options, group) => {
591
+ const optionList = options.map((option) => {
592
+ return callFormatItem(
593
+ helper.styleOptionTerm(helper.optionTerm(option)),
594
+ helper.styleOptionDescription(helper.optionDescription(option))
595
+ );
596
+ });
597
+ output = output.concat(this.formatItemList(group, optionList, helper));
555
598
  });
556
- if (optionList.length > 0) {
557
- output = output.concat([
558
- helper.styleTitle("Options:"),
559
- ...optionList,
560
- ""
561
- ]);
562
- }
563
599
  if (helper.showGlobalOptions) {
564
600
  const globalOptionList = helper.visibleGlobalOptions(cmd).map((option) => {
565
601
  return callFormatItem(
@@ -567,27 +603,24 @@ var require_help = __commonJS({
567
603
  helper.styleOptionDescription(helper.optionDescription(option))
568
604
  );
569
605
  });
570
- if (globalOptionList.length > 0) {
571
- output = output.concat([
572
- helper.styleTitle("Global Options:"),
573
- ...globalOptionList,
574
- ""
575
- ]);
576
- }
577
- }
578
- const commandList = helper.visibleCommands(cmd).map((cmd2) => {
579
- return callFormatItem(
580
- helper.styleSubcommandTerm(helper.subcommandTerm(cmd2)),
581
- helper.styleSubcommandDescription(helper.subcommandDescription(cmd2))
606
+ output = output.concat(
607
+ this.formatItemList("Global Options:", globalOptionList, helper)
582
608
  );
583
- });
584
- if (commandList.length > 0) {
585
- output = output.concat([
586
- helper.styleTitle("Commands:"),
587
- ...commandList,
588
- ""
589
- ]);
590
609
  }
610
+ const commandGroups = this.groupItems(
611
+ cmd.commands,
612
+ helper.visibleCommands(cmd),
613
+ (sub) => sub.helpGroup() || "Commands:"
614
+ );
615
+ commandGroups.forEach((commands, group) => {
616
+ const commandList = commands.map((sub) => {
617
+ return callFormatItem(
618
+ helper.styleSubcommandTerm(helper.subcommandTerm(sub)),
619
+ helper.styleSubcommandDescription(helper.subcommandDescription(sub))
620
+ );
621
+ });
622
+ output = output.concat(this.formatItemList(group, commandList, helper));
623
+ });
591
624
  return output.join("\n");
592
625
  }
593
626
  /**
@@ -800,6 +833,7 @@ var require_option = __commonJS({
800
833
  this.argChoices = void 0;
801
834
  this.conflictsWith = [];
802
835
  this.implied = void 0;
836
+ this.helpGroupHeading = void 0;
803
837
  }
804
838
  /**
805
839
  * Set the default value, and optionally supply the description to be displayed in the help.
@@ -960,6 +994,16 @@ var require_option = __commonJS({
960
994
  }
961
995
  return camelcase(this.name());
962
996
  }
997
+ /**
998
+ * Set the help group heading.
999
+ *
1000
+ * @param {string} heading
1001
+ * @return {Option}
1002
+ */
1003
+ helpGroup(heading) {
1004
+ this.helpGroupHeading = heading;
1005
+ return this;
1006
+ }
963
1007
  /**
964
1008
  * Check if `arg` matches the short or long flag.
965
1009
  *
@@ -1152,11 +1196,11 @@ var require_suggestSimilar = __commonJS({
1152
1196
  var require_command = __commonJS({
1153
1197
  "../../node_modules/commander/lib/command.js"(exports) {
1154
1198
  "use strict";
1155
- var EventEmitter = __require("node:events").EventEmitter;
1156
- var childProcess = __require("node:child_process");
1157
- var path2 = __require("node:path");
1158
- var fs2 = __require("node:fs");
1159
- var process2 = __require("node:process");
1199
+ var EventEmitter = __require("events").EventEmitter;
1200
+ var childProcess = __require("child_process");
1201
+ var path2 = __require("path");
1202
+ var fs2 = __require("fs");
1203
+ var process2 = __require("process");
1160
1204
  var { Argument: Argument2, humanReadableArgName } = require_argument();
1161
1205
  var { CommanderError: CommanderError2 } = require_error();
1162
1206
  var { Help: Help2, stripColor } = require_help();
@@ -1217,6 +1261,9 @@ var require_command = __commonJS({
1217
1261
  this._addImplicitHelpCommand = void 0;
1218
1262
  this._helpCommand = void 0;
1219
1263
  this._helpConfiguration = {};
1264
+ this._helpGroupHeading = void 0;
1265
+ this._defaultCommandGroup = void 0;
1266
+ this._defaultOptionGroup = void 0;
1220
1267
  }
1221
1268
  /**
1222
1269
  * Copy settings that are useful to have in common across root command and subcommands.
@@ -1356,7 +1403,11 @@ var require_command = __commonJS({
1356
1403
  */
1357
1404
  configureOutput(configuration) {
1358
1405
  if (configuration === void 0) return this._outputConfiguration;
1359
- Object.assign(this._outputConfiguration, configuration);
1406
+ this._outputConfiguration = Object.assign(
1407
+ {},
1408
+ this._outputConfiguration,
1409
+ configuration
1410
+ );
1360
1411
  return this;
1361
1412
  }
1362
1413
  /**
@@ -1427,16 +1478,16 @@ var require_command = __commonJS({
1427
1478
  *
1428
1479
  * @param {string} name
1429
1480
  * @param {string} [description]
1430
- * @param {(Function|*)} [fn] - custom argument processing function
1481
+ * @param {(Function|*)} [parseArg] - custom argument processing function or default value
1431
1482
  * @param {*} [defaultValue]
1432
1483
  * @return {Command} `this` command for chaining
1433
1484
  */
1434
- argument(name, description, fn, defaultValue) {
1485
+ argument(name, description, parseArg, defaultValue) {
1435
1486
  const argument = this.createArgument(name, description);
1436
- if (typeof fn === "function") {
1437
- argument.default(defaultValue).argParser(fn);
1487
+ if (typeof parseArg === "function") {
1488
+ argument.default(defaultValue).argParser(parseArg);
1438
1489
  } else {
1439
- argument.default(fn);
1490
+ argument.default(parseArg);
1440
1491
  }
1441
1492
  this.addArgument(argument);
1442
1493
  return this;
@@ -1495,10 +1546,13 @@ var require_command = __commonJS({
1495
1546
  helpCommand(enableOrNameAndArgs, description) {
1496
1547
  if (typeof enableOrNameAndArgs === "boolean") {
1497
1548
  this._addImplicitHelpCommand = enableOrNameAndArgs;
1549
+ if (enableOrNameAndArgs && this._defaultCommandGroup) {
1550
+ this._initCommandGroup(this._getHelpCommand());
1551
+ }
1498
1552
  return this;
1499
1553
  }
1500
- enableOrNameAndArgs = enableOrNameAndArgs ?? "help [command]";
1501
- const [, helpName, helpArgs] = enableOrNameAndArgs.match(/([^ ]+) *(.*)/);
1554
+ const nameAndArgs = enableOrNameAndArgs ?? "help [command]";
1555
+ const [, helpName, helpArgs] = nameAndArgs.match(/([^ ]+) *(.*)/);
1502
1556
  const helpDescription = description ?? "display help for command";
1503
1557
  const helpCommand = this.createCommand(helpName);
1504
1558
  helpCommand.helpOption(false);
@@ -1506,6 +1560,7 @@ var require_command = __commonJS({
1506
1560
  if (helpDescription) helpCommand.description(helpDescription);
1507
1561
  this._addImplicitHelpCommand = true;
1508
1562
  this._helpCommand = helpCommand;
1563
+ if (enableOrNameAndArgs || description) this._initCommandGroup(helpCommand);
1509
1564
  return this;
1510
1565
  }
1511
1566
  /**
@@ -1522,6 +1577,7 @@ var require_command = __commonJS({
1522
1577
  }
1523
1578
  this._addImplicitHelpCommand = true;
1524
1579
  this._helpCommand = helpCommand;
1580
+ this._initCommandGroup(helpCommand);
1525
1581
  return this;
1526
1582
  }
1527
1583
  /**
@@ -1670,6 +1726,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
1670
1726
  throw new Error(`Cannot add option '${option.flags}'${this._name && ` to command '${this._name}'`} due to conflicting flag '${matchingFlag}'
1671
1727
  - already used by option '${matchingOption.flags}'`);
1672
1728
  }
1729
+ this._initOptionGroup(option);
1673
1730
  this.options.push(option);
1674
1731
  }
1675
1732
  /**
@@ -1693,6 +1750,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
1693
1750
  `cannot add command '${newCmd}' as already have command '${existingCmd}'`
1694
1751
  );
1695
1752
  }
1753
+ this._initCommandGroup(command);
1696
1754
  this.commands.push(command);
1697
1755
  }
1698
1756
  /**
@@ -2604,6 +2662,12 @@ Expecting one of '${allowedValues.join("', '")}'`);
2604
2662
  function maybeOption(arg) {
2605
2663
  return arg.length > 1 && arg[0] === "-";
2606
2664
  }
2665
+ const negativeNumberArg = (arg) => {
2666
+ if (!/^-\d*\.?\d+(e[+-]?\d+)?$/.test(arg)) return false;
2667
+ return !this._getCommandAndAncestors().some(
2668
+ (cmd) => cmd.options.map((opt) => opt.short).some((short) => /^-\d$/.test(short))
2669
+ );
2670
+ };
2607
2671
  let activeVariadicOption = null;
2608
2672
  while (args.length) {
2609
2673
  const arg = args.shift();
@@ -2612,7 +2676,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
2612
2676
  dest.push(...args);
2613
2677
  break;
2614
2678
  }
2615
- if (activeVariadicOption && !maybeOption(arg)) {
2679
+ if (activeVariadicOption && (!maybeOption(arg) || negativeNumberArg(arg))) {
2616
2680
  this.emit(`option:${activeVariadicOption.name()}`, arg);
2617
2681
  continue;
2618
2682
  }
@@ -2626,7 +2690,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
2626
2690
  this.emit(`option:${option.name()}`, value);
2627
2691
  } else if (option.optional) {
2628
2692
  let value = null;
2629
- if (args.length > 0 && !maybeOption(args[0])) {
2693
+ if (args.length > 0 && (!maybeOption(args[0]) || negativeNumberArg(args[0]))) {
2630
2694
  value = args.shift();
2631
2695
  }
2632
2696
  this.emit(`option:${option.name()}`, value);
@@ -2657,7 +2721,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
2657
2721
  continue;
2658
2722
  }
2659
2723
  }
2660
- if (maybeOption(arg)) {
2724
+ if (dest === operands && maybeOption(arg) && !(this.commands.length === 0 && negativeNumberArg(arg))) {
2661
2725
  dest = unknown;
2662
2726
  }
2663
2727
  if ((this._enablePositionalOptions || this._passThroughOptions) && operands.length === 0 && unknown.length === 0) {
@@ -3029,6 +3093,69 @@ Expecting one of '${allowedValues.join("', '")}'`);
3029
3093
  this._name = str;
3030
3094
  return this;
3031
3095
  }
3096
+ /**
3097
+ * Set/get the help group heading for this subcommand in parent command's help.
3098
+ *
3099
+ * @param {string} [heading]
3100
+ * @return {Command | string}
3101
+ */
3102
+ helpGroup(heading) {
3103
+ if (heading === void 0) return this._helpGroupHeading ?? "";
3104
+ this._helpGroupHeading = heading;
3105
+ return this;
3106
+ }
3107
+ /**
3108
+ * Set/get the default help group heading for subcommands added to this command.
3109
+ * (This does not override a group set directly on the subcommand using .helpGroup().)
3110
+ *
3111
+ * @example
3112
+ * program.commandsGroup('Development Commands:);
3113
+ * program.command('watch')...
3114
+ * program.command('lint')...
3115
+ * ...
3116
+ *
3117
+ * @param {string} [heading]
3118
+ * @returns {Command | string}
3119
+ */
3120
+ commandsGroup(heading) {
3121
+ if (heading === void 0) return this._defaultCommandGroup ?? "";
3122
+ this._defaultCommandGroup = heading;
3123
+ return this;
3124
+ }
3125
+ /**
3126
+ * Set/get the default help group heading for options added to this command.
3127
+ * (This does not override a group set directly on the option using .helpGroup().)
3128
+ *
3129
+ * @example
3130
+ * program
3131
+ * .optionsGroup('Development Options:')
3132
+ * .option('-d, --debug', 'output extra debugging')
3133
+ * .option('-p, --profile', 'output profiling information')
3134
+ *
3135
+ * @param {string} [heading]
3136
+ * @returns {Command | string}
3137
+ */
3138
+ optionsGroup(heading) {
3139
+ if (heading === void 0) return this._defaultOptionGroup ?? "";
3140
+ this._defaultOptionGroup = heading;
3141
+ return this;
3142
+ }
3143
+ /**
3144
+ * @param {Option} option
3145
+ * @private
3146
+ */
3147
+ _initOptionGroup(option) {
3148
+ if (this._defaultOptionGroup && !option.helpGroupHeading)
3149
+ option.helpGroup(this._defaultOptionGroup);
3150
+ }
3151
+ /**
3152
+ * @param {Command} cmd
3153
+ * @private
3154
+ */
3155
+ _initCommandGroup(cmd) {
3156
+ if (this._defaultCommandGroup && !cmd.helpGroup())
3157
+ cmd.helpGroup(this._defaultCommandGroup);
3158
+ }
3032
3159
  /**
3033
3160
  * Set the name of the command from script filename, such as process.argv[1],
3034
3161
  * or require.main.filename, or __filename.
@@ -3163,15 +3290,20 @@ Expecting one of '${allowedValues.join("', '")}'`);
3163
3290
  helpOption(flags, description) {
3164
3291
  if (typeof flags === "boolean") {
3165
3292
  if (flags) {
3166
- this._helpOption = this._helpOption ?? void 0;
3293
+ if (this._helpOption === null) this._helpOption = void 0;
3294
+ if (this._defaultOptionGroup) {
3295
+ this._initOptionGroup(this._getHelpOption());
3296
+ }
3167
3297
  } else {
3168
3298
  this._helpOption = null;
3169
3299
  }
3170
3300
  return this;
3171
3301
  }
3172
- flags = flags ?? "-h, --help";
3173
- description = description ?? "display help for command";
3174
- this._helpOption = this.createOption(flags, description);
3302
+ this._helpOption = this.createOption(
3303
+ flags ?? "-h, --help",
3304
+ description ?? "display help for command"
3305
+ );
3306
+ if (flags || description) this._initOptionGroup(this._helpOption);
3175
3307
  return this;
3176
3308
  }
3177
3309
  /**
@@ -3196,6 +3328,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
3196
3328
  */
3197
3329
  addHelpOption(option) {
3198
3330
  this._helpOption = option;
3331
+ this._initOptionGroup(option);
3199
3332
  return this;
3200
3333
  }
3201
3334
  /**
@@ -3349,9 +3482,9 @@ var {
3349
3482
  } = import_index.default;
3350
3483
 
3351
3484
  // bin/index.ts
3352
- import { execSync } from "node:child_process";
3353
- import fs from "node:fs";
3354
- import path from "node:path";
3485
+ import { execSync } from "child_process";
3486
+ import fs from "fs";
3487
+ import path from "path";
3355
3488
  var program2 = new Command();
3356
3489
  program2.name("js-style-kit").description(
3357
3490
  "Initialize JavaScript/TypeScript project with style configurations"