@clerc/plugin-help 0.3.4 → 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 +3 -3
- package/dist/index.cjs +0 -164
- package/dist/index.d.ts +0 -20
- package/dist/index.mjs +0 -156
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clerc/plugin-help",
|
|
3
|
-
"version": "0.
|
|
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,8 +43,8 @@
|
|
|
43
43
|
"clerc": "*"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@clerc/utils": "0.
|
|
47
|
-
"clerc": "0.
|
|
46
|
+
"@clerc/utils": "0.4.0",
|
|
47
|
+
"clerc": "0.4.0",
|
|
48
48
|
"picocolors": "^1.0.0"
|
|
49
49
|
},
|
|
50
50
|
"scripts": {
|
package/dist/index.cjs
DELETED
|
@@ -1,164 +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
|
-
const gracefulVersion = (v) => v.startsWith("v") ? v : `v${v}`;
|
|
15
|
-
function generateNameAndAliasFromCommands(commands) {
|
|
16
|
-
return Object.fromEntries(
|
|
17
|
-
Object.entries(commands).map(([name, command]) => [name, [name, ...command.alias ? mustArray(command.alias) : []].join(", ")])
|
|
18
|
-
);
|
|
19
|
-
}
|
|
20
|
-
function generateFlagNameAndAliasFromCommand(command) {
|
|
21
|
-
return Object.fromEntries(
|
|
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
|
-
)
|
|
31
|
-
);
|
|
32
|
-
}
|
|
33
|
-
function getPadLength(strings) {
|
|
34
|
-
const maxLength = Math.max(...strings.map((n) => n.length));
|
|
35
|
-
return Math.floor((maxLength + 4) / 4) * 4;
|
|
36
|
-
}
|
|
37
|
-
const mergeFlags = (ctx) => ({ ...ctx.flags, ...ctx.unknownFlags });
|
|
38
|
-
|
|
39
|
-
const newline = () => {
|
|
40
|
-
console.log();
|
|
41
|
-
};
|
|
42
|
-
const defaultOptions = {
|
|
43
|
-
command: true,
|
|
44
|
-
examples: [],
|
|
45
|
-
notes: []
|
|
46
|
-
};
|
|
47
|
-
const helpPlugin = (_options) => clerc.definePlugin({
|
|
48
|
-
setup(cli) {
|
|
49
|
-
const { command, ...rest } = { ...defaultOptions, ..._options };
|
|
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
|
-
}
|
|
64
|
-
if (ctx.name === "help") {
|
|
65
|
-
showSubcommandHelp({
|
|
66
|
-
...ctx,
|
|
67
|
-
name: "help"
|
|
68
|
-
});
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
if (ctx.resolved) {
|
|
72
|
-
showSubcommandHelp(ctx);
|
|
73
|
-
} else {
|
|
74
|
-
showHelp(ctx, rest);
|
|
75
|
-
}
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
if (!ctx.resolved && ctx.parameters.length === 0 && Object.keys(flags).length === 0) {
|
|
79
|
-
showHelp(ctx, rest);
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
next();
|
|
83
|
-
});
|
|
84
|
-
return cli;
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
function showHelp(ctx, { examples, notes }) {
|
|
88
|
-
const { cli } = ctx;
|
|
89
|
-
if (ctx.parameters.length > 0) {
|
|
90
|
-
showSubcommandHelp(ctx);
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
cli._name && console.log(`${pc__default["default"].green(cli._name)} ${gracefulVersion(cli._version)}`);
|
|
94
|
-
if (cli._description) {
|
|
95
|
-
console.log(cli._description);
|
|
96
|
-
newline();
|
|
97
|
-
}
|
|
98
|
-
console.log(pc__default["default"].yellow("USAGE:"));
|
|
99
|
-
console.log(` ${cli._name || "<CLI NAME>"} <SUBCOMMAND> [OPTIONS]`);
|
|
100
|
-
newline();
|
|
101
|
-
console.log(pc__default["default"].yellow("COMMANDS:"));
|
|
102
|
-
const commandNameAndAlias = generateNameAndAliasFromCommands(cli._commands);
|
|
103
|
-
const commandsPadLength = getPadLength(Object.values(commandNameAndAlias));
|
|
104
|
-
for (const [name, nameAndAlias] of Object.entries(commandNameAndAlias)) {
|
|
105
|
-
console.log(` ${pc__default["default"].green(nameAndAlias.padEnd(commandsPadLength))}${cli._commands[name].description}`);
|
|
106
|
-
}
|
|
107
|
-
if (examples.length > 0) {
|
|
108
|
-
newline();
|
|
109
|
-
console.log(pc__default["default"].yellow("EXAMPLES:"));
|
|
110
|
-
const examplesPadLength = getPadLength(examples.map((e) => e[0]));
|
|
111
|
-
for (const [exampleCommand, exampleDescription] of examples) {
|
|
112
|
-
console.log(` ${exampleCommand.padEnd(examplesPadLength)}${exampleDescription}`);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
if (notes.length > 0) {
|
|
116
|
-
newline();
|
|
117
|
-
console.log(pc__default["default"].yellow("NOTES:"));
|
|
118
|
-
for (const note of notes) {
|
|
119
|
-
console.log(` ${note}`);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
function showSubcommandHelp(ctx) {
|
|
124
|
-
const { cli } = ctx;
|
|
125
|
-
const commandName = String(ctx.name || ctx.parameters[0]);
|
|
126
|
-
const commandToShowHelp = clerc.resolveCommand(cli._commands, commandName);
|
|
127
|
-
if (!commandToShowHelp) {
|
|
128
|
-
throw new clerc.NoSuchCommandError(`No such command: ${commandName}`);
|
|
129
|
-
}
|
|
130
|
-
console.log(`${pc__default["default"].green(`${cli._name}.${commandToShowHelp.name}`)} ${gracefulVersion(cli._version)}`);
|
|
131
|
-
commandToShowHelp.description && console.log(commandToShowHelp.description);
|
|
132
|
-
newline();
|
|
133
|
-
console.log(pc__default["default"].yellow("USAGE:"));
|
|
134
|
-
console.log(` ${cli._name} ${commandToShowHelp.name} [PARAMETERS] [FLAGS]`);
|
|
135
|
-
const flagNameAndAlias = generateFlagNameAndAliasFromCommand(commandToShowHelp);
|
|
136
|
-
if (Object.keys(flagNameAndAlias).length > 0) {
|
|
137
|
-
newline();
|
|
138
|
-
console.log(pc__default["default"].yellow("FLAGS:"));
|
|
139
|
-
const flagsPadLength = getPadLength(Object.values(flagNameAndAlias));
|
|
140
|
-
for (const [name, nameAndAlias] of Object.entries(flagNameAndAlias)) {
|
|
141
|
-
console.log(` ${pc__default["default"].green(nameAndAlias.padEnd(flagsPadLength))}${commandToShowHelp.flags[name].description}`);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
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
|
-
}
|
|
163
|
-
|
|
164
|
-
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,156 +0,0 @@
|
|
|
1
|
-
import { definePlugin, resolveCommand, NoSuchCommandError, SingleCommand } 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
|
-
const gracefulVersion = (v) => v.startsWith("v") ? v : `v${v}`;
|
|
7
|
-
function generateNameAndAliasFromCommands(commands) {
|
|
8
|
-
return Object.fromEntries(
|
|
9
|
-
Object.entries(commands).map(([name, command]) => [name, [name, ...command.alias ? mustArray(command.alias) : []].join(", ")])
|
|
10
|
-
);
|
|
11
|
-
}
|
|
12
|
-
function generateFlagNameAndAliasFromCommand(command) {
|
|
13
|
-
return Object.fromEntries(
|
|
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
|
-
)
|
|
23
|
-
);
|
|
24
|
-
}
|
|
25
|
-
function getPadLength(strings) {
|
|
26
|
-
const maxLength = Math.max(...strings.map((n) => n.length));
|
|
27
|
-
return Math.floor((maxLength + 4) / 4) * 4;
|
|
28
|
-
}
|
|
29
|
-
const mergeFlags = (ctx) => ({ ...ctx.flags, ...ctx.unknownFlags });
|
|
30
|
-
|
|
31
|
-
const newline = () => {
|
|
32
|
-
console.log();
|
|
33
|
-
};
|
|
34
|
-
const defaultOptions = {
|
|
35
|
-
command: true,
|
|
36
|
-
examples: [],
|
|
37
|
-
notes: []
|
|
38
|
-
};
|
|
39
|
-
const helpPlugin = (_options) => definePlugin({
|
|
40
|
-
setup(cli) {
|
|
41
|
-
const { command, ...rest } = { ...defaultOptions, ..._options };
|
|
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
|
-
}
|
|
56
|
-
if (ctx.name === "help") {
|
|
57
|
-
showSubcommandHelp({
|
|
58
|
-
...ctx,
|
|
59
|
-
name: "help"
|
|
60
|
-
});
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
63
|
-
if (ctx.resolved) {
|
|
64
|
-
showSubcommandHelp(ctx);
|
|
65
|
-
} else {
|
|
66
|
-
showHelp(ctx, rest);
|
|
67
|
-
}
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
if (!ctx.resolved && ctx.parameters.length === 0 && Object.keys(flags).length === 0) {
|
|
71
|
-
showHelp(ctx, rest);
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
next();
|
|
75
|
-
});
|
|
76
|
-
return cli;
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
function showHelp(ctx, { examples, notes }) {
|
|
80
|
-
const { cli } = ctx;
|
|
81
|
-
if (ctx.parameters.length > 0) {
|
|
82
|
-
showSubcommandHelp(ctx);
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
cli._name && console.log(`${pc.green(cli._name)} ${gracefulVersion(cli._version)}`);
|
|
86
|
-
if (cli._description) {
|
|
87
|
-
console.log(cli._description);
|
|
88
|
-
newline();
|
|
89
|
-
}
|
|
90
|
-
console.log(pc.yellow("USAGE:"));
|
|
91
|
-
console.log(` ${cli._name || "<CLI NAME>"} <SUBCOMMAND> [OPTIONS]`);
|
|
92
|
-
newline();
|
|
93
|
-
console.log(pc.yellow("COMMANDS:"));
|
|
94
|
-
const commandNameAndAlias = generateNameAndAliasFromCommands(cli._commands);
|
|
95
|
-
const commandsPadLength = getPadLength(Object.values(commandNameAndAlias));
|
|
96
|
-
for (const [name, nameAndAlias] of Object.entries(commandNameAndAlias)) {
|
|
97
|
-
console.log(` ${pc.green(nameAndAlias.padEnd(commandsPadLength))}${cli._commands[name].description}`);
|
|
98
|
-
}
|
|
99
|
-
if (examples.length > 0) {
|
|
100
|
-
newline();
|
|
101
|
-
console.log(pc.yellow("EXAMPLES:"));
|
|
102
|
-
const examplesPadLength = getPadLength(examples.map((e) => e[0]));
|
|
103
|
-
for (const [exampleCommand, exampleDescription] of examples) {
|
|
104
|
-
console.log(` ${exampleCommand.padEnd(examplesPadLength)}${exampleDescription}`);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
if (notes.length > 0) {
|
|
108
|
-
newline();
|
|
109
|
-
console.log(pc.yellow("NOTES:"));
|
|
110
|
-
for (const note of notes) {
|
|
111
|
-
console.log(` ${note}`);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
function showSubcommandHelp(ctx) {
|
|
116
|
-
const { cli } = ctx;
|
|
117
|
-
const commandName = String(ctx.name || ctx.parameters[0]);
|
|
118
|
-
const commandToShowHelp = resolveCommand(cli._commands, commandName);
|
|
119
|
-
if (!commandToShowHelp) {
|
|
120
|
-
throw new NoSuchCommandError(`No such command: ${commandName}`);
|
|
121
|
-
}
|
|
122
|
-
console.log(`${pc.green(`${cli._name}.${commandToShowHelp.name}`)} ${gracefulVersion(cli._version)}`);
|
|
123
|
-
commandToShowHelp.description && console.log(commandToShowHelp.description);
|
|
124
|
-
newline();
|
|
125
|
-
console.log(pc.yellow("USAGE:"));
|
|
126
|
-
console.log(` ${cli._name} ${commandToShowHelp.name} [PARAMETERS] [FLAGS]`);
|
|
127
|
-
const flagNameAndAlias = generateFlagNameAndAliasFromCommand(commandToShowHelp);
|
|
128
|
-
if (Object.keys(flagNameAndAlias).length > 0) {
|
|
129
|
-
newline();
|
|
130
|
-
console.log(pc.yellow("FLAGS:"));
|
|
131
|
-
const flagsPadLength = getPadLength(Object.values(flagNameAndAlias));
|
|
132
|
-
for (const [name, nameAndAlias] of Object.entries(flagNameAndAlias)) {
|
|
133
|
-
console.log(` ${pc.green(nameAndAlias.padEnd(flagsPadLength))}${commandToShowHelp.flags[name].description}`);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
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
|
-
}
|
|
155
|
-
|
|
156
|
-
export { helpPlugin };
|