@clerc/plugin-help 1.0.0-beta.11 → 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 +43 -23
  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";
@@ -18,6 +18,10 @@ function formatFlagType(type) {
18
18
  const innerType = type[0];
19
19
  return `Array<${innerType.displayName ?? innerType.name}>`;
20
20
  }
21
+ function formatCommandName(name) {
22
+ if (name === "") return "(root)";
23
+ return name;
24
+ }
21
25
 
22
26
  //#endregion
23
27
  //#region src/formatters.ts
@@ -92,23 +96,48 @@ var HelpRenderer = class {
92
96
  body: [yc.magenta(usage)]
93
97
  };
94
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
+ }
95
109
  renderCommands() {
96
110
  const commands = this._cli._commands;
97
- 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;
98
121
  const groupedCommands = /* @__PURE__ */ new Map();
99
122
  const defaultCommands = [];
100
- for (const command of commands.values()) {
123
+ let rootCommand = [];
124
+ for (const command of commandsToShow.values()) {
101
125
  if (command.__isAlias || command.help?.show === false) continue;
102
126
  const group = command.help?.group;
103
127
  validateGroup(group, this._commandGroups, "command", command.name);
104
- const item = [`${yc.cyan(command.name)}${command.alias ? ` (${toArray(command.alias).join(", ")})` : ""}`, command.description];
105
- if (group && group !== DEFAULT_GROUP_KEY) {
128
+ const item = [`${yc.cyan(formatCommandName(command.name.slice(prefix.length)))}${command.alias ? ` (${toArray(command.alias).join(", ")})` : ""}`, command.description];
129
+ if (command.name === "") rootCommand = item;
130
+ else if (group && group !== DEFAULT_GROUP_KEY) {
106
131
  const groupItems = groupedCommands.get(group) ?? [];
107
132
  groupItems.push(item);
108
133
  groupedCommands.set(group, groupItems);
109
134
  } else defaultCommands.push(item);
110
135
  }
111
136
  const body = [];
137
+ const defaultGroup = [];
138
+ if (rootCommand.length > 0) defaultGroup.push(rootCommand);
139
+ if (defaultCommands.length > 0) defaultGroup.push(...defaultCommands);
140
+ if (defaultGroup.length > 0) body.push(...splitTable(defaultGroup));
112
141
  for (const [key, name] of this._commandGroups) {
113
142
  const items = groupedCommands.get(key);
114
143
  if (items && items.length > 0) {
@@ -117,13 +146,8 @@ var HelpRenderer = class {
117
146
  body.push(...splitTable(items).map(withIndent));
118
147
  }
119
148
  }
120
- if (defaultCommands.length > 0) if (body.length > 0) {
121
- body.push("");
122
- body.push(`${yc.dim("Other")}`);
123
- body.push(...splitTable(defaultCommands).map(withIndent));
124
- } else body.push(...splitTable(defaultCommands));
125
149
  return {
126
- title: "Commands",
150
+ title,
127
151
  body
128
152
  };
129
153
  }
@@ -152,6 +176,7 @@ var HelpRenderer = class {
152
176
  } else defaultFlags.push(item);
153
177
  }
154
178
  const body = [];
179
+ if (defaultFlags.length > 0) body.push(...splitTable(defaultFlags));
155
180
  for (const [key, name] of groupMap) {
156
181
  const items = groupedFlags.get(key);
157
182
  if (items && items.length > 0) {
@@ -160,11 +185,6 @@ var HelpRenderer = class {
160
185
  body.push(...splitTable(items).map(withIndent));
161
186
  }
162
187
  }
163
- if (defaultFlags.length > 0) if (body.length > 0) {
164
- body.push("");
165
- body.push(`${yc.dim("Other")}`);
166
- body.push(...splitTable(defaultFlags).map(withIndent));
167
- } else body.push(...splitTable(defaultFlags));
168
188
  return body;
169
189
  }
170
190
  renderCommandFlags() {
@@ -238,10 +258,7 @@ const helpPlugin = ({ command = true, flag = true, showHelpWhenNoCommandSpecifie
238
258
  let command$1;
239
259
  if (commandName.length > 0) {
240
260
  [command$1] = resolveCommand(cli._commands, commandName);
241
- if (!command$1) {
242
- console.error(`Command "${commandName.join(" ")}" not found.`);
243
- return;
244
- }
261
+ if (!command$1) throw new NoSuchCommandError(commandName.join(" "));
245
262
  }
246
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());
247
264
  });
@@ -251,10 +268,13 @@ const helpPlugin = ({ command = true, flag = true, showHelpWhenNoCommandSpecifie
251
268
  default: false
252
269
  });
253
270
  cli.interceptor({
254
- enforce: "pre",
271
+ enforce: "post",
255
272
  handler: async (ctx, next) => {
256
- 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());
257
- 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());
258
278
  else await next();
259
279
  }
260
280
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clerc/plugin-help",
3
- "version": "1.0.0-beta.11",
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/parser": "1.0.0-beta.11",
55
- "@clerc/core": "1.0.0-beta.11",
56
- "@clerc/utils": "1.0.0-beta.11"
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": "*"