@clerc/plugin-help 1.0.1 → 1.0.3

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
@@ -1,26 +1,6 @@
1
1
  import { Plugin } from "@clerc/core";
2
+ import { FlagDefaultValue, TypeValue } from "@clerc/parser";
2
3
 
3
- //#region ../parser/src/types.d.ts
4
- interface FlagDefaultValueFunction<T> {
5
- (): T;
6
- display?: string;
7
- }
8
- type FlagDefaultValue<T = unknown> = T | FlagDefaultValueFunction<T>;
9
- /**
10
- * Defines how a string input is converted to the target type T.
11
- *
12
- * @template T The target type.
13
- */
14
- interface TypeFunction<T = unknown> {
15
- (value: string): T;
16
- /**
17
- * Optional display name for the type, useful in help output.
18
- * If provided, this will be shown instead of the function name.
19
- */
20
- display?: string;
21
- }
22
- type TypeValue<T = unknown> = TypeFunction<T> | readonly [TypeFunction<T>];
23
- //#endregion
24
4
  //#region src/types.d.ts
25
5
  interface Formatters {
26
6
  formatTypeValue: (type: TypeValue) => string;
package/dist/index.js CHANGED
@@ -1,17 +1,9 @@
1
1
  import { DOUBLE_DASH, NoSuchCommandError, definePlugin, normalizeFlagValue, normalizeParameterValue, resolveCommand } from "@clerc/core";
2
+ import { formatFlagName, formatVersion, isTruthy, objectIsEmpty, toArray } from "@clerc/utils";
2
3
  import * as tint from "@uttr/tint";
3
4
  import stringWidth from "string-width";
4
5
  import textTable from "text-table";
5
6
 
6
- //#region ../utils/src/index.ts
7
- const toArray = (a) => Array.isArray(a) ? a : [a];
8
- const kebabCase = (s) => s.replace(/([A-Z])/g, (_, c) => `-${c.toLowerCase()}`);
9
- const formatFlagName = (n) => n.length <= 1 ? `-${n}` : `--${kebabCase(n)}`;
10
- const formatVersion = (v) => v.length === 0 ? "" : v.startsWith("v") ? v : `v${v}`;
11
- const isTruthy = Boolean;
12
- const objectIsEmpty = (obj) => Object.keys(obj).length === 0;
13
-
14
- //#endregion
15
7
  //#region src/utils.ts
16
8
  function formatTypeValue(type) {
17
9
  if (typeof type === "function") return type.display ?? type.name;
@@ -61,18 +53,18 @@ var HelpRenderer = class {
61
53
  get _globalFlagGroups() {
62
54
  return groupDefinitionsToMap(this._getGroups().globalFlags);
63
55
  }
64
- constructor(_formatters, _cli, _globalFlags, _getGroups, _getExamples, _notes) {
56
+ constructor(_formatters, _cli, _globalFlags, _getGroups, _examples, _notes) {
65
57
  this._formatters = _formatters;
66
58
  this._cli = _cli;
67
59
  this._globalFlags = _globalFlags;
68
60
  this._getGroups = _getGroups;
69
- this._getExamples = _getExamples;
61
+ this._examples = _examples;
70
62
  this._notes = _notes;
71
63
  }
72
64
  setCommand(command) {
73
65
  if (command) {
74
66
  this._command = command;
75
- this._getExamples = () => command?.help?.examples;
67
+ this._examples = command?.help?.examples;
76
68
  this._notes = command?.help?.notes;
77
69
  }
78
70
  }
@@ -276,11 +268,10 @@ var HelpRenderer = class {
276
268
  };
277
269
  }
278
270
  renderExamples() {
279
- const examples = this._getExamples();
280
- if (!examples?.length) return;
271
+ if (!this._examples?.length) return;
281
272
  return {
282
273
  title: "Examples",
283
- body: splitTable(examples.map(([command, description]) => {
274
+ body: splitTable(this._examples.map(([command, description]) => {
284
275
  return [
285
276
  command,
286
277
  DELIMITER,
@@ -309,24 +300,12 @@ const helpPlugin = ({ command = true, flag = true, showHelpWhenNoCommandSpecifie
309
300
  ...defaultFormatters,
310
301
  ...formatters
311
302
  };
312
- const generalHelpNotes = [
313
- "If no command is specified, show help for the CLI.",
314
- "If a command is specified, show help for the command.",
315
- flag && "-h is an alias for --help."
316
- ].filter(isTruthy);
317
- const getGeneralHelpExamples = () => [
318
- command && [`$ ${cli._scriptName} help`, "Show help"],
319
- command && [`$ ${cli._scriptName} help <command>`, "Show help for a specific command"],
320
- flag && [`$ ${cli._scriptName} <command> --help`, "Show help for a specific command"]
321
- ].filter(isTruthy);
322
- const effectiveNotes = notes ?? generalHelpNotes;
323
- const getEffectiveExamples = () => examples ?? getGeneralHelpExamples();
324
303
  function printHelp(s) {
325
304
  if (header) console.log(header);
326
305
  console.log(s);
327
306
  if (footer) console.log(footer);
328
307
  }
329
- const renderer = new HelpRenderer(mergedFormatters, cli, cli._globalFlags, () => groups, getEffectiveExamples, effectiveNotes);
308
+ const renderer = new HelpRenderer(mergedFormatters, cli, cli._globalFlags, () => groups, examples, notes);
330
309
  function tryPrintSubcommandsHelp(commandName) {
331
310
  const subcommandsOutput = renderer.renderAvailableSubcommands(commandName);
332
311
  if (subcommandsOutput) {
@@ -338,8 +317,16 @@ const helpPlugin = ({ command = true, flag = true, showHelpWhenNoCommandSpecifie
338
317
  if (command) cli.command("help", "Show help", {
339
318
  parameters: ["[command...]"],
340
319
  help: {
341
- notes: generalHelpNotes,
342
- examples: getGeneralHelpExamples()
320
+ notes: [
321
+ "If no command is specified, show help for the CLI.",
322
+ "If a command is specified, show help for the command.",
323
+ flag && "-h is an alias for --help."
324
+ ].filter(isTruthy),
325
+ examples: [
326
+ command && [`$ ${cli._scriptName} help`, "Show help"],
327
+ command && [`$ ${cli._scriptName} help <command>`, "Show help for a specific command"],
328
+ flag && [`$ ${cli._scriptName} <command> --help`, "Show help for a specific command"]
329
+ ].filter(isTruthy)
343
330
  }
344
331
  }).on("help", (ctx) => {
345
332
  const commandName = ctx.parameters.command;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clerc/plugin-help",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "author": "Ray <i@mk1.io> (https://github.com/so1ve)",
5
5
  "type": "module",
6
6
  "description": "Clerc plugin help",
@@ -45,16 +45,16 @@
45
45
  "access": "public"
46
46
  },
47
47
  "dependencies": {
48
- "@types/text-table": "^0.2.5",
49
48
  "@uttr/tint": "^0.1.3",
50
49
  "string-width": "^8.1.0",
51
- "text-table": "^0.2.0"
50
+ "text-table": "^0.2.0",
51
+ "@clerc/utils": "1.0.3"
52
52
  },
53
53
  "devDependencies": {
54
+ "@types/text-table": "^0.2.5",
54
55
  "kons": "^0.7.1",
55
- "@clerc/core": "1.0.1",
56
- "@clerc/utils": "1.0.1",
57
- "@clerc/parser": "1.0.1"
56
+ "@clerc/core": "1.0.3",
57
+ "@clerc/parser": "1.0.3"
58
58
  },
59
59
  "peerDependencies": {
60
60
  "@clerc/core": "*"