@clerc/plugin-help 0.21.0 → 0.22.1

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.d.ts CHANGED
@@ -20,7 +20,11 @@ interface HelpPluginOptions {
20
20
  * Global examples.
21
21
  */
22
22
  examples?: [string, string][];
23
+ /**
24
+ * Banner
25
+ */
26
+ banner?: string;
23
27
  }
24
- declare const helpPlugin: ({ command, showHelpWhenNoCommand, notes, examples, }?: HelpPluginOptions) => _clerc_core.Plugin<Clerc<{}>, Clerc<{}>>;
28
+ declare const helpPlugin: ({ command, showHelpWhenNoCommand, notes, examples, banner, }?: HelpPluginOptions) => _clerc_core.Plugin<Clerc<{}>, Clerc<{}>>;
25
29
 
26
30
  export { HelpPluginOptions, helpPlugin };
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { definePlugin, SingleCommand, resolveCommand, NoSuchCommandError } from '@clerc/core';
1
+ import { definePlugin, Root, withBrackets, resolveCommand, NoSuchCommandError, formatCommandName } from '@clerc/core';
2
2
  import { toArray, gracefulFlagName } from '@clerc/utils';
3
3
  import pc from 'picocolors';
4
4
  import getFuncName from 'get-func-name';
@@ -56,7 +56,6 @@ const render = (sections) => {
56
56
 
57
57
  const DELIMITER = pc.yellow("-");
58
58
  const NO_DESCRIPTION = "(No description)";
59
- const SINGLE_COMMAND = "<Single Command>";
60
59
  const NAME = "Name:";
61
60
  const VERSION = "Version:";
62
61
  const COMMANDS = "Commands:";
@@ -69,7 +68,6 @@ const NOTES = "Notes:";
69
68
  const print = (s) => {
70
69
  process.stdout.write(s);
71
70
  };
72
- const formatCommandName = (name) => Array.isArray(name) ? name.join(" ") : typeof name === "string" ? name : SINGLE_COMMAND;
73
71
  const generateCliDetail = (sections, cli, subcommand) => {
74
72
  const items = [
75
73
  {
@@ -109,14 +107,14 @@ const generateHelp = (ctx, notes, examples) => {
109
107
  generateCliDetail(sections, cli);
110
108
  sections.push({
111
109
  title: USAGE,
112
- body: [pc.magenta(`$ ${cli._name} [command] [flags]`)]
110
+ body: [pc.magenta(`$ ${cli._name} ${withBrackets("command", ctx.hasRootOrAlias)} [flags]`)]
113
111
  });
114
- const commands = [...ctx.hasSingleCommand ? [cli._commands[SingleCommand]] : [], ...Object.values(cli._commands)].map((command) => {
112
+ const commands = [...ctx.hasRoot ? [cli._commands[Root]] : [], ...Object.values(cli._commands)].map((command) => {
115
113
  const commandNameWithAlias = [typeof command.name === "symbol" ? "" : command.name, ...toArray(command.alias || [])].sort((a, b) => {
116
- if (a === SingleCommand) {
114
+ if (a === Root) {
117
115
  return -1;
118
116
  }
119
- if (b === SingleCommand) {
117
+ if (b === Root) {
120
118
  return 1;
121
119
  }
122
120
  return a.length - b.length;
@@ -150,7 +148,7 @@ const generateSubcommandHelp = (ctx, command) => {
150
148
  const sections = [];
151
149
  generateCliDetail(sections, cli, subcommand);
152
150
  const parameters = ((_a = subcommand.parameters) == null ? void 0 : _a.join(" ")) || void 0;
153
- const commandName = ctx.name === SingleCommand ? "" : ` ${formatCommandName(subcommand.name)}`;
151
+ const commandName = ctx.name === Root ? "" : ` ${formatCommandName(subcommand.name)}`;
154
152
  const parametersString = parameters ? ` ${parameters}` : "";
155
153
  const flagsString = subcommand.flags ? " [flags]" : "";
156
154
  sections.push({
@@ -192,9 +190,15 @@ const helpPlugin = ({
192
190
  command = true,
193
191
  showHelpWhenNoCommand = true,
194
192
  notes,
195
- examples
193
+ examples,
194
+ banner
196
195
  } = {}) => definePlugin({
197
196
  setup: (cli) => {
197
+ const printHelp = (s) => {
198
+ banner && print(`${banner}
199
+ `);
200
+ print(s);
201
+ };
198
202
  if (command) {
199
203
  cli = cli.command("help", "Show help", {
200
204
  parameters: [
@@ -212,31 +216,31 @@ const helpPlugin = ({
212
216
  ]
213
217
  }).on("help", (ctx) => {
214
218
  if (ctx.parameters.command.length) {
215
- print(generateSubcommandHelp(ctx, ctx.parameters.command));
219
+ printHelp(generateSubcommandHelp(ctx, ctx.parameters.command));
216
220
  } else {
217
- print(generateHelp(ctx, notes, examples));
221
+ printHelp(generateHelp(ctx, notes, examples));
218
222
  }
219
223
  });
220
224
  }
221
225
  cli.inspector((ctx, next) => {
222
- const hasHelpFlag = ctx.raw.mergedFlags.h || ctx.raw.mergedFlags.help;
223
- if (!ctx.hasSingleCommandOrAlias && !ctx.raw._.length && showHelpWhenNoCommand && !hasHelpFlag) {
224
- let str = "No command supplied.\n\n";
226
+ const helpFlag = ctx.raw.mergedFlags.h || ctx.raw.mergedFlags.help;
227
+ if (!ctx.hasRootOrAlias && !ctx.raw._.length && showHelpWhenNoCommand && !helpFlag) {
228
+ let str = "No command given.\n\n";
225
229
  str += generateHelp(ctx, notes, examples);
226
230
  str += "\n";
227
- print(str);
231
+ printHelp(str);
228
232
  process.exit(1);
229
- } else if (hasHelpFlag) {
233
+ } else if (helpFlag) {
230
234
  if (ctx.raw._.length) {
231
- if (ctx.called !== SingleCommand) {
232
- if (ctx.name === SingleCommand) {
233
- print(generateHelp(ctx, notes, examples));
235
+ if (ctx.called !== Root) {
236
+ if (ctx.name === Root) {
237
+ printHelp(generateHelp(ctx, notes, examples));
234
238
  } else {
235
- print(generateSubcommandHelp(ctx, ctx.raw._));
239
+ printHelp(generateSubcommandHelp(ctx, ctx.raw._));
236
240
  }
237
241
  }
238
242
  } else {
239
- print(generateHelp(ctx, notes, examples));
243
+ printHelp(generateHelp(ctx, notes, examples));
240
244
  }
241
245
  } else {
242
246
  next();
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { definePlugin, SingleCommand, resolveCommand, NoSuchCommandError } from '@clerc/core';
1
+ import { definePlugin, Root, withBrackets, resolveCommand, NoSuchCommandError, formatCommandName } from '@clerc/core';
2
2
  import { toArray, gracefulFlagName } from '@clerc/utils';
3
3
  import pc from 'picocolors';
4
4
  import getFuncName from 'get-func-name';
@@ -56,7 +56,6 @@ const render = (sections) => {
56
56
 
57
57
  const DELIMITER = pc.yellow("-");
58
58
  const NO_DESCRIPTION = "(No description)";
59
- const SINGLE_COMMAND = "<Single Command>";
60
59
  const NAME = "Name:";
61
60
  const VERSION = "Version:";
62
61
  const COMMANDS = "Commands:";
@@ -69,7 +68,6 @@ const NOTES = "Notes:";
69
68
  const print = (s) => {
70
69
  process.stdout.write(s);
71
70
  };
72
- const formatCommandName = (name) => Array.isArray(name) ? name.join(" ") : typeof name === "string" ? name : SINGLE_COMMAND;
73
71
  const generateCliDetail = (sections, cli, subcommand) => {
74
72
  const items = [
75
73
  {
@@ -109,14 +107,14 @@ const generateHelp = (ctx, notes, examples) => {
109
107
  generateCliDetail(sections, cli);
110
108
  sections.push({
111
109
  title: USAGE,
112
- body: [pc.magenta(`$ ${cli._name} [command] [flags]`)]
110
+ body: [pc.magenta(`$ ${cli._name} ${withBrackets("command", ctx.hasRootOrAlias)} [flags]`)]
113
111
  });
114
- const commands = [...ctx.hasSingleCommand ? [cli._commands[SingleCommand]] : [], ...Object.values(cli._commands)].map((command) => {
112
+ const commands = [...ctx.hasRoot ? [cli._commands[Root]] : [], ...Object.values(cli._commands)].map((command) => {
115
113
  const commandNameWithAlias = [typeof command.name === "symbol" ? "" : command.name, ...toArray(command.alias || [])].sort((a, b) => {
116
- if (a === SingleCommand) {
114
+ if (a === Root) {
117
115
  return -1;
118
116
  }
119
- if (b === SingleCommand) {
117
+ if (b === Root) {
120
118
  return 1;
121
119
  }
122
120
  return a.length - b.length;
@@ -150,7 +148,7 @@ const generateSubcommandHelp = (ctx, command) => {
150
148
  const sections = [];
151
149
  generateCliDetail(sections, cli, subcommand);
152
150
  const parameters = ((_a = subcommand.parameters) == null ? void 0 : _a.join(" ")) || void 0;
153
- const commandName = ctx.name === SingleCommand ? "" : ` ${formatCommandName(subcommand.name)}`;
151
+ const commandName = ctx.name === Root ? "" : ` ${formatCommandName(subcommand.name)}`;
154
152
  const parametersString = parameters ? ` ${parameters}` : "";
155
153
  const flagsString = subcommand.flags ? " [flags]" : "";
156
154
  sections.push({
@@ -192,9 +190,15 @@ const helpPlugin = ({
192
190
  command = true,
193
191
  showHelpWhenNoCommand = true,
194
192
  notes,
195
- examples
193
+ examples,
194
+ banner
196
195
  } = {}) => definePlugin({
197
196
  setup: (cli) => {
197
+ const printHelp = (s) => {
198
+ banner && print(`${banner}
199
+ `);
200
+ print(s);
201
+ };
198
202
  if (command) {
199
203
  cli = cli.command("help", "Show help", {
200
204
  parameters: [
@@ -212,31 +216,31 @@ const helpPlugin = ({
212
216
  ]
213
217
  }).on("help", (ctx) => {
214
218
  if (ctx.parameters.command.length) {
215
- print(generateSubcommandHelp(ctx, ctx.parameters.command));
219
+ printHelp(generateSubcommandHelp(ctx, ctx.parameters.command));
216
220
  } else {
217
- print(generateHelp(ctx, notes, examples));
221
+ printHelp(generateHelp(ctx, notes, examples));
218
222
  }
219
223
  });
220
224
  }
221
225
  cli.inspector((ctx, next) => {
222
- const hasHelpFlag = ctx.raw.mergedFlags.h || ctx.raw.mergedFlags.help;
223
- if (!ctx.hasSingleCommandOrAlias && !ctx.raw._.length && showHelpWhenNoCommand && !hasHelpFlag) {
224
- let str = "No command supplied.\n\n";
226
+ const helpFlag = ctx.raw.mergedFlags.h || ctx.raw.mergedFlags.help;
227
+ if (!ctx.hasRootOrAlias && !ctx.raw._.length && showHelpWhenNoCommand && !helpFlag) {
228
+ let str = "No command given.\n\n";
225
229
  str += generateHelp(ctx, notes, examples);
226
230
  str += "\n";
227
- print(str);
231
+ printHelp(str);
228
232
  process.exit(1);
229
- } else if (hasHelpFlag) {
233
+ } else if (helpFlag) {
230
234
  if (ctx.raw._.length) {
231
- if (ctx.called !== SingleCommand) {
232
- if (ctx.name === SingleCommand) {
233
- print(generateHelp(ctx, notes, examples));
235
+ if (ctx.called !== Root) {
236
+ if (ctx.name === Root) {
237
+ printHelp(generateHelp(ctx, notes, examples));
234
238
  } else {
235
- print(generateSubcommandHelp(ctx, ctx.raw._));
239
+ printHelp(generateSubcommandHelp(ctx, ctx.raw._));
236
240
  }
237
241
  }
238
242
  } else {
239
- print(generateHelp(ctx, notes, examples));
243
+ printHelp(generateHelp(ctx, notes, examples));
240
244
  }
241
245
  } else {
242
246
  next();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clerc/plugin-help",
3
- "version": "0.21.0",
3
+ "version": "0.22.1",
4
4
  "author": "Ray <nn_201312@163.com> (https://github.com/so1ve)",
5
5
  "description": "Clerc plugin help",
6
6
  "keywords": [
@@ -53,11 +53,11 @@
53
53
  "dependencies": {
54
54
  "get-func-name": "^2.0.0",
55
55
  "picocolors": "^1.0.0",
56
- "@clerc/toolkit": "0.21.0",
57
- "@clerc/utils": "0.21.0"
56
+ "@clerc/toolkit": "0.22.1",
57
+ "@clerc/utils": "0.22.1"
58
58
  },
59
59
  "devDependencies": {
60
- "@clerc/core": "0.21.0"
60
+ "@clerc/core": "0.22.1"
61
61
  },
62
62
  "scripts": {
63
63
  "build": "puild",