breadc 0.8.4 → 0.8.6

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;
@@ -569,6 +576,7 @@ function makeHelpCommand(name, config, allCommands) {
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, allCommands) {
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);
@@ -586,11 +595,11 @@ function makeHelpCommand(name, config, allCommands) {
586
595
  }
587
596
  return commands;
588
597
  }
589
- const usage = allCommands.length === 0 ? `[OPTIONS]` : allCommands.length === 1 ? `[OPTIONS] ${allCommands[0].format}` : allCommands.some((c) => c._default) ? `[OPTIONS] [COMMAND]` : `[OPTIONS] <COMMAND>`;
590
598
  const command = {
591
599
  async callback(parsed) {
592
600
  const context = parsed.options.__context__;
593
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>`;
594
603
  const output = [
595
604
  `${name}/${config.version ? config.version : "unknown"}`,
596
605
  () => {
@@ -666,7 +675,10 @@ function makeHelpCommand(name, config, allCommands) {
666
675
  function breadc(name, config = {}) {
667
676
  let defaultCommand = void 0;
668
677
  const allCommands = [];
669
- const globalOptions = [];
678
+ const globalOptions = [
679
+ makeHelpCommand(name, config, allCommands),
680
+ makeVersionCommand(name, config)
681
+ ];
670
682
  const container = makePluginContainer(config.plugins);
671
683
  const root = makeTreeNode({
672
684
  init(context) {
@@ -674,18 +686,13 @@ function breadc(name, config = {}) {
674
686
  if (defaultCommand) {
675
687
  initContextOptions(defaultCommand._options, context);
676
688
  }
677
- initContextOptions(
678
- [
679
- makeHelpCommand(name, config, allCommands),
680
- makeVersionCommand(name, config)
681
- ],
682
- context
683
- );
684
689
  },
685
690
  finish() {
686
691
  }
687
692
  });
688
693
  const breadc2 = {
694
+ name,
695
+ description: config.description ?? "",
689
696
  option(format, _config, _config2 = {}) {
690
697
  const config2 = typeof _config === "string" ? { description: _config, ..._config2 } : _config;
691
698
  const option = makeOption(format, config2);
@@ -719,6 +726,7 @@ function breadc(name, config = {}) {
719
726
  return void 0;
720
727
  }
721
728
  };
729
+ container.init(breadc2, allCommands, globalOptions);
722
730
  return breadc2;
723
731
  }
724
732
 
@@ -727,3 +735,4 @@ exports.ParseError = ParseError;
727
735
  exports.breadc = breadc;
728
736
  exports.default = breadc;
729
737
  exports.definePlugin = definePlugin;
738
+ exports.makeTreeNode = makeTreeNode;
package/dist/index.d.ts CHANGED
@@ -47,6 +47,11 @@ interface TreeNode {
47
47
  finish(context: Context): void;
48
48
  }
49
49
 
50
+ declare function makeTreeNode(pnode: Partial<TreeNode>): TreeNode;
51
+
52
+ type Prettify<T> = {
53
+ [K in keyof T]: T[K];
54
+ } & {};
50
55
  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
56
  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
57
  type Letter = Lowercase | Uppercase;
@@ -75,9 +80,9 @@ type ExtractOption<T extends string, D = undefined> = {
75
80
  [k in ExtractOptionName<T>]: D extends undefined ? ExtractOptionType<T> : D;
76
81
  };
77
82
  type Push<T extends any[], U, R> = [...T, U, R];
78
- type ActionFn<T extends any[], Option extends object = {}, R = any> = (...arg: Push<T, Option & {
83
+ type ActionFn<T extends any[], Option extends object = {}, R = any> = (...arg: Push<T, Prettify<Option & {
79
84
  '--': string[];
80
- }, {}>) => R | Promise<R>;
85
+ }>, {}>) => R | Promise<R>;
81
86
  /**
82
87
  * Max Dep: 5
83
88
  *
@@ -91,6 +96,8 @@ interface AppOption {
91
96
  plugins?: Partial<Plugin>[];
92
97
  }
93
98
  interface Breadc<GlobalOption extends object = {}> {
99
+ name: string;
100
+ description: string;
94
101
  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
102
  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
103
  command<F extends string = string>(format: F, description?: string): Command<F, ExtractCommand<F>, {}, GlobalOption>;
@@ -137,10 +144,11 @@ interface OptionOption<T extends string | boolean, R extends any = T> {
137
144
  }
138
145
  type CommandHookFn = (result: ParseResult) => void | Promise<void>;
139
146
  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>;
147
+ onInit?(breadc: Breadc, allCommands: Command[], globalOptions: Option[]): void;
148
+ onPreRun?(breadc: Breadc): void | Promise<void>;
149
+ onPreCommand?: Record<string, CommandHookFn> | CommandHookFn;
150
+ onPostCommand?: Record<string, CommandHookFn> | CommandHookFn;
151
+ onPostRun?(breadc: Breadc): void | Promise<void>;
144
152
  }
145
153
 
146
154
  declare function breadc(name: string, config?: AppOption): Breadc<{}>;
@@ -152,4 +160,4 @@ declare class BreadcError extends Error {
152
160
  declare class ParseError extends Error {
153
161
  }
154
162
 
155
- export { AppOption, Argument, Breadc, BreadcError, Command, Option, ParseError, Plugin, breadc, breadc as default, definePlugin };
163
+ 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;
@@ -565,6 +572,7 @@ function makeHelpCommand(name, config, allCommands) {
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, allCommands) {
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);
@@ -582,11 +591,11 @@ function makeHelpCommand(name, config, allCommands) {
582
591
  }
583
592
  return commands;
584
593
  }
585
- const usage = allCommands.length === 0 ? `[OPTIONS]` : allCommands.length === 1 ? `[OPTIONS] ${allCommands[0].format}` : allCommands.some((c) => c._default) ? `[OPTIONS] [COMMAND]` : `[OPTIONS] <COMMAND>`;
586
594
  const command = {
587
595
  async callback(parsed) {
588
596
  const context = parsed.options.__context__;
589
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>`;
590
599
  const output = [
591
600
  `${name}/${config.version ? config.version : "unknown"}`,
592
601
  () => {
@@ -662,7 +671,10 @@ function makeHelpCommand(name, config, allCommands) {
662
671
  function breadc(name, config = {}) {
663
672
  let defaultCommand = void 0;
664
673
  const allCommands = [];
665
- const globalOptions = [];
674
+ const globalOptions = [
675
+ makeHelpCommand(name, config, allCommands),
676
+ makeVersionCommand(name, config)
677
+ ];
666
678
  const container = makePluginContainer(config.plugins);
667
679
  const root = makeTreeNode({
668
680
  init(context) {
@@ -670,18 +682,13 @@ function breadc(name, config = {}) {
670
682
  if (defaultCommand) {
671
683
  initContextOptions(defaultCommand._options, context);
672
684
  }
673
- initContextOptions(
674
- [
675
- makeHelpCommand(name, config, allCommands),
676
- makeVersionCommand(name, config)
677
- ],
678
- context
679
- );
680
685
  },
681
686
  finish() {
682
687
  }
683
688
  });
684
689
  const breadc2 = {
690
+ name,
691
+ description: config.description ?? "",
685
692
  option(format, _config, _config2 = {}) {
686
693
  const config2 = typeof _config === "string" ? { description: _config, ..._config2 } : _config;
687
694
  const option = makeOption(format, config2);
@@ -715,7 +722,8 @@ function breadc(name, config = {}) {
715
722
  return void 0;
716
723
  }
717
724
  };
725
+ container.init(breadc2, allCommands, globalOptions);
718
726
  return breadc2;
719
727
  }
720
728
 
721
- 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.4",
3
+ "version": "0.8.6",
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.4"
37
+ "@breadc/color": "0.8.6"
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",