@clerc/plugin-help 1.0.0-beta.12 → 1.0.0-beta.13

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.
Files changed (2) hide show
  1. package/dist/index.js +31 -12
  2. package/package.json +5 -4
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { definePlugin, resolveCommand } from "@clerc/core";
1
+ import { NoSuchCommandError, definePlugin, resolveCommand } from "@clerc/core";
2
2
  import stringWidth from "string-width";
3
3
  import textTable from "text-table";
4
4
  import * as yc from "yoctocolors";
@@ -96,17 +96,36 @@ var HelpRenderer = class {
96
96
  body: [yc.magenta(usage)]
97
97
  };
98
98
  }
99
+ getSubcommands(parentCommandName) {
100
+ const subcommands = /* @__PURE__ */ new Map();
101
+ if (!parentCommandName) return subcommands;
102
+ const prefix = `${parentCommandName} `;
103
+ for (const [name, command] of this._cli._commands) if (name.startsWith(prefix) && name !== parentCommandName) {
104
+ const subcommandName = name.slice(prefix.length);
105
+ subcommands.set(subcommandName, command);
106
+ }
107
+ return subcommands;
108
+ }
99
109
  renderCommands() {
100
110
  const commands = this._cli._commands;
101
- if (this._command || commands.size === 0) return;
111
+ let commandsToShow;
112
+ let title = "Commands";
113
+ let prefix = "";
114
+ if (this._command) {
115
+ prefix = this._command.name ? `${this._command.name} ` : "";
116
+ title = "Subcommands";
117
+ commandsToShow = this.getSubcommands(this._command.name);
118
+ if (commandsToShow.size === 0) return;
119
+ } else commandsToShow = commands;
120
+ if (commandsToShow.size === 0) return;
102
121
  const groupedCommands = /* @__PURE__ */ new Map();
103
122
  const defaultCommands = [];
104
123
  let rootCommand = [];
105
- for (const command of commands.values()) {
124
+ for (const command of commandsToShow.values()) {
106
125
  if (command.__isAlias || command.help?.show === false) continue;
107
126
  const group = command.help?.group;
108
127
  validateGroup(group, this._commandGroups, "command", command.name);
109
- const item = [`${yc.cyan(formatCommandName(command.name))}${command.alias ? ` (${toArray(command.alias).join(", ")})` : ""}`, command.description];
128
+ const item = [`${yc.cyan(formatCommandName(command.name.slice(prefix.length)))}${command.alias ? ` (${toArray(command.alias).join(", ")})` : ""}`, command.description];
110
129
  if (command.name === "") rootCommand = item;
111
130
  else if (group && group !== DEFAULT_GROUP_KEY) {
112
131
  const groupItems = groupedCommands.get(group) ?? [];
@@ -128,7 +147,7 @@ var HelpRenderer = class {
128
147
  }
129
148
  }
130
149
  return {
131
- title: "Commands",
150
+ title,
132
151
  body
133
152
  };
134
153
  }
@@ -239,10 +258,7 @@ const helpPlugin = ({ command = true, flag = true, showHelpWhenNoCommandSpecifie
239
258
  let command$1;
240
259
  if (commandName.length > 0) {
241
260
  [command$1] = resolveCommand(cli._commands, commandName);
242
- if (!command$1) {
243
- console.error(`Command "${commandName.join(" ")}" not found.`);
244
- return;
245
- }
261
+ if (!command$1) throw new NoSuchCommandError(commandName.join(" "));
246
262
  }
247
263
  printHelp(new HelpRenderer(mergedFormatters, cli, cli._globalFlags, command$1, command$1 ? command$1.help?.notes : effectiveNotes, command$1 ? command$1.help?.examples : effectiveExamples, groups).render());
248
264
  });
@@ -252,10 +268,13 @@ const helpPlugin = ({ command = true, flag = true, showHelpWhenNoCommandSpecifie
252
268
  default: false
253
269
  });
254
270
  cli.interceptor({
255
- enforce: "pre",
271
+ enforce: "post",
256
272
  handler: async (ctx, next) => {
257
- if (ctx.flags.help) printHelp(new HelpRenderer(mergedFormatters, cli, cli._globalFlags, ctx.command, ctx.command ? ctx.command.help?.notes : effectiveNotes, ctx.command ? ctx.command.help?.examples : effectiveExamples, groups).render());
258
- else if (showHelpWhenNoCommandSpecified && !ctx.command && ctx.rawParsed.parameters.length === 0) printHelp(new HelpRenderer(mergedFormatters, cli, cli._globalFlags, void 0, effectiveNotes, effectiveExamples, groups).render());
273
+ if (ctx.flags.help) {
274
+ const command$1 = ctx.command;
275
+ if (!command$1 && ctx.rawParsed.parameters.length > 0) await next();
276
+ printHelp(new HelpRenderer(mergedFormatters, cli, cli._globalFlags, command$1, command$1 ? command$1.help?.notes : effectiveNotes, command$1 ? command$1.help?.examples : effectiveExamples, groups).render());
277
+ } else if (showHelpWhenNoCommandSpecified && !ctx.command && ctx.rawParsed.parameters.length === 0) printHelp(new HelpRenderer(mergedFormatters, cli, cli._globalFlags, void 0, effectiveNotes, effectiveExamples, groups).render());
259
278
  else await next();
260
279
  }
261
280
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clerc/plugin-help",
3
- "version": "1.0.0-beta.12",
3
+ "version": "1.0.0-beta.13",
4
4
  "author": "Ray <i@mk1.io> (https://github.com/so1ve)",
5
5
  "type": "module",
6
6
  "description": "Clerc plugin help",
@@ -51,9 +51,10 @@
51
51
  "yoctocolors": "^2.1.2"
52
52
  },
53
53
  "devDependencies": {
54
- "@clerc/core": "1.0.0-beta.12",
55
- "@clerc/utils": "1.0.0-beta.12",
56
- "@clerc/parser": "1.0.0-beta.12"
54
+ "kons": "^0.7.1",
55
+ "@clerc/core": "1.0.0-beta.13",
56
+ "@clerc/parser": "1.0.0-beta.13",
57
+ "@clerc/utils": "1.0.0-beta.13"
57
58
  },
58
59
  "peerDependencies": {
59
60
  "@clerc/core": "*"