@clerc/plugin-help 0.4.0 → 0.6.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 +182 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.mjs +174 -0
- package/package.json +3 -3
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var clerc = require('clerc');
|
|
6
|
+
var utils = require('@clerc/utils');
|
|
7
|
+
var pc = require('picocolors');
|
|
8
|
+
|
|
9
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
10
|
+
|
|
11
|
+
var pc__default = /*#__PURE__*/_interopDefaultLegacy(pc);
|
|
12
|
+
|
|
13
|
+
function generateNameAndAliasFromCommands(commands) {
|
|
14
|
+
return Object.fromEntries(
|
|
15
|
+
Object.entries(commands).map(([name, command]) => [name, [name, ...command.alias ? utils.mustArray(command.alias) : []].join(", ")])
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
function generateFlagNameAndAliasFromCommand(command) {
|
|
19
|
+
return Object.fromEntries(
|
|
20
|
+
Object.entries(command.flags || {}).map(
|
|
21
|
+
([name, flag]) => {
|
|
22
|
+
const nameAndAlias = [name];
|
|
23
|
+
if (flag.alias) {
|
|
24
|
+
nameAndAlias.push(...utils.mustArray(flag.alias));
|
|
25
|
+
}
|
|
26
|
+
return [name, nameAndAlias.map(utils.gracefulFlagName).join(", ")];
|
|
27
|
+
}
|
|
28
|
+
)
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
function getPadLength(strings) {
|
|
32
|
+
const maxLength = Math.max(...strings.map((n) => n.length));
|
|
33
|
+
return Math.floor((maxLength + 4) / 4) * 4;
|
|
34
|
+
}
|
|
35
|
+
const mergeFlags = (ctx) => ({ ...ctx.flags, ...ctx.unknownFlags });
|
|
36
|
+
|
|
37
|
+
const newline = () => {
|
|
38
|
+
console.log();
|
|
39
|
+
};
|
|
40
|
+
const getExamples = (cli) => [
|
|
41
|
+
[`${cli._name} help`, "Displays help of the cli"],
|
|
42
|
+
[`${cli._name} -h`, "Displays help of the cli"],
|
|
43
|
+
[`${cli._name} help help`, "Displays help of the help command"]
|
|
44
|
+
];
|
|
45
|
+
const defaultOptions = {
|
|
46
|
+
command: true,
|
|
47
|
+
examples: [],
|
|
48
|
+
notes: []
|
|
49
|
+
};
|
|
50
|
+
const helpPlugin = (_options) => clerc.definePlugin({
|
|
51
|
+
setup(cli) {
|
|
52
|
+
const { command, ...rest } = { ...defaultOptions, ..._options };
|
|
53
|
+
cli.inspector((inspectorCtx, next) => {
|
|
54
|
+
if (command && !inspectorCtx.isSingleCommand) {
|
|
55
|
+
cli = cli.command("help", "Show help", {
|
|
56
|
+
examples: getExamples(cli)
|
|
57
|
+
}).on("help", (ctx2) => {
|
|
58
|
+
showHelp(ctx2, rest);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
const ctx = inspectorCtx;
|
|
62
|
+
const flags = mergeFlags(ctx);
|
|
63
|
+
if (flags.h || flags.help) {
|
|
64
|
+
if (ctx.isSingleCommand) {
|
|
65
|
+
showSingleCommandHelp(ctx);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (ctx.name === "help") {
|
|
69
|
+
showSubcommandHelp(ctx);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (ctx.resolved) {
|
|
73
|
+
showSubcommandHelp(ctx);
|
|
74
|
+
} else {
|
|
75
|
+
showHelp(ctx, rest);
|
|
76
|
+
}
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (!ctx.resolved && ctx.parameters.length === 0 && Object.keys(flags).length === 0) {
|
|
80
|
+
showHelp(ctx, rest);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
next();
|
|
84
|
+
});
|
|
85
|
+
return cli;
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
function showHelp(ctx, { examples, notes }) {
|
|
89
|
+
const { cli } = ctx;
|
|
90
|
+
if (ctx.parameters.length > 0) {
|
|
91
|
+
showSubcommandHelp(ctx);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
cli._name && console.log(`${pc__default["default"].green(cli._name)} ${utils.gracefulVersion(cli._version)}`);
|
|
95
|
+
if (cli._description) {
|
|
96
|
+
console.log(cli._description);
|
|
97
|
+
newline();
|
|
98
|
+
}
|
|
99
|
+
console.log(pc__default["default"].yellow("USAGE:"));
|
|
100
|
+
console.log(` ${cli._name || "<CLI NAME>"} <SUBCOMMAND> [OPTIONS]`);
|
|
101
|
+
newline();
|
|
102
|
+
console.log(pc__default["default"].yellow("COMMANDS:"));
|
|
103
|
+
const commandNameAndAlias = generateNameAndAliasFromCommands(utils.generateCommandRecordFromCommandArray(clerc.resolveRootCommands(cli._commands)));
|
|
104
|
+
const commandsPadLength = getPadLength(Object.values(commandNameAndAlias));
|
|
105
|
+
for (const [name, nameAndAlias] of Object.entries(commandNameAndAlias)) {
|
|
106
|
+
console.log(` ${pc__default["default"].green(nameAndAlias.padEnd(commandsPadLength))}${cli._commands[name].description}`);
|
|
107
|
+
}
|
|
108
|
+
if (examples.length > 0) {
|
|
109
|
+
newline();
|
|
110
|
+
console.log(pc__default["default"].yellow("EXAMPLES:"));
|
|
111
|
+
const examplesPadLength = getPadLength(examples.map((e) => e[0]));
|
|
112
|
+
for (const [exampleCommand, exampleDescription] of examples) {
|
|
113
|
+
console.log(` ${exampleCommand.padEnd(examplesPadLength)}${exampleDescription}`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
showCommandNotes(notes);
|
|
117
|
+
}
|
|
118
|
+
function showCommandExamples(examples) {
|
|
119
|
+
if (examples && examples.length > 0) {
|
|
120
|
+
newline();
|
|
121
|
+
console.log(pc__default["default"].yellow("EXAMPLES:"));
|
|
122
|
+
const examplesPadLength = getPadLength(examples.map((e) => e[0]));
|
|
123
|
+
for (const [exampleCommand, exampleDescription] of examples) {
|
|
124
|
+
console.log(` ${exampleCommand.padEnd(examplesPadLength)}${exampleDescription}`);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
function showCommandNotes(notes) {
|
|
129
|
+
if (notes && notes.length > 0) {
|
|
130
|
+
newline();
|
|
131
|
+
console.log(pc__default["default"].yellow("NOTES:"));
|
|
132
|
+
for (const note of notes) {
|
|
133
|
+
console.log(` ${note}`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
function showSubcommandHelp(ctx) {
|
|
138
|
+
const { cli } = ctx;
|
|
139
|
+
const commandName = ctx.parameters.map(String);
|
|
140
|
+
const commandToShowHelp = clerc.resolveCommand(cli._commands, commandName);
|
|
141
|
+
if (!commandToShowHelp) {
|
|
142
|
+
throw new clerc.NoSuchCommandError(commandName.join(" "));
|
|
143
|
+
}
|
|
144
|
+
console.log(`${pc__default["default"].green(`${cli._name} ${commandToShowHelp.name}`)} ${utils.gracefulVersion(cli._version)}`);
|
|
145
|
+
commandToShowHelp.description && console.log(commandToShowHelp.description);
|
|
146
|
+
newline();
|
|
147
|
+
console.log(pc__default["default"].yellow("USAGE:"));
|
|
148
|
+
console.log(` ${cli._name} ${commandToShowHelp.name} [PARAMETERS] [FLAGS]`);
|
|
149
|
+
const flagNameAndAlias = generateFlagNameAndAliasFromCommand(commandToShowHelp);
|
|
150
|
+
if (Object.keys(flagNameAndAlias).length > 0) {
|
|
151
|
+
newline();
|
|
152
|
+
console.log(pc__default["default"].yellow("FLAGS:"));
|
|
153
|
+
const flagsPadLength = getPadLength(Object.values(flagNameAndAlias));
|
|
154
|
+
for (const [name, nameAndAlias] of Object.entries(flagNameAndAlias)) {
|
|
155
|
+
console.log(` ${pc__default["default"].green(nameAndAlias.padEnd(flagsPadLength))}${commandToShowHelp.flags[name].description}`);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
showCommandExamples(commandToShowHelp.examples);
|
|
159
|
+
showCommandNotes(commandToShowHelp.notes);
|
|
160
|
+
}
|
|
161
|
+
function showSingleCommandHelp(ctx) {
|
|
162
|
+
const { cli } = ctx;
|
|
163
|
+
const singleCommand = cli._commands[clerc.SingleCommand];
|
|
164
|
+
console.log(`${pc__default["default"].green(`${cli._name} ${utils.gracefulVersion(cli._version)}`)}`);
|
|
165
|
+
singleCommand.description && console.log(singleCommand.description);
|
|
166
|
+
newline();
|
|
167
|
+
console.log(pc__default["default"].yellow("USAGE:"));
|
|
168
|
+
console.log(` ${cli._name} [PARAMETERS] [FLAGS]`);
|
|
169
|
+
const flagNameAndAlias = generateFlagNameAndAliasFromCommand(singleCommand);
|
|
170
|
+
if (Object.keys(flagNameAndAlias).length > 0) {
|
|
171
|
+
newline();
|
|
172
|
+
console.log(pc__default["default"].yellow("FLAGS:"));
|
|
173
|
+
const flagsPadLength = getPadLength(Object.values(flagNameAndAlias));
|
|
174
|
+
for (const [name, nameAndAlias] of Object.entries(flagNameAndAlias)) {
|
|
175
|
+
console.log(` ${pc__default["default"].green(nameAndAlias.padEnd(flagsPadLength))}${singleCommand.flags[name].description}`);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
showCommandExamples(singleCommand.examples);
|
|
179
|
+
showCommandNotes(singleCommand.notes);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
exports.helpPlugin = helpPlugin;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import * as clerc from 'clerc';
|
|
2
|
+
import { Clerc } from 'clerc';
|
|
3
|
+
|
|
4
|
+
interface Options {
|
|
5
|
+
/**
|
|
6
|
+
* Register a help command or not.
|
|
7
|
+
*/
|
|
8
|
+
command?: boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Examples
|
|
11
|
+
* Syntax: [example command, description]
|
|
12
|
+
*/
|
|
13
|
+
examples?: [string, string][];
|
|
14
|
+
/**
|
|
15
|
+
* Notes
|
|
16
|
+
*/
|
|
17
|
+
notes?: string[];
|
|
18
|
+
}
|
|
19
|
+
declare const helpPlugin: (_options?: Options) => clerc.Plugin<Clerc<{}>, Clerc<{}>>;
|
|
20
|
+
|
|
21
|
+
export { helpPlugin };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { definePlugin, resolveRootCommands, resolveCommand, NoSuchCommandError, SingleCommand } from 'clerc';
|
|
2
|
+
import { mustArray, gracefulFlagName, gracefulVersion, generateCommandRecordFromCommandArray } from '@clerc/utils';
|
|
3
|
+
import pc from 'picocolors';
|
|
4
|
+
|
|
5
|
+
function generateNameAndAliasFromCommands(commands) {
|
|
6
|
+
return Object.fromEntries(
|
|
7
|
+
Object.entries(commands).map(([name, command]) => [name, [name, ...command.alias ? mustArray(command.alias) : []].join(", ")])
|
|
8
|
+
);
|
|
9
|
+
}
|
|
10
|
+
function generateFlagNameAndAliasFromCommand(command) {
|
|
11
|
+
return Object.fromEntries(
|
|
12
|
+
Object.entries(command.flags || {}).map(
|
|
13
|
+
([name, flag]) => {
|
|
14
|
+
const nameAndAlias = [name];
|
|
15
|
+
if (flag.alias) {
|
|
16
|
+
nameAndAlias.push(...mustArray(flag.alias));
|
|
17
|
+
}
|
|
18
|
+
return [name, nameAndAlias.map(gracefulFlagName).join(", ")];
|
|
19
|
+
}
|
|
20
|
+
)
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
function getPadLength(strings) {
|
|
24
|
+
const maxLength = Math.max(...strings.map((n) => n.length));
|
|
25
|
+
return Math.floor((maxLength + 4) / 4) * 4;
|
|
26
|
+
}
|
|
27
|
+
const mergeFlags = (ctx) => ({ ...ctx.flags, ...ctx.unknownFlags });
|
|
28
|
+
|
|
29
|
+
const newline = () => {
|
|
30
|
+
console.log();
|
|
31
|
+
};
|
|
32
|
+
const getExamples = (cli) => [
|
|
33
|
+
[`${cli._name} help`, "Displays help of the cli"],
|
|
34
|
+
[`${cli._name} -h`, "Displays help of the cli"],
|
|
35
|
+
[`${cli._name} help help`, "Displays help of the help command"]
|
|
36
|
+
];
|
|
37
|
+
const defaultOptions = {
|
|
38
|
+
command: true,
|
|
39
|
+
examples: [],
|
|
40
|
+
notes: []
|
|
41
|
+
};
|
|
42
|
+
const helpPlugin = (_options) => definePlugin({
|
|
43
|
+
setup(cli) {
|
|
44
|
+
const { command, ...rest } = { ...defaultOptions, ..._options };
|
|
45
|
+
cli.inspector((inspectorCtx, next) => {
|
|
46
|
+
if (command && !inspectorCtx.isSingleCommand) {
|
|
47
|
+
cli = cli.command("help", "Show help", {
|
|
48
|
+
examples: getExamples(cli)
|
|
49
|
+
}).on("help", (ctx2) => {
|
|
50
|
+
showHelp(ctx2, rest);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
const ctx = inspectorCtx;
|
|
54
|
+
const flags = mergeFlags(ctx);
|
|
55
|
+
if (flags.h || flags.help) {
|
|
56
|
+
if (ctx.isSingleCommand) {
|
|
57
|
+
showSingleCommandHelp(ctx);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
if (ctx.name === "help") {
|
|
61
|
+
showSubcommandHelp(ctx);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (ctx.resolved) {
|
|
65
|
+
showSubcommandHelp(ctx);
|
|
66
|
+
} else {
|
|
67
|
+
showHelp(ctx, rest);
|
|
68
|
+
}
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (!ctx.resolved && ctx.parameters.length === 0 && Object.keys(flags).length === 0) {
|
|
72
|
+
showHelp(ctx, rest);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
next();
|
|
76
|
+
});
|
|
77
|
+
return cli;
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
function showHelp(ctx, { examples, notes }) {
|
|
81
|
+
const { cli } = ctx;
|
|
82
|
+
if (ctx.parameters.length > 0) {
|
|
83
|
+
showSubcommandHelp(ctx);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
cli._name && console.log(`${pc.green(cli._name)} ${gracefulVersion(cli._version)}`);
|
|
87
|
+
if (cli._description) {
|
|
88
|
+
console.log(cli._description);
|
|
89
|
+
newline();
|
|
90
|
+
}
|
|
91
|
+
console.log(pc.yellow("USAGE:"));
|
|
92
|
+
console.log(` ${cli._name || "<CLI NAME>"} <SUBCOMMAND> [OPTIONS]`);
|
|
93
|
+
newline();
|
|
94
|
+
console.log(pc.yellow("COMMANDS:"));
|
|
95
|
+
const commandNameAndAlias = generateNameAndAliasFromCommands(generateCommandRecordFromCommandArray(resolveRootCommands(cli._commands)));
|
|
96
|
+
const commandsPadLength = getPadLength(Object.values(commandNameAndAlias));
|
|
97
|
+
for (const [name, nameAndAlias] of Object.entries(commandNameAndAlias)) {
|
|
98
|
+
console.log(` ${pc.green(nameAndAlias.padEnd(commandsPadLength))}${cli._commands[name].description}`);
|
|
99
|
+
}
|
|
100
|
+
if (examples.length > 0) {
|
|
101
|
+
newline();
|
|
102
|
+
console.log(pc.yellow("EXAMPLES:"));
|
|
103
|
+
const examplesPadLength = getPadLength(examples.map((e) => e[0]));
|
|
104
|
+
for (const [exampleCommand, exampleDescription] of examples) {
|
|
105
|
+
console.log(` ${exampleCommand.padEnd(examplesPadLength)}${exampleDescription}`);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
showCommandNotes(notes);
|
|
109
|
+
}
|
|
110
|
+
function showCommandExamples(examples) {
|
|
111
|
+
if (examples && examples.length > 0) {
|
|
112
|
+
newline();
|
|
113
|
+
console.log(pc.yellow("EXAMPLES:"));
|
|
114
|
+
const examplesPadLength = getPadLength(examples.map((e) => e[0]));
|
|
115
|
+
for (const [exampleCommand, exampleDescription] of examples) {
|
|
116
|
+
console.log(` ${exampleCommand.padEnd(examplesPadLength)}${exampleDescription}`);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function showCommandNotes(notes) {
|
|
121
|
+
if (notes && notes.length > 0) {
|
|
122
|
+
newline();
|
|
123
|
+
console.log(pc.yellow("NOTES:"));
|
|
124
|
+
for (const note of notes) {
|
|
125
|
+
console.log(` ${note}`);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
function showSubcommandHelp(ctx) {
|
|
130
|
+
const { cli } = ctx;
|
|
131
|
+
const commandName = ctx.parameters.map(String);
|
|
132
|
+
const commandToShowHelp = resolveCommand(cli._commands, commandName);
|
|
133
|
+
if (!commandToShowHelp) {
|
|
134
|
+
throw new NoSuchCommandError(commandName.join(" "));
|
|
135
|
+
}
|
|
136
|
+
console.log(`${pc.green(`${cli._name} ${commandToShowHelp.name}`)} ${gracefulVersion(cli._version)}`);
|
|
137
|
+
commandToShowHelp.description && console.log(commandToShowHelp.description);
|
|
138
|
+
newline();
|
|
139
|
+
console.log(pc.yellow("USAGE:"));
|
|
140
|
+
console.log(` ${cli._name} ${commandToShowHelp.name} [PARAMETERS] [FLAGS]`);
|
|
141
|
+
const flagNameAndAlias = generateFlagNameAndAliasFromCommand(commandToShowHelp);
|
|
142
|
+
if (Object.keys(flagNameAndAlias).length > 0) {
|
|
143
|
+
newline();
|
|
144
|
+
console.log(pc.yellow("FLAGS:"));
|
|
145
|
+
const flagsPadLength = getPadLength(Object.values(flagNameAndAlias));
|
|
146
|
+
for (const [name, nameAndAlias] of Object.entries(flagNameAndAlias)) {
|
|
147
|
+
console.log(` ${pc.green(nameAndAlias.padEnd(flagsPadLength))}${commandToShowHelp.flags[name].description}`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
showCommandExamples(commandToShowHelp.examples);
|
|
151
|
+
showCommandNotes(commandToShowHelp.notes);
|
|
152
|
+
}
|
|
153
|
+
function showSingleCommandHelp(ctx) {
|
|
154
|
+
const { cli } = ctx;
|
|
155
|
+
const singleCommand = cli._commands[SingleCommand];
|
|
156
|
+
console.log(`${pc.green(`${cli._name} ${gracefulVersion(cli._version)}`)}`);
|
|
157
|
+
singleCommand.description && console.log(singleCommand.description);
|
|
158
|
+
newline();
|
|
159
|
+
console.log(pc.yellow("USAGE:"));
|
|
160
|
+
console.log(` ${cli._name} [PARAMETERS] [FLAGS]`);
|
|
161
|
+
const flagNameAndAlias = generateFlagNameAndAliasFromCommand(singleCommand);
|
|
162
|
+
if (Object.keys(flagNameAndAlias).length > 0) {
|
|
163
|
+
newline();
|
|
164
|
+
console.log(pc.yellow("FLAGS:"));
|
|
165
|
+
const flagsPadLength = getPadLength(Object.values(flagNameAndAlias));
|
|
166
|
+
for (const [name, nameAndAlias] of Object.entries(flagNameAndAlias)) {
|
|
167
|
+
console.log(` ${pc.green(nameAndAlias.padEnd(flagsPadLength))}${singleCommand.flags[name].description}`);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
showCommandExamples(singleCommand.examples);
|
|
171
|
+
showCommandNotes(singleCommand.notes);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export { helpPlugin };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clerc/plugin-help",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"author": "Ray <nn_201312@163.com> (https://github.com/so1ve)",
|
|
5
5
|
"description": "Clerc plugin help",
|
|
6
6
|
"keywords": [
|
|
@@ -43,8 +43,8 @@
|
|
|
43
43
|
"clerc": "*"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@clerc/utils": "0.
|
|
47
|
-
"clerc": "0.
|
|
46
|
+
"@clerc/utils": "0.6.0",
|
|
47
|
+
"clerc": "0.6.0",
|
|
48
48
|
"picocolors": "^1.0.0"
|
|
49
49
|
},
|
|
50
50
|
"scripts": {
|