@clerc/plugin-help 0.3.3 → 0.3.4

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
@@ -11,6 +11,7 @@ var pc__default = /*#__PURE__*/_interopDefaultLegacy(pc);
11
11
 
12
12
  const mustArray = (a) => Array.isArray(a) ? a : [a];
13
13
  const gracefulFlagName = (n) => n.length <= 1 ? `-${n}` : `--${n}`;
14
+ const gracefulVersion = (v) => v.startsWith("v") ? v : `v${v}`;
14
15
  function generateNameAndAliasFromCommands(commands) {
15
16
  return Object.fromEntries(
16
17
  Object.entries(commands).map(([name, command]) => [name, [name, ...command.alias ? mustArray(command.alias) : []].join(", ")])
@@ -18,13 +19,22 @@ function generateNameAndAliasFromCommands(commands) {
18
19
  }
19
20
  function generateFlagNameAndAliasFromCommand(command) {
20
21
  return Object.fromEntries(
21
- Object.entries(command.flags || {}).map(([name, flag]) => [name, [name, ...mustArray(flag.alias || "")].map(gracefulFlagName).join(", ")])
22
+ Object.entries(command.flags || {}).map(
23
+ ([name, flag]) => {
24
+ const nameAndAlias = [name];
25
+ if (flag.alias) {
26
+ nameAndAlias.push(...mustArray(flag.alias));
27
+ }
28
+ return [name, nameAndAlias.map(gracefulFlagName).join(", ")];
29
+ }
30
+ )
22
31
  );
23
32
  }
24
33
  function getPadLength(strings) {
25
34
  const maxLength = Math.max(...strings.map((n) => n.length));
26
35
  return Math.floor((maxLength + 4) / 4) * 4;
27
36
  }
37
+ const mergeFlags = (ctx) => ({ ...ctx.flags, ...ctx.unknownFlags });
28
38
 
29
39
  const newline = () => {
30
40
  console.log();
@@ -37,14 +47,20 @@ const defaultOptions = {
37
47
  const helpPlugin = (_options) => clerc.definePlugin({
38
48
  setup(cli) {
39
49
  const { command, ...rest } = { ...defaultOptions, ..._options };
40
- if (command) {
41
- cli = cli.command("help", "Show help").on("help", (ctx) => {
42
- showHelp(ctx, rest);
43
- });
44
- }
45
- cli = cli.inspector((_ctx, next) => {
46
- const ctx = _ctx;
47
- if (_ctx.flags.h || _ctx.flags.help) {
50
+ cli.inspector((inspectorCtx, next) => {
51
+ if (command && !inspectorCtx.isSingleCommand) {
52
+ cli.command("help", "Show help").on("help", (ctx2) => {
53
+ showHelp(ctx2, rest);
54
+ });
55
+ }
56
+ next();
57
+ const ctx = inspectorCtx;
58
+ const flags = mergeFlags(ctx);
59
+ if (flags.h || flags.help) {
60
+ if (ctx.isSingleCommand) {
61
+ showSingleCommandHelp(ctx);
62
+ return;
63
+ }
48
64
  if (ctx.name === "help") {
49
65
  showSubcommandHelp({
50
66
  ...ctx,
@@ -59,7 +75,7 @@ const helpPlugin = (_options) => clerc.definePlugin({
59
75
  }
60
76
  return;
61
77
  }
62
- if (!ctx.resolved && ctx.parameters.length === 0 && Object.keys(ctx.flags).length === 0) {
78
+ if (!ctx.resolved && ctx.parameters.length === 0 && Object.keys(flags).length === 0) {
63
79
  showHelp(ctx, rest);
64
80
  return;
65
81
  }
@@ -74,7 +90,7 @@ function showHelp(ctx, { examples, notes }) {
74
90
  showSubcommandHelp(ctx);
75
91
  return;
76
92
  }
77
- cli._name && console.log(`${pc__default["default"].green(cli._name)} ${cli._version}`);
93
+ cli._name && console.log(`${pc__default["default"].green(cli._name)} ${gracefulVersion(cli._version)}`);
78
94
  if (cli._description) {
79
95
  console.log(cli._description);
80
96
  newline();
@@ -111,7 +127,7 @@ function showSubcommandHelp(ctx) {
111
127
  if (!commandToShowHelp) {
112
128
  throw new clerc.NoSuchCommandError(`No such command: ${commandName}`);
113
129
  }
114
- console.log(`${pc__default["default"].green(`${cli._name}.${commandToShowHelp.name}`)} ${cli._version}`);
130
+ console.log(`${pc__default["default"].green(`${cli._name}.${commandToShowHelp.name}`)} ${gracefulVersion(cli._version)}`);
115
131
  commandToShowHelp.description && console.log(commandToShowHelp.description);
116
132
  newline();
117
133
  console.log(pc__default["default"].yellow("USAGE:"));
@@ -126,5 +142,23 @@ function showSubcommandHelp(ctx) {
126
142
  }
127
143
  }
128
144
  }
145
+ function showSingleCommandHelp(ctx) {
146
+ const { cli } = ctx;
147
+ const singleCommand = cli._commands[clerc.SingleCommand];
148
+ console.log(`${pc__default["default"].green(`${cli._name} ${gracefulVersion(cli._version)}`)}`);
149
+ singleCommand.description && console.log(singleCommand.description);
150
+ newline();
151
+ console.log(pc__default["default"].yellow("USAGE:"));
152
+ console.log(` ${cli._name} [PARAMETERS] [FLAGS]`);
153
+ const flagNameAndAlias = generateFlagNameAndAliasFromCommand(singleCommand);
154
+ if (Object.keys(flagNameAndAlias).length > 0) {
155
+ newline();
156
+ console.log(pc__default["default"].yellow("FLAGS:"));
157
+ const flagsPadLength = getPadLength(Object.values(flagNameAndAlias));
158
+ for (const [name, nameAndAlias] of Object.entries(flagNameAndAlias)) {
159
+ console.log(` ${pc__default["default"].green(nameAndAlias.padEnd(flagsPadLength))}${singleCommand.flags[name].description}`);
160
+ }
161
+ }
162
+ }
129
163
 
130
164
  exports.helpPlugin = helpPlugin;
package/dist/index.mjs CHANGED
@@ -1,8 +1,9 @@
1
- import { definePlugin, resolveCommand, NoSuchCommandError } from 'clerc';
1
+ import { definePlugin, resolveCommand, NoSuchCommandError, SingleCommand } from 'clerc';
2
2
  import pc from 'picocolors';
3
3
 
4
4
  const mustArray = (a) => Array.isArray(a) ? a : [a];
5
5
  const gracefulFlagName = (n) => n.length <= 1 ? `-${n}` : `--${n}`;
6
+ const gracefulVersion = (v) => v.startsWith("v") ? v : `v${v}`;
6
7
  function generateNameAndAliasFromCommands(commands) {
7
8
  return Object.fromEntries(
8
9
  Object.entries(commands).map(([name, command]) => [name, [name, ...command.alias ? mustArray(command.alias) : []].join(", ")])
@@ -10,13 +11,22 @@ function generateNameAndAliasFromCommands(commands) {
10
11
  }
11
12
  function generateFlagNameAndAliasFromCommand(command) {
12
13
  return Object.fromEntries(
13
- Object.entries(command.flags || {}).map(([name, flag]) => [name, [name, ...mustArray(flag.alias || "")].map(gracefulFlagName).join(", ")])
14
+ Object.entries(command.flags || {}).map(
15
+ ([name, flag]) => {
16
+ const nameAndAlias = [name];
17
+ if (flag.alias) {
18
+ nameAndAlias.push(...mustArray(flag.alias));
19
+ }
20
+ return [name, nameAndAlias.map(gracefulFlagName).join(", ")];
21
+ }
22
+ )
14
23
  );
15
24
  }
16
25
  function getPadLength(strings) {
17
26
  const maxLength = Math.max(...strings.map((n) => n.length));
18
27
  return Math.floor((maxLength + 4) / 4) * 4;
19
28
  }
29
+ const mergeFlags = (ctx) => ({ ...ctx.flags, ...ctx.unknownFlags });
20
30
 
21
31
  const newline = () => {
22
32
  console.log();
@@ -29,14 +39,20 @@ const defaultOptions = {
29
39
  const helpPlugin = (_options) => definePlugin({
30
40
  setup(cli) {
31
41
  const { command, ...rest } = { ...defaultOptions, ..._options };
32
- if (command) {
33
- cli = cli.command("help", "Show help").on("help", (ctx) => {
34
- showHelp(ctx, rest);
35
- });
36
- }
37
- cli = cli.inspector((_ctx, next) => {
38
- const ctx = _ctx;
39
- if (_ctx.flags.h || _ctx.flags.help) {
42
+ cli.inspector((inspectorCtx, next) => {
43
+ if (command && !inspectorCtx.isSingleCommand) {
44
+ cli.command("help", "Show help").on("help", (ctx2) => {
45
+ showHelp(ctx2, rest);
46
+ });
47
+ }
48
+ next();
49
+ const ctx = inspectorCtx;
50
+ const flags = mergeFlags(ctx);
51
+ if (flags.h || flags.help) {
52
+ if (ctx.isSingleCommand) {
53
+ showSingleCommandHelp(ctx);
54
+ return;
55
+ }
40
56
  if (ctx.name === "help") {
41
57
  showSubcommandHelp({
42
58
  ...ctx,
@@ -51,7 +67,7 @@ const helpPlugin = (_options) => definePlugin({
51
67
  }
52
68
  return;
53
69
  }
54
- if (!ctx.resolved && ctx.parameters.length === 0 && Object.keys(ctx.flags).length === 0) {
70
+ if (!ctx.resolved && ctx.parameters.length === 0 && Object.keys(flags).length === 0) {
55
71
  showHelp(ctx, rest);
56
72
  return;
57
73
  }
@@ -66,7 +82,7 @@ function showHelp(ctx, { examples, notes }) {
66
82
  showSubcommandHelp(ctx);
67
83
  return;
68
84
  }
69
- cli._name && console.log(`${pc.green(cli._name)} ${cli._version}`);
85
+ cli._name && console.log(`${pc.green(cli._name)} ${gracefulVersion(cli._version)}`);
70
86
  if (cli._description) {
71
87
  console.log(cli._description);
72
88
  newline();
@@ -103,7 +119,7 @@ function showSubcommandHelp(ctx) {
103
119
  if (!commandToShowHelp) {
104
120
  throw new NoSuchCommandError(`No such command: ${commandName}`);
105
121
  }
106
- console.log(`${pc.green(`${cli._name}.${commandToShowHelp.name}`)} ${cli._version}`);
122
+ console.log(`${pc.green(`${cli._name}.${commandToShowHelp.name}`)} ${gracefulVersion(cli._version)}`);
107
123
  commandToShowHelp.description && console.log(commandToShowHelp.description);
108
124
  newline();
109
125
  console.log(pc.yellow("USAGE:"));
@@ -118,5 +134,23 @@ function showSubcommandHelp(ctx) {
118
134
  }
119
135
  }
120
136
  }
137
+ function showSingleCommandHelp(ctx) {
138
+ const { cli } = ctx;
139
+ const singleCommand = cli._commands[SingleCommand];
140
+ console.log(`${pc.green(`${cli._name} ${gracefulVersion(cli._version)}`)}`);
141
+ singleCommand.description && console.log(singleCommand.description);
142
+ newline();
143
+ console.log(pc.yellow("USAGE:"));
144
+ console.log(` ${cli._name} [PARAMETERS] [FLAGS]`);
145
+ const flagNameAndAlias = generateFlagNameAndAliasFromCommand(singleCommand);
146
+ if (Object.keys(flagNameAndAlias).length > 0) {
147
+ newline();
148
+ console.log(pc.yellow("FLAGS:"));
149
+ const flagsPadLength = getPadLength(Object.values(flagNameAndAlias));
150
+ for (const [name, nameAndAlias] of Object.entries(flagNameAndAlias)) {
151
+ console.log(` ${pc.green(nameAndAlias.padEnd(flagsPadLength))}${singleCommand.flags[name].description}`);
152
+ }
153
+ }
154
+ }
121
155
 
122
156
  export { helpPlugin };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clerc/plugin-help",
3
- "version": "0.3.3",
3
+ "version": "0.3.4",
4
4
  "author": "Ray <nn_201312@163.com> (https://github.com/so1ve)",
5
5
  "description": "Clerc plugin help",
6
6
  "keywords": [
@@ -43,7 +43,8 @@
43
43
  "clerc": "*"
44
44
  },
45
45
  "dependencies": {
46
- "clerc": "0.3.3",
46
+ "@clerc/utils": "0.3.4",
47
+ "clerc": "0.3.4",
47
48
  "picocolors": "^1.0.0"
48
49
  },
49
50
  "scripts": {