@clerc/plugin-help 0.3.3 → 0.4.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clerc/plugin-help",
3
- "version": "0.3.3",
3
+ "version": "0.4.0",
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.4.0",
47
+ "clerc": "0.4.0",
47
48
  "picocolors": "^1.0.0"
48
49
  },
49
50
  "scripts": {
package/dist/index.cjs DELETED
@@ -1,130 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var clerc = require('clerc');
6
- var pc = require('picocolors');
7
-
8
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
9
-
10
- var pc__default = /*#__PURE__*/_interopDefaultLegacy(pc);
11
-
12
- const mustArray = (a) => Array.isArray(a) ? a : [a];
13
- const gracefulFlagName = (n) => n.length <= 1 ? `-${n}` : `--${n}`;
14
- function generateNameAndAliasFromCommands(commands) {
15
- return Object.fromEntries(
16
- Object.entries(commands).map(([name, command]) => [name, [name, ...command.alias ? mustArray(command.alias) : []].join(", ")])
17
- );
18
- }
19
- function generateFlagNameAndAliasFromCommand(command) {
20
- return Object.fromEntries(
21
- Object.entries(command.flags || {}).map(([name, flag]) => [name, [name, ...mustArray(flag.alias || "")].map(gracefulFlagName).join(", ")])
22
- );
23
- }
24
- function getPadLength(strings) {
25
- const maxLength = Math.max(...strings.map((n) => n.length));
26
- return Math.floor((maxLength + 4) / 4) * 4;
27
- }
28
-
29
- const newline = () => {
30
- console.log();
31
- };
32
- const defaultOptions = {
33
- command: true,
34
- examples: [],
35
- notes: []
36
- };
37
- const helpPlugin = (_options) => clerc.definePlugin({
38
- setup(cli) {
39
- 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) {
48
- if (ctx.name === "help") {
49
- showSubcommandHelp({
50
- ...ctx,
51
- name: "help"
52
- });
53
- return;
54
- }
55
- if (ctx.resolved) {
56
- showSubcommandHelp(ctx);
57
- } else {
58
- showHelp(ctx, rest);
59
- }
60
- return;
61
- }
62
- if (!ctx.resolved && ctx.parameters.length === 0 && Object.keys(ctx.flags).length === 0) {
63
- showHelp(ctx, rest);
64
- return;
65
- }
66
- next();
67
- });
68
- return cli;
69
- }
70
- });
71
- function showHelp(ctx, { examples, notes }) {
72
- const { cli } = ctx;
73
- if (ctx.parameters.length > 0) {
74
- showSubcommandHelp(ctx);
75
- return;
76
- }
77
- cli._name && console.log(`${pc__default["default"].green(cli._name)} ${cli._version}`);
78
- if (cli._description) {
79
- console.log(cli._description);
80
- newline();
81
- }
82
- console.log(pc__default["default"].yellow("USAGE:"));
83
- console.log(` ${cli._name || "<CLI NAME>"} <SUBCOMMAND> [OPTIONS]`);
84
- newline();
85
- console.log(pc__default["default"].yellow("COMMANDS:"));
86
- const commandNameAndAlias = generateNameAndAliasFromCommands(cli._commands);
87
- const commandsPadLength = getPadLength(Object.values(commandNameAndAlias));
88
- for (const [name, nameAndAlias] of Object.entries(commandNameAndAlias)) {
89
- console.log(` ${pc__default["default"].green(nameAndAlias.padEnd(commandsPadLength))}${cli._commands[name].description}`);
90
- }
91
- if (examples.length > 0) {
92
- newline();
93
- console.log(pc__default["default"].yellow("EXAMPLES:"));
94
- const examplesPadLength = getPadLength(examples.map((e) => e[0]));
95
- for (const [exampleCommand, exampleDescription] of examples) {
96
- console.log(` ${exampleCommand.padEnd(examplesPadLength)}${exampleDescription}`);
97
- }
98
- }
99
- if (notes.length > 0) {
100
- newline();
101
- console.log(pc__default["default"].yellow("NOTES:"));
102
- for (const note of notes) {
103
- console.log(` ${note}`);
104
- }
105
- }
106
- }
107
- function showSubcommandHelp(ctx) {
108
- const { cli } = ctx;
109
- const commandName = String(ctx.name || ctx.parameters[0]);
110
- const commandToShowHelp = clerc.resolveCommand(cli._commands, commandName);
111
- if (!commandToShowHelp) {
112
- throw new clerc.NoSuchCommandError(`No such command: ${commandName}`);
113
- }
114
- console.log(`${pc__default["default"].green(`${cli._name}.${commandToShowHelp.name}`)} ${cli._version}`);
115
- commandToShowHelp.description && console.log(commandToShowHelp.description);
116
- newline();
117
- console.log(pc__default["default"].yellow("USAGE:"));
118
- console.log(` ${cli._name} ${commandToShowHelp.name} [PARAMETERS] [FLAGS]`);
119
- const flagNameAndAlias = generateFlagNameAndAliasFromCommand(commandToShowHelp);
120
- if (Object.keys(flagNameAndAlias).length > 0) {
121
- newline();
122
- console.log(pc__default["default"].yellow("FLAGS:"));
123
- const flagsPadLength = getPadLength(Object.values(flagNameAndAlias));
124
- for (const [name, nameAndAlias] of Object.entries(flagNameAndAlias)) {
125
- console.log(` ${pc__default["default"].green(nameAndAlias.padEnd(flagsPadLength))}${commandToShowHelp.flags[name].description}`);
126
- }
127
- }
128
- }
129
-
130
- exports.helpPlugin = helpPlugin;
package/dist/index.d.ts DELETED
@@ -1,20 +0,0 @@
1
- import * as clerc from 'clerc';
2
-
3
- interface Options {
4
- /**
5
- * Register a help command or not.
6
- */
7
- command?: boolean;
8
- /**
9
- * Examples
10
- * Syntax: [example command, description]
11
- */
12
- examples?: [string, string][];
13
- /**
14
- * Notes
15
- */
16
- notes?: string[];
17
- }
18
- declare const helpPlugin: (_options?: Options) => clerc.Plugin<clerc.Clerc<{}>, clerc.Clerc<{}>>;
19
-
20
- export { helpPlugin };
package/dist/index.mjs DELETED
@@ -1,122 +0,0 @@
1
- import { definePlugin, resolveCommand, NoSuchCommandError } from 'clerc';
2
- import pc from 'picocolors';
3
-
4
- const mustArray = (a) => Array.isArray(a) ? a : [a];
5
- const gracefulFlagName = (n) => n.length <= 1 ? `-${n}` : `--${n}`;
6
- function generateNameAndAliasFromCommands(commands) {
7
- return Object.fromEntries(
8
- Object.entries(commands).map(([name, command]) => [name, [name, ...command.alias ? mustArray(command.alias) : []].join(", ")])
9
- );
10
- }
11
- function generateFlagNameAndAliasFromCommand(command) {
12
- return Object.fromEntries(
13
- Object.entries(command.flags || {}).map(([name, flag]) => [name, [name, ...mustArray(flag.alias || "")].map(gracefulFlagName).join(", ")])
14
- );
15
- }
16
- function getPadLength(strings) {
17
- const maxLength = Math.max(...strings.map((n) => n.length));
18
- return Math.floor((maxLength + 4) / 4) * 4;
19
- }
20
-
21
- const newline = () => {
22
- console.log();
23
- };
24
- const defaultOptions = {
25
- command: true,
26
- examples: [],
27
- notes: []
28
- };
29
- const helpPlugin = (_options) => definePlugin({
30
- setup(cli) {
31
- 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) {
40
- if (ctx.name === "help") {
41
- showSubcommandHelp({
42
- ...ctx,
43
- name: "help"
44
- });
45
- return;
46
- }
47
- if (ctx.resolved) {
48
- showSubcommandHelp(ctx);
49
- } else {
50
- showHelp(ctx, rest);
51
- }
52
- return;
53
- }
54
- if (!ctx.resolved && ctx.parameters.length === 0 && Object.keys(ctx.flags).length === 0) {
55
- showHelp(ctx, rest);
56
- return;
57
- }
58
- next();
59
- });
60
- return cli;
61
- }
62
- });
63
- function showHelp(ctx, { examples, notes }) {
64
- const { cli } = ctx;
65
- if (ctx.parameters.length > 0) {
66
- showSubcommandHelp(ctx);
67
- return;
68
- }
69
- cli._name && console.log(`${pc.green(cli._name)} ${cli._version}`);
70
- if (cli._description) {
71
- console.log(cli._description);
72
- newline();
73
- }
74
- console.log(pc.yellow("USAGE:"));
75
- console.log(` ${cli._name || "<CLI NAME>"} <SUBCOMMAND> [OPTIONS]`);
76
- newline();
77
- console.log(pc.yellow("COMMANDS:"));
78
- const commandNameAndAlias = generateNameAndAliasFromCommands(cli._commands);
79
- const commandsPadLength = getPadLength(Object.values(commandNameAndAlias));
80
- for (const [name, nameAndAlias] of Object.entries(commandNameAndAlias)) {
81
- console.log(` ${pc.green(nameAndAlias.padEnd(commandsPadLength))}${cli._commands[name].description}`);
82
- }
83
- if (examples.length > 0) {
84
- newline();
85
- console.log(pc.yellow("EXAMPLES:"));
86
- const examplesPadLength = getPadLength(examples.map((e) => e[0]));
87
- for (const [exampleCommand, exampleDescription] of examples) {
88
- console.log(` ${exampleCommand.padEnd(examplesPadLength)}${exampleDescription}`);
89
- }
90
- }
91
- if (notes.length > 0) {
92
- newline();
93
- console.log(pc.yellow("NOTES:"));
94
- for (const note of notes) {
95
- console.log(` ${note}`);
96
- }
97
- }
98
- }
99
- function showSubcommandHelp(ctx) {
100
- const { cli } = ctx;
101
- const commandName = String(ctx.name || ctx.parameters[0]);
102
- const commandToShowHelp = resolveCommand(cli._commands, commandName);
103
- if (!commandToShowHelp) {
104
- throw new NoSuchCommandError(`No such command: ${commandName}`);
105
- }
106
- console.log(`${pc.green(`${cli._name}.${commandToShowHelp.name}`)} ${cli._version}`);
107
- commandToShowHelp.description && console.log(commandToShowHelp.description);
108
- newline();
109
- console.log(pc.yellow("USAGE:"));
110
- console.log(` ${cli._name} ${commandToShowHelp.name} [PARAMETERS] [FLAGS]`);
111
- const flagNameAndAlias = generateFlagNameAndAliasFromCommand(commandToShowHelp);
112
- if (Object.keys(flagNameAndAlias).length > 0) {
113
- newline();
114
- console.log(pc.yellow("FLAGS:"));
115
- const flagsPadLength = getPadLength(Object.values(flagNameAndAlias));
116
- for (const [name, nameAndAlias] of Object.entries(flagNameAndAlias)) {
117
- console.log(` ${pc.green(nameAndAlias.padEnd(flagsPadLength))}${commandToShowHelp.flags[name].description}`);
118
- }
119
- }
120
- }
121
-
122
- export { helpPlugin };