@clerc/plugin-help 0.1.1 → 0.3.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.cjs CHANGED
@@ -39,34 +39,39 @@ const helpPlugin = (_options) => clerc.definePlugin({
39
39
  const { command, ...rest } = { ...defaultOptions, ..._options };
40
40
  if (command) {
41
41
  cli = cli.command("help", "Show help").on("help", (ctx) => {
42
- showHelp(cli, ctx, rest);
42
+ showHelp(ctx, rest);
43
43
  });
44
44
  }
45
45
  cli = cli.inspector((_ctx, next) => {
46
46
  const ctx = _ctx;
47
- if (ctx.flags.h || ctx.flags.help) {
47
+ if (_ctx.flags.h || _ctx.flags.help) {
48
48
  if (ctx.name === "help") {
49
- showSubcommandHelp(cli, {
49
+ showSubcommandHelp({
50
50
  ...ctx,
51
51
  name: "help"
52
52
  });
53
53
  return;
54
54
  }
55
55
  if (ctx.resolved) {
56
- showSubcommandHelp(cli, ctx);
56
+ showSubcommandHelp(ctx);
57
57
  } else {
58
- showHelp(cli, ctx, rest);
58
+ showHelp(ctx, rest);
59
59
  }
60
60
  return;
61
61
  }
62
+ if (!ctx.resolved && ctx.parameters.length === 0 && Object.keys(ctx.flags).length === 0) {
63
+ showHelp(ctx, rest);
64
+ return;
65
+ }
62
66
  next();
63
67
  });
64
68
  return cli;
65
69
  }
66
70
  });
67
- function showHelp(cli, ctx, { examples, notes }) {
71
+ function showHelp(ctx, { examples, notes }) {
72
+ const { cli } = ctx;
68
73
  if (ctx.parameters.length > 0) {
69
- showSubcommandHelp(cli, ctx);
74
+ showSubcommandHelp(ctx);
70
75
  return;
71
76
  }
72
77
  cli._name && console.log(`${pc__default["default"].green(cli._name)} ${cli._version}`);
@@ -75,11 +80,10 @@ function showHelp(cli, ctx, { examples, notes }) {
75
80
  newline();
76
81
  }
77
82
  console.log(pc__default["default"].yellow("USAGE:"));
78
- console.log(` ${cli._name} <SUBCOMMAND> [OPTIONS]`);
83
+ console.log(` ${cli._name || "<CLI NAME>"} <SUBCOMMAND> [OPTIONS]`);
79
84
  newline();
80
85
  console.log(pc__default["default"].yellow("COMMANDS:"));
81
86
  const commandNameAndAlias = generateNameAndAliasFromCommands(cli._commands);
82
- console.log(commandNameAndAlias);
83
87
  const commandsPadLength = getPadLength(Object.values(commandNameAndAlias));
84
88
  for (const [name, nameAndAlias] of Object.entries(commandNameAndAlias)) {
85
89
  console.log(` ${pc__default["default"].green(nameAndAlias.padEnd(commandsPadLength))}${cli._commands[name].description}`);
@@ -100,28 +104,18 @@ function showHelp(cli, ctx, { examples, notes }) {
100
104
  }
101
105
  }
102
106
  }
103
- function showSubcommandHelp(cli, ctx) {
107
+ function showSubcommandHelp(ctx) {
108
+ const { cli } = ctx;
104
109
  const commandName = String(ctx.name || ctx.parameters[0]);
105
110
  const commandToShowHelp = clerc.resolveCommand(cli._commands, commandName);
106
111
  if (!commandToShowHelp) {
107
- throw new clerc.NoSuchCommandsError(`No such command: ${commandName}`);
112
+ throw new clerc.NoSuchCommandError(`No such command: ${commandName}`);
108
113
  }
109
- console.log(`${pc__default["default"].green(`${cli._name} ${commandToShowHelp.name}`)} ${cli._version}`);
114
+ console.log(`${pc__default["default"].green(`${cli._name}.${commandToShowHelp.name}`)} ${cli._version}`);
110
115
  commandToShowHelp.description && console.log(commandToShowHelp.description);
111
116
  newline();
112
117
  console.log(pc__default["default"].yellow("USAGE:"));
113
118
  console.log(` ${cli._name} ${commandToShowHelp.name} [PARAMETERS] [FLAGS]`);
114
- const parameters = commandToShowHelp.parameters || {};
115
- const parameterKeys = Object.keys(parameters);
116
- if (parameterKeys.length > 0) {
117
- newline();
118
- console.log(pc__default["default"].yellow("PARAMETERS:"));
119
- const parametersPadLength = getPadLength(parameterKeys);
120
- for (const [name, param] of Object.entries(parameters)) {
121
- const resuired = param.required ? pc__default["default"].red(" (required)") : "";
122
- console.log(` ${pc__default["default"].green(name.padEnd(parametersPadLength))}${param.description}${resuired}`);
123
- }
124
- }
125
119
  const flagNameAndAlias = generateFlagNameAndAliasFromCommand(commandToShowHelp);
126
120
  if (Object.keys(flagNameAndAlias).length > 0) {
127
121
  newline();
package/dist/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import * as clerc from 'clerc';
2
- import { Clerc } from 'clerc';
3
2
 
4
3
  interface Options {
5
4
  /**
@@ -15,6 +14,6 @@ interface Options {
15
14
  */
16
15
  notes?: string[];
17
16
  }
18
- declare const helpPlugin: (_options?: Options) => clerc.Plugin<Clerc<{}>, Clerc<{}>>;
17
+ declare const helpPlugin: (_options?: Options) => clerc.Plugin<clerc.Clerc<{}>, clerc.Clerc<{}>>;
19
18
 
20
19
  export { helpPlugin };
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { definePlugin, resolveCommand, NoSuchCommandsError } from 'clerc';
1
+ import { definePlugin, resolveCommand, NoSuchCommandError } from 'clerc';
2
2
  import pc from 'picocolors';
3
3
 
4
4
  const mustArray = (a) => Array.isArray(a) ? a : [a];
@@ -31,34 +31,39 @@ const helpPlugin = (_options) => definePlugin({
31
31
  const { command, ...rest } = { ...defaultOptions, ..._options };
32
32
  if (command) {
33
33
  cli = cli.command("help", "Show help").on("help", (ctx) => {
34
- showHelp(cli, ctx, rest);
34
+ showHelp(ctx, rest);
35
35
  });
36
36
  }
37
37
  cli = cli.inspector((_ctx, next) => {
38
38
  const ctx = _ctx;
39
- if (ctx.flags.h || ctx.flags.help) {
39
+ if (_ctx.flags.h || _ctx.flags.help) {
40
40
  if (ctx.name === "help") {
41
- showSubcommandHelp(cli, {
41
+ showSubcommandHelp({
42
42
  ...ctx,
43
43
  name: "help"
44
44
  });
45
45
  return;
46
46
  }
47
47
  if (ctx.resolved) {
48
- showSubcommandHelp(cli, ctx);
48
+ showSubcommandHelp(ctx);
49
49
  } else {
50
- showHelp(cli, ctx, rest);
50
+ showHelp(ctx, rest);
51
51
  }
52
52
  return;
53
53
  }
54
+ if (!ctx.resolved && ctx.parameters.length === 0 && Object.keys(ctx.flags).length === 0) {
55
+ showHelp(ctx, rest);
56
+ return;
57
+ }
54
58
  next();
55
59
  });
56
60
  return cli;
57
61
  }
58
62
  });
59
- function showHelp(cli, ctx, { examples, notes }) {
63
+ function showHelp(ctx, { examples, notes }) {
64
+ const { cli } = ctx;
60
65
  if (ctx.parameters.length > 0) {
61
- showSubcommandHelp(cli, ctx);
66
+ showSubcommandHelp(ctx);
62
67
  return;
63
68
  }
64
69
  cli._name && console.log(`${pc.green(cli._name)} ${cli._version}`);
@@ -67,11 +72,10 @@ function showHelp(cli, ctx, { examples, notes }) {
67
72
  newline();
68
73
  }
69
74
  console.log(pc.yellow("USAGE:"));
70
- console.log(` ${cli._name} <SUBCOMMAND> [OPTIONS]`);
75
+ console.log(` ${cli._name || "<CLI NAME>"} <SUBCOMMAND> [OPTIONS]`);
71
76
  newline();
72
77
  console.log(pc.yellow("COMMANDS:"));
73
78
  const commandNameAndAlias = generateNameAndAliasFromCommands(cli._commands);
74
- console.log(commandNameAndAlias);
75
79
  const commandsPadLength = getPadLength(Object.values(commandNameAndAlias));
76
80
  for (const [name, nameAndAlias] of Object.entries(commandNameAndAlias)) {
77
81
  console.log(` ${pc.green(nameAndAlias.padEnd(commandsPadLength))}${cli._commands[name].description}`);
@@ -92,28 +96,18 @@ function showHelp(cli, ctx, { examples, notes }) {
92
96
  }
93
97
  }
94
98
  }
95
- function showSubcommandHelp(cli, ctx) {
99
+ function showSubcommandHelp(ctx) {
100
+ const { cli } = ctx;
96
101
  const commandName = String(ctx.name || ctx.parameters[0]);
97
102
  const commandToShowHelp = resolveCommand(cli._commands, commandName);
98
103
  if (!commandToShowHelp) {
99
- throw new NoSuchCommandsError(`No such command: ${commandName}`);
104
+ throw new NoSuchCommandError(`No such command: ${commandName}`);
100
105
  }
101
- console.log(`${pc.green(`${cli._name} ${commandToShowHelp.name}`)} ${cli._version}`);
106
+ console.log(`${pc.green(`${cli._name}.${commandToShowHelp.name}`)} ${cli._version}`);
102
107
  commandToShowHelp.description && console.log(commandToShowHelp.description);
103
108
  newline();
104
109
  console.log(pc.yellow("USAGE:"));
105
110
  console.log(` ${cli._name} ${commandToShowHelp.name} [PARAMETERS] [FLAGS]`);
106
- const parameters = commandToShowHelp.parameters || {};
107
- const parameterKeys = Object.keys(parameters);
108
- if (parameterKeys.length > 0) {
109
- newline();
110
- console.log(pc.yellow("PARAMETERS:"));
111
- const parametersPadLength = getPadLength(parameterKeys);
112
- for (const [name, param] of Object.entries(parameters)) {
113
- const resuired = param.required ? pc.red(" (required)") : "";
114
- console.log(` ${pc.green(name.padEnd(parametersPadLength))}${param.description}${resuired}`);
115
- }
116
- }
117
111
  const flagNameAndAlias = generateFlagNameAndAliasFromCommand(commandToShowHelp);
118
112
  if (Object.keys(flagNameAndAlias).length > 0) {
119
113
  newline();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clerc/plugin-help",
3
- "version": "0.1.1",
3
+ "version": "0.3.0",
4
4
  "author": "Ray <nn_201312@163.com> (https://github.com/so1ve)",
5
5
  "description": "Clerc plugin help",
6
6
  "keywords": [
@@ -39,8 +39,11 @@
39
39
  "publishConfig": {
40
40
  "access": "public"
41
41
  },
42
+ "peerDependencies": {
43
+ "clerc": "*"
44
+ },
42
45
  "dependencies": {
43
- "clerc": "0.1.1",
46
+ "clerc": "0.3.0",
44
47
  "picocolors": "^1.0.0"
45
48
  },
46
49
  "scripts": {