@clerc/plugin-help 0.22.1 → 0.24.0

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
@@ -24,7 +24,11 @@ interface HelpPluginOptions {
24
24
  * Banner
25
25
  */
26
26
  banner?: string;
27
+ /**
28
+ * Render type
29
+ */
30
+ renderer?: "cliffy" | "typer";
27
31
  }
28
- declare const helpPlugin: ({ command, showHelpWhenNoCommand, notes, examples, banner, }?: HelpPluginOptions) => _clerc_core.Plugin<Clerc<{}>, Clerc<{}>>;
32
+ declare const helpPlugin: ({ command, showHelpWhenNoCommand, notes, examples, banner, renderer, }?: HelpPluginOptions) => _clerc_core.Plugin<Clerc<{}>, Clerc<{}>>;
29
33
 
30
34
  export { HelpPluginOptions, helpPlugin };
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
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
+ import boxen from 'boxen';
4
5
  import getFuncName from 'get-func-name';
5
6
  import { Table } from '@clerc/toolkit';
6
7
 
@@ -35,7 +36,7 @@ const stringifyType = (type) => {
35
36
  return Array.isArray(type) ? `Array<${getFuncName(type[0])}>` : getFuncName(type);
36
37
  };
37
38
 
38
- const render = (sections) => {
39
+ const renderCliffy = (sections) => {
39
40
  const rendered = [];
40
41
  for (const section of sections) {
41
42
  if (section.type === "block" || !section.type) {
@@ -43,7 +44,25 @@ const render = (sections) => {
43
44
  const formattedBody = section.body.map((line) => indent + line);
44
45
  formattedBody.unshift("");
45
46
  const body = formattedBody.join("\n");
46
- rendered.push(table([pc.bold(section.title)], [body]).toString());
47
+ rendered.push(table([pc.bold(`${section.title}:`)], [body]).toString());
48
+ } else if (section.type === "inline") {
49
+ const formattedBody = section.items.map((item) => [pc.bold(item.title), item.body]);
50
+ const tableGenerated = table(...formattedBody);
51
+ rendered.push(tableGenerated.toString());
52
+ }
53
+ rendered.push("");
54
+ }
55
+ return rendered.join("\n");
56
+ };
57
+ const renderTyper = (sections) => {
58
+ const rendered = [];
59
+ for (const section of sections) {
60
+ if (section.type === "block" || !section.type) {
61
+ rendered.push(boxen(section.body.join("\n\n"), {
62
+ title: section.title,
63
+ borderStyle: "round",
64
+ padding: 0.5
65
+ }));
47
66
  } else if (section.type === "inline") {
48
67
  const formattedBody = section.items.map((item) => [pc.bold(item.title), item.body]);
49
68
  const tableGenerated = table(...formattedBody);
@@ -58,13 +77,13 @@ const DELIMITER = pc.yellow("-");
58
77
  const NO_DESCRIPTION = "(No description)";
59
78
  const NAME = "Name:";
60
79
  const VERSION = "Version:";
61
- const COMMANDS = "Commands:";
62
- const SUBCOMMAND = "Subcommand:";
63
- const FLAGS = "Flags:";
64
- const DESCRIPTION = "Description:";
65
- const USAGE = "Usage:";
66
- const EXAMPLES = "Examples:";
67
- const NOTES = "Notes:";
80
+ const COMMANDS = "Commands";
81
+ const SUBCOMMAND = "Subcommand";
82
+ const FLAGS = "Flags";
83
+ const DESCRIPTION = "Description";
84
+ const USAGE = "Usage";
85
+ const EXAMPLES = "Examples";
86
+ const NOTES = "Notes";
68
87
  const print = (s) => {
69
88
  process.stdout.write(s);
70
89
  };
@@ -101,7 +120,7 @@ const generateExamples = (sections, examples) => {
101
120
  body: splitTable(...examplesFormatted)
102
121
  });
103
122
  };
104
- const generateHelp = (ctx, notes, examples) => {
123
+ const generateHelp = (render, ctx, notes, examples) => {
105
124
  const { cli } = ctx;
106
125
  const sections = [];
107
126
  generateCliDetail(sections, cli);
@@ -138,7 +157,7 @@ const generateHelp = (ctx, notes, examples) => {
138
157
  }
139
158
  return render(sections);
140
159
  };
141
- const generateSubcommandHelp = (ctx, command) => {
160
+ const generateSubcommandHelp = (render, ctx, command) => {
142
161
  var _a;
143
162
  const { cli } = ctx;
144
163
  const subcommand = resolveCommand(cli._commands, command);
@@ -191,9 +210,11 @@ const helpPlugin = ({
191
210
  showHelpWhenNoCommand = true,
192
211
  notes,
193
212
  examples,
194
- banner
213
+ banner,
214
+ renderer
195
215
  } = {}) => definePlugin({
196
216
  setup: (cli) => {
217
+ const render = renderer === "cliffy" || !renderer ? renderCliffy : renderTyper;
197
218
  const printHelp = (s) => {
198
219
  banner && print(`${banner}
199
220
  `);
@@ -216,9 +237,9 @@ const helpPlugin = ({
216
237
  ]
217
238
  }).on("help", (ctx) => {
218
239
  if (ctx.parameters.command.length) {
219
- printHelp(generateSubcommandHelp(ctx, ctx.parameters.command));
240
+ printHelp(generateSubcommandHelp(render, ctx, ctx.parameters.command));
220
241
  } else {
221
- printHelp(generateHelp(ctx, notes, examples));
242
+ printHelp(generateHelp(render, ctx, notes, examples));
222
243
  }
223
244
  });
224
245
  }
@@ -226,7 +247,7 @@ const helpPlugin = ({
226
247
  const helpFlag = ctx.raw.mergedFlags.h || ctx.raw.mergedFlags.help;
227
248
  if (!ctx.hasRootOrAlias && !ctx.raw._.length && showHelpWhenNoCommand && !helpFlag) {
228
249
  let str = "No command given.\n\n";
229
- str += generateHelp(ctx, notes, examples);
250
+ str += generateHelp(render, ctx, notes, examples);
230
251
  str += "\n";
231
252
  printHelp(str);
232
253
  process.exit(1);
@@ -234,13 +255,13 @@ const helpPlugin = ({
234
255
  if (ctx.raw._.length) {
235
256
  if (ctx.called !== Root) {
236
257
  if (ctx.name === Root) {
237
- printHelp(generateHelp(ctx, notes, examples));
258
+ printHelp(generateHelp(render, ctx, notes, examples));
238
259
  } else {
239
- printHelp(generateSubcommandHelp(ctx, ctx.raw._));
260
+ printHelp(generateSubcommandHelp(render, ctx, ctx.raw._));
240
261
  }
241
262
  }
242
263
  } else {
243
- printHelp(generateHelp(ctx, notes, examples));
264
+ printHelp(generateHelp(render, ctx, notes, examples));
244
265
  }
245
266
  } else {
246
267
  next();
package/dist/index.mjs CHANGED
@@ -1,6 +1,7 @@
1
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
+ import boxen from 'boxen';
4
5
  import getFuncName from 'get-func-name';
5
6
  import { Table } from '@clerc/toolkit';
6
7
 
@@ -35,7 +36,7 @@ const stringifyType = (type) => {
35
36
  return Array.isArray(type) ? `Array<${getFuncName(type[0])}>` : getFuncName(type);
36
37
  };
37
38
 
38
- const render = (sections) => {
39
+ const renderCliffy = (sections) => {
39
40
  const rendered = [];
40
41
  for (const section of sections) {
41
42
  if (section.type === "block" || !section.type) {
@@ -43,7 +44,25 @@ const render = (sections) => {
43
44
  const formattedBody = section.body.map((line) => indent + line);
44
45
  formattedBody.unshift("");
45
46
  const body = formattedBody.join("\n");
46
- rendered.push(table([pc.bold(section.title)], [body]).toString());
47
+ rendered.push(table([pc.bold(`${section.title}:`)], [body]).toString());
48
+ } else if (section.type === "inline") {
49
+ const formattedBody = section.items.map((item) => [pc.bold(item.title), item.body]);
50
+ const tableGenerated = table(...formattedBody);
51
+ rendered.push(tableGenerated.toString());
52
+ }
53
+ rendered.push("");
54
+ }
55
+ return rendered.join("\n");
56
+ };
57
+ const renderTyper = (sections) => {
58
+ const rendered = [];
59
+ for (const section of sections) {
60
+ if (section.type === "block" || !section.type) {
61
+ rendered.push(boxen(section.body.join("\n\n"), {
62
+ title: section.title,
63
+ borderStyle: "round",
64
+ padding: 0.5
65
+ }));
47
66
  } else if (section.type === "inline") {
48
67
  const formattedBody = section.items.map((item) => [pc.bold(item.title), item.body]);
49
68
  const tableGenerated = table(...formattedBody);
@@ -58,13 +77,13 @@ const DELIMITER = pc.yellow("-");
58
77
  const NO_DESCRIPTION = "(No description)";
59
78
  const NAME = "Name:";
60
79
  const VERSION = "Version:";
61
- const COMMANDS = "Commands:";
62
- const SUBCOMMAND = "Subcommand:";
63
- const FLAGS = "Flags:";
64
- const DESCRIPTION = "Description:";
65
- const USAGE = "Usage:";
66
- const EXAMPLES = "Examples:";
67
- const NOTES = "Notes:";
80
+ const COMMANDS = "Commands";
81
+ const SUBCOMMAND = "Subcommand";
82
+ const FLAGS = "Flags";
83
+ const DESCRIPTION = "Description";
84
+ const USAGE = "Usage";
85
+ const EXAMPLES = "Examples";
86
+ const NOTES = "Notes";
68
87
  const print = (s) => {
69
88
  process.stdout.write(s);
70
89
  };
@@ -101,7 +120,7 @@ const generateExamples = (sections, examples) => {
101
120
  body: splitTable(...examplesFormatted)
102
121
  });
103
122
  };
104
- const generateHelp = (ctx, notes, examples) => {
123
+ const generateHelp = (render, ctx, notes, examples) => {
105
124
  const { cli } = ctx;
106
125
  const sections = [];
107
126
  generateCliDetail(sections, cli);
@@ -138,7 +157,7 @@ const generateHelp = (ctx, notes, examples) => {
138
157
  }
139
158
  return render(sections);
140
159
  };
141
- const generateSubcommandHelp = (ctx, command) => {
160
+ const generateSubcommandHelp = (render, ctx, command) => {
142
161
  var _a;
143
162
  const { cli } = ctx;
144
163
  const subcommand = resolveCommand(cli._commands, command);
@@ -191,9 +210,11 @@ const helpPlugin = ({
191
210
  showHelpWhenNoCommand = true,
192
211
  notes,
193
212
  examples,
194
- banner
213
+ banner,
214
+ renderer
195
215
  } = {}) => definePlugin({
196
216
  setup: (cli) => {
217
+ const render = renderer === "cliffy" || !renderer ? renderCliffy : renderTyper;
197
218
  const printHelp = (s) => {
198
219
  banner && print(`${banner}
199
220
  `);
@@ -216,9 +237,9 @@ const helpPlugin = ({
216
237
  ]
217
238
  }).on("help", (ctx) => {
218
239
  if (ctx.parameters.command.length) {
219
- printHelp(generateSubcommandHelp(ctx, ctx.parameters.command));
240
+ printHelp(generateSubcommandHelp(render, ctx, ctx.parameters.command));
220
241
  } else {
221
- printHelp(generateHelp(ctx, notes, examples));
242
+ printHelp(generateHelp(render, ctx, notes, examples));
222
243
  }
223
244
  });
224
245
  }
@@ -226,7 +247,7 @@ const helpPlugin = ({
226
247
  const helpFlag = ctx.raw.mergedFlags.h || ctx.raw.mergedFlags.help;
227
248
  if (!ctx.hasRootOrAlias && !ctx.raw._.length && showHelpWhenNoCommand && !helpFlag) {
228
249
  let str = "No command given.\n\n";
229
- str += generateHelp(ctx, notes, examples);
250
+ str += generateHelp(render, ctx, notes, examples);
230
251
  str += "\n";
231
252
  printHelp(str);
232
253
  process.exit(1);
@@ -234,13 +255,13 @@ const helpPlugin = ({
234
255
  if (ctx.raw._.length) {
235
256
  if (ctx.called !== Root) {
236
257
  if (ctx.name === Root) {
237
- printHelp(generateHelp(ctx, notes, examples));
258
+ printHelp(generateHelp(render, ctx, notes, examples));
238
259
  } else {
239
- printHelp(generateSubcommandHelp(ctx, ctx.raw._));
260
+ printHelp(generateSubcommandHelp(render, ctx, ctx.raw._));
240
261
  }
241
262
  }
242
263
  } else {
243
- printHelp(generateHelp(ctx, notes, examples));
264
+ printHelp(generateHelp(render, ctx, notes, examples));
244
265
  }
245
266
  } else {
246
267
  next();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clerc/plugin-help",
3
- "version": "0.22.1",
3
+ "version": "0.24.0",
4
4
  "author": "Ray <nn_201312@163.com> (https://github.com/so1ve)",
5
5
  "description": "Clerc plugin help",
6
6
  "keywords": [
@@ -51,13 +51,14 @@
51
51
  "@clerc/core": "*"
52
52
  },
53
53
  "dependencies": {
54
+ "boxen": "^7.0.1",
54
55
  "get-func-name": "^2.0.0",
55
56
  "picocolors": "^1.0.0",
56
- "@clerc/toolkit": "0.22.1",
57
- "@clerc/utils": "0.22.1"
57
+ "@clerc/toolkit": "0.24.0",
58
+ "@clerc/utils": "0.24.0"
58
59
  },
59
60
  "devDependencies": {
60
- "@clerc/core": "0.22.1"
61
+ "@clerc/core": "0.24.0"
61
62
  },
62
63
  "scripts": {
63
64
  "build": "puild",