breadc 0.8.3 → 0.8.5

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/index.cjs CHANGED
@@ -53,6 +53,13 @@ function makePluginContainer(plugins = []) {
53
53
  }
54
54
  };
55
55
  return {
56
+ init(breadc, allCommands, globalOptions) {
57
+ if (plugins.length === 0)
58
+ return;
59
+ for (const p of plugins) {
60
+ p.onInit?.(breadc, allCommands, globalOptions);
61
+ }
62
+ },
56
63
  async preRun(breadc) {
57
64
  if (plugins.length === 0)
58
65
  return;
@@ -547,7 +554,7 @@ function makeVersionCommand(name, config) {
547
554
  };
548
555
  return option;
549
556
  }
550
- function makeHelpCommand(name, config) {
557
+ function makeHelpCommand(name, config, allCommands) {
551
558
  function expandMessage(message) {
552
559
  const result = [];
553
560
  for (const row of message) {
@@ -569,6 +576,7 @@ function makeHelpCommand(name, config) {
569
576
  }
570
577
  function expandCommands(cursor) {
571
578
  const visited = /* @__PURE__ */ new WeakSet();
579
+ const added = /* @__PURE__ */ new WeakSet();
572
580
  const commands = cursor.command ? [cursor.command] : [];
573
581
  const q = [cursor];
574
582
  visited.add(cursor);
@@ -577,7 +585,8 @@ function makeHelpCommand(name, config) {
577
585
  for (const [_key, cmd] of cur.children) {
578
586
  if (!visited.has(cmd)) {
579
587
  visited.add(cmd);
580
- if (cmd.command) {
588
+ if (cmd.command && !added.has(cmd.command)) {
589
+ added.add(cmd.command);
581
590
  commands.push(cmd.command);
582
591
  }
583
592
  q.push(cmd);
@@ -590,6 +599,7 @@ function makeHelpCommand(name, config) {
590
599
  async callback(parsed) {
591
600
  const context = parsed.options.__context__;
592
601
  const cursor = parsed.options.__cursor__;
602
+ const usage = allCommands.length === 0 ? `[OPTIONS]` : allCommands.length === 1 ? `[OPTIONS] ${allCommands[0].format}` : allCommands.some((c) => c._default) ? `[OPTIONS] [COMMAND]` : `[OPTIONS] <COMMAND>`;
593
603
  const output = [
594
604
  `${name}/${config.version ? config.version : "unknown"}`,
595
605
  () => {
@@ -599,6 +609,8 @@ function makeHelpCommand(name, config) {
599
609
  return void 0;
600
610
  }
601
611
  },
612
+ "",
613
+ `${color.bold(color.underline("Usage:"))} ${color.bold(name)} ${usage}`,
602
614
  () => {
603
615
  const cmds = expandCommands(cursor);
604
616
  if (cmds.length > 0) {
@@ -662,7 +674,11 @@ function makeHelpCommand(name, config) {
662
674
 
663
675
  function breadc(name, config = {}) {
664
676
  let defaultCommand = void 0;
665
- const globalOptions = [];
677
+ const allCommands = [];
678
+ const globalOptions = [
679
+ makeHelpCommand(name, config, allCommands),
680
+ makeVersionCommand(name, config)
681
+ ];
666
682
  const container = makePluginContainer(config.plugins);
667
683
  const root = makeTreeNode({
668
684
  init(context) {
@@ -670,15 +686,13 @@ function breadc(name, config = {}) {
670
686
  if (defaultCommand) {
671
687
  initContextOptions(defaultCommand._options, context);
672
688
  }
673
- initContextOptions(
674
- [makeHelpCommand(name, config), makeVersionCommand(name, config)],
675
- context
676
- );
677
689
  },
678
690
  finish() {
679
691
  }
680
692
  });
681
693
  const breadc2 = {
694
+ name,
695
+ description: config.description ?? "",
682
696
  option(format, _config, _config2 = {}) {
683
697
  const config2 = typeof _config === "string" ? { description: _config, ..._config2 } : _config;
684
698
  const option = makeOption(format, config2);
@@ -691,6 +705,7 @@ function breadc(name, config = {}) {
691
705
  if (command._default) {
692
706
  defaultCommand = command;
693
707
  }
708
+ allCommands.push(command);
694
709
  return command;
695
710
  },
696
711
  parse(args) {
@@ -711,6 +726,7 @@ function breadc(name, config = {}) {
711
726
  return void 0;
712
727
  }
713
728
  };
729
+ container.init(breadc2, allCommands, globalOptions);
714
730
  return breadc2;
715
731
  }
716
732
 
@@ -719,3 +735,4 @@ exports.ParseError = ParseError;
719
735
  exports.breadc = breadc;
720
736
  exports.default = breadc;
721
737
  exports.definePlugin = definePlugin;
738
+ exports.makeTreeNode = makeTreeNode;
package/dist/index.d.ts CHANGED
@@ -47,6 +47,8 @@ interface TreeNode {
47
47
  finish(context: Context): void;
48
48
  }
49
49
 
50
+ declare function makeTreeNode(pnode: Partial<TreeNode>): TreeNode;
51
+
50
52
  type Lowercase = 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h' | 'i' | 'j' | 'k' | 'l' | 'm' | 'n' | 'o' | 'p' | 'q' | 'r' | 's' | 't' | 'u' | 'v' | 'w' | 'x' | 'y' | 'z';
51
53
  type Uppercase = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z';
52
54
  type Letter = Lowercase | Uppercase;
@@ -91,6 +93,8 @@ interface AppOption {
91
93
  plugins?: Partial<Plugin>[];
92
94
  }
93
95
  interface Breadc<GlobalOption extends object = {}> {
96
+ name: string;
97
+ description: string;
94
98
  option<F extends string = string, T extends string | boolean = ExtractOptionType<F>, R extends any = ExtractOptionType<F>>(format: F, description?: string, option?: OptionOption<T, R>): Breadc<GlobalOption & ExtractOption<F, R>>;
95
99
  option<F extends string = string, T extends string | boolean = ExtractOptionType<F>, R extends any = ExtractOptionType<F>>(format: F, option?: OptionOption<T, R>): Breadc<GlobalOption & ExtractOption<F, R>>;
96
100
  command<F extends string = string>(format: F, description?: string): Command<F, ExtractCommand<F>, {}, GlobalOption>;
@@ -137,10 +141,11 @@ interface OptionOption<T extends string | boolean, R extends any = T> {
137
141
  }
138
142
  type CommandHookFn = (result: ParseResult) => void | Promise<void>;
139
143
  interface Plugin {
140
- onPreRun(breadc: Breadc): void | Promise<void>;
141
- onPreCommand: Record<string, CommandHookFn> | CommandHookFn;
142
- onPostCommand: Record<string, CommandHookFn> | CommandHookFn;
143
- onPostRun(breadc: Breadc): void | Promise<void>;
144
+ onInit?(breadc: Breadc, allCommands: Command[], globalOptions: Option[]): void;
145
+ onPreRun?(breadc: Breadc): void | Promise<void>;
146
+ onPreCommand?: Record<string, CommandHookFn> | CommandHookFn;
147
+ onPostCommand?: Record<string, CommandHookFn> | CommandHookFn;
148
+ onPostRun?(breadc: Breadc): void | Promise<void>;
144
149
  }
145
150
 
146
151
  declare function breadc(name: string, config?: AppOption): Breadc<{}>;
@@ -152,4 +157,4 @@ declare class BreadcError extends Error {
152
157
  declare class ParseError extends Error {
153
158
  }
154
159
 
155
- export { AppOption, Argument, Breadc, BreadcError, Command, Option, ParseError, Plugin, breadc, breadc as default, definePlugin };
160
+ export { AppOption, Argument, Breadc, BreadcError, Command, Option, ParseError, Plugin, breadc, breadc as default, definePlugin, makeTreeNode };
package/dist/index.mjs CHANGED
@@ -49,6 +49,13 @@ function makePluginContainer(plugins = []) {
49
49
  }
50
50
  };
51
51
  return {
52
+ init(breadc, allCommands, globalOptions) {
53
+ if (plugins.length === 0)
54
+ return;
55
+ for (const p of plugins) {
56
+ p.onInit?.(breadc, allCommands, globalOptions);
57
+ }
58
+ },
52
59
  async preRun(breadc) {
53
60
  if (plugins.length === 0)
54
61
  return;
@@ -543,7 +550,7 @@ function makeVersionCommand(name, config) {
543
550
  };
544
551
  return option;
545
552
  }
546
- function makeHelpCommand(name, config) {
553
+ function makeHelpCommand(name, config, allCommands) {
547
554
  function expandMessage(message) {
548
555
  const result = [];
549
556
  for (const row of message) {
@@ -565,6 +572,7 @@ function makeHelpCommand(name, config) {
565
572
  }
566
573
  function expandCommands(cursor) {
567
574
  const visited = /* @__PURE__ */ new WeakSet();
575
+ const added = /* @__PURE__ */ new WeakSet();
568
576
  const commands = cursor.command ? [cursor.command] : [];
569
577
  const q = [cursor];
570
578
  visited.add(cursor);
@@ -573,7 +581,8 @@ function makeHelpCommand(name, config) {
573
581
  for (const [_key, cmd] of cur.children) {
574
582
  if (!visited.has(cmd)) {
575
583
  visited.add(cmd);
576
- if (cmd.command) {
584
+ if (cmd.command && !added.has(cmd.command)) {
585
+ added.add(cmd.command);
577
586
  commands.push(cmd.command);
578
587
  }
579
588
  q.push(cmd);
@@ -586,6 +595,7 @@ function makeHelpCommand(name, config) {
586
595
  async callback(parsed) {
587
596
  const context = parsed.options.__context__;
588
597
  const cursor = parsed.options.__cursor__;
598
+ const usage = allCommands.length === 0 ? `[OPTIONS]` : allCommands.length === 1 ? `[OPTIONS] ${allCommands[0].format}` : allCommands.some((c) => c._default) ? `[OPTIONS] [COMMAND]` : `[OPTIONS] <COMMAND>`;
589
599
  const output = [
590
600
  `${name}/${config.version ? config.version : "unknown"}`,
591
601
  () => {
@@ -595,6 +605,8 @@ function makeHelpCommand(name, config) {
595
605
  return void 0;
596
606
  }
597
607
  },
608
+ "",
609
+ `${bold(underline("Usage:"))} ${bold(name)} ${usage}`,
598
610
  () => {
599
611
  const cmds = expandCommands(cursor);
600
612
  if (cmds.length > 0) {
@@ -658,7 +670,11 @@ function makeHelpCommand(name, config) {
658
670
 
659
671
  function breadc(name, config = {}) {
660
672
  let defaultCommand = void 0;
661
- const globalOptions = [];
673
+ const allCommands = [];
674
+ const globalOptions = [
675
+ makeHelpCommand(name, config, allCommands),
676
+ makeVersionCommand(name, config)
677
+ ];
662
678
  const container = makePluginContainer(config.plugins);
663
679
  const root = makeTreeNode({
664
680
  init(context) {
@@ -666,15 +682,13 @@ function breadc(name, config = {}) {
666
682
  if (defaultCommand) {
667
683
  initContextOptions(defaultCommand._options, context);
668
684
  }
669
- initContextOptions(
670
- [makeHelpCommand(name, config), makeVersionCommand(name, config)],
671
- context
672
- );
673
685
  },
674
686
  finish() {
675
687
  }
676
688
  });
677
689
  const breadc2 = {
690
+ name,
691
+ description: config.description ?? "",
678
692
  option(format, _config, _config2 = {}) {
679
693
  const config2 = typeof _config === "string" ? { description: _config, ..._config2 } : _config;
680
694
  const option = makeOption(format, config2);
@@ -687,6 +701,7 @@ function breadc(name, config = {}) {
687
701
  if (command._default) {
688
702
  defaultCommand = command;
689
703
  }
704
+ allCommands.push(command);
690
705
  return command;
691
706
  },
692
707
  parse(args) {
@@ -707,7 +722,8 @@ function breadc(name, config = {}) {
707
722
  return void 0;
708
723
  }
709
724
  };
725
+ container.init(breadc2, allCommands, globalOptions);
710
726
  return breadc2;
711
727
  }
712
728
 
713
- export { BreadcError, ParseError, breadc, breadc as default, definePlugin };
729
+ export { BreadcError, ParseError, breadc, breadc as default, definePlugin, makeTreeNode };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "breadc",
3
- "version": "0.8.3",
3
+ "version": "0.8.5",
4
4
  "description": "Yet another Command Line Application Framework with fully strong TypeScript support",
5
5
  "keywords": [
6
6
  "breadc",
@@ -34,13 +34,13 @@
34
34
  "dist"
35
35
  ],
36
36
  "dependencies": {
37
- "@breadc/color": "0.8.3"
37
+ "@breadc/color": "0.8.5"
38
38
  },
39
39
  "devDependencies": {
40
- "@types/node": "^18.11.18",
40
+ "@types/node": "^18.11.19",
41
41
  "@vitest/coverage-c8": "^0.28.4",
42
42
  "cac": "^6.7.14",
43
- "vitest": "0.28.3"
43
+ "vitest": "0.28.4"
44
44
  },
45
45
  "scripts": {
46
46
  "build": "unbuild",