@clerc/plugin-help 0.18.0 → 0.20.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.js CHANGED
@@ -1,4 +1,4 @@
1
- import { definePlugin, resolveCommand, NoSuchCommandError, SingleCommand } from '@clerc/core';
1
+ import { definePlugin, SingleCommand, resolveCommand, NoSuchCommandError } from '@clerc/core';
2
2
  import { toArray, gracefulFlagName } from '@clerc/utils';
3
3
  import pc from 'picocolors';
4
4
  import getFuncName from 'get-func-name';
@@ -39,7 +39,7 @@ const render = (sections) => {
39
39
  const rendered = [];
40
40
  for (const section of sections) {
41
41
  if (section.type === "block" || !section.type) {
42
- const indent = " ".repeat(4);
42
+ const indent = " ";
43
43
  const formattedBody = section.body.map((line) => indent + line);
44
44
  formattedBody.unshift("");
45
45
  const body = formattedBody.join("\n");
@@ -55,6 +55,9 @@ const render = (sections) => {
55
55
  };
56
56
 
57
57
  const DELIMITER = pc.yellow("-");
58
+ const print = (s) => {
59
+ process.stdout.write(s);
60
+ };
58
61
  const formatCommandName = (name) => Array.isArray(name) ? name.join(" ") : typeof name === "string" ? name : "<Single Command>";
59
62
  const generateCliDetail = (sections, cli, subcommand) => {
60
63
  const items = [
@@ -70,7 +73,7 @@ const generateCliDetail = (sections, cli, subcommand) => {
70
73
  if (subcommand) {
71
74
  items.push({
72
75
  title: "Subcommand:",
73
- body: pc.green(formatCommandName(subcommand.name))
76
+ body: pc.green(`${cli._name} ${formatCommandName(subcommand.name)}`)
74
77
  });
75
78
  }
76
79
  sections.push({
@@ -89,30 +92,22 @@ const generateExamples = (sections, examples) => {
89
92
  body: splitTable(...examplesFormatted)
90
93
  });
91
94
  };
92
- const showHelp = (ctx, notes, examples) => {
95
+ const generateHelp = (ctx, notes, examples) => {
93
96
  const { cli } = ctx;
94
97
  const sections = [];
95
98
  generateCliDetail(sections, cli);
96
- if (ctx.isSingleCommand) {
97
- sections.push({
98
- title: "Usage:",
99
- body: [pc.magenta(`$ ${cli._name} [flags]`)]
100
- });
101
- } else {
102
- sections.push({
103
- title: "Usage:",
104
- body: [pc.magenta(`$ ${cli._name} [command] [flags]`)]
105
- });
106
- }
107
- if (!ctx.isSingleCommand) {
108
- sections.push({
109
- title: "Commands:",
110
- body: splitTable(...Object.values(cli._commands).map((command) => {
111
- const commandNameWithAlias = [command.name, ...toArray(command.alias || [])].join(", ");
112
- return [pc.cyan(commandNameWithAlias), DELIMITER, command.description];
113
- }))
114
- });
115
- }
99
+ sections.push({
100
+ title: "Usage:",
101
+ body: [pc.magenta(`$ ${cli._name} [command] [flags]`)]
102
+ });
103
+ const commands = [...ctx.hasSingleCommand ? [cli._commands[SingleCommand]] : [], ...Object.values(cli._commands)].map((command) => {
104
+ const commandNameWithAlias = [typeof command.name === "symbol" ? "" : command.name, ...toArray(command.alias || [])].map((n) => `${cli._name} ${n}`).join(", ");
105
+ return [pc.cyan(commandNameWithAlias), DELIMITER, command.description];
106
+ });
107
+ sections.push({
108
+ title: "Commands:",
109
+ body: splitTable(...commands)
110
+ });
116
111
  if (notes) {
117
112
  sections.push({
118
113
  title: "Notes:",
@@ -122,21 +117,21 @@ const showHelp = (ctx, notes, examples) => {
122
117
  if (examples) {
123
118
  generateExamples(sections, examples);
124
119
  }
125
- process.stdout.write(render(sections));
120
+ return render(sections);
126
121
  };
127
- const showSubcommandHelp = (ctx, command) => {
122
+ const generateSubcommandHelp = (ctx, command) => {
128
123
  var _a;
129
124
  const { cli } = ctx;
130
125
  const subcommand = resolveCommand(cli._commands, command);
131
- if (!subcommand) {
126
+ if (!subcommand || subcommand.name === SingleCommand) {
132
127
  throw new NoSuchCommandError(formatCommandName(command));
133
128
  }
134
129
  const sections = [];
135
- generateCliDetail(sections, cli, ctx.isSingleCommand ? void 0 : subcommand);
130
+ generateCliDetail(sections, cli, subcommand);
136
131
  const parameters = ((_a = subcommand.parameters) == null ? void 0 : _a.join(" ")) || void 0;
137
132
  sections.push({
138
133
  title: "Usage:",
139
- body: [pc.magenta(`$ ${cli._name}${ctx.isSingleCommand ? "" : ` ${formatCommandName(subcommand.name)}`}${parameters ? ` ${parameters}` : ""} [flags]`)]
134
+ body: [pc.magenta(`$ ${cli._name}${ctx.name === SingleCommand ? "" : ` ${formatCommandName(subcommand.name)}`}${parameters ? ` ${parameters}` : ""} [flags]`)]
140
135
  });
141
136
  if (subcommand.flags) {
142
137
  sections.push({
@@ -169,7 +164,7 @@ const showSubcommandHelp = (ctx, command) => {
169
164
  if (subcommand.examples) {
170
165
  generateExamples(sections, subcommand.examples);
171
166
  }
172
- process.stdout.write(render(sections));
167
+ return render(sections);
173
168
  };
174
169
  const helpPlugin = ({
175
170
  command = true,
@@ -195,23 +190,27 @@ const helpPlugin = ({
195
190
  ]
196
191
  }).on("help", (ctx) => {
197
192
  if (ctx.parameters.command.length) {
198
- showSubcommandHelp(ctx, ctx.parameters.command);
193
+ print(generateSubcommandHelp(ctx, ctx.parameters.command));
199
194
  } else {
200
- showHelp(ctx, notes, examples);
195
+ print(generateHelp(ctx, notes, examples));
201
196
  }
202
197
  });
203
198
  }
204
199
  cli.inspector((ctx, next) => {
205
- if (!ctx.isSingleCommand && !ctx.raw._.length && showHelpWhenNoCommand) {
206
- showHelp(ctx, notes, examples);
200
+ if (!ctx.hasSingleCommand && !ctx.raw._.length && showHelpWhenNoCommand) {
201
+ let str = "No command supplied.\n\n";
202
+ str += generateHelp(ctx, notes, examples);
203
+ str += "\n";
204
+ print(str);
205
+ process.exit(1);
207
206
  } else if (ctx.raw.mergedFlags.h || ctx.raw.mergedFlags.help) {
208
- if (ctx.raw._.length) {
209
- showSubcommandHelp(ctx, ctx.raw._);
207
+ if (ctx.raw._.length && ctx.name !== SingleCommand) {
208
+ print(generateSubcommandHelp(ctx, ctx.raw._));
210
209
  } else {
211
- if (!ctx.isSingleCommand) {
212
- showHelp(ctx, notes, examples);
210
+ if (ctx.hasSingleCommand) {
211
+ print(generateHelp(ctx, notes, examples));
213
212
  } else {
214
- showSubcommandHelp(ctx, SingleCommand);
213
+ print(generateSubcommandHelp(ctx, SingleCommand));
215
214
  }
216
215
  }
217
216
  } else {
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { definePlugin, resolveCommand, NoSuchCommandError, SingleCommand } from '@clerc/core';
1
+ import { definePlugin, SingleCommand, resolveCommand, NoSuchCommandError } from '@clerc/core';
2
2
  import { toArray, gracefulFlagName } from '@clerc/utils';
3
3
  import pc from 'picocolors';
4
4
  import getFuncName from 'get-func-name';
@@ -39,7 +39,7 @@ const render = (sections) => {
39
39
  const rendered = [];
40
40
  for (const section of sections) {
41
41
  if (section.type === "block" || !section.type) {
42
- const indent = " ".repeat(4);
42
+ const indent = " ";
43
43
  const formattedBody = section.body.map((line) => indent + line);
44
44
  formattedBody.unshift("");
45
45
  const body = formattedBody.join("\n");
@@ -55,6 +55,9 @@ const render = (sections) => {
55
55
  };
56
56
 
57
57
  const DELIMITER = pc.yellow("-");
58
+ const print = (s) => {
59
+ process.stdout.write(s);
60
+ };
58
61
  const formatCommandName = (name) => Array.isArray(name) ? name.join(" ") : typeof name === "string" ? name : "<Single Command>";
59
62
  const generateCliDetail = (sections, cli, subcommand) => {
60
63
  const items = [
@@ -70,7 +73,7 @@ const generateCliDetail = (sections, cli, subcommand) => {
70
73
  if (subcommand) {
71
74
  items.push({
72
75
  title: "Subcommand:",
73
- body: pc.green(formatCommandName(subcommand.name))
76
+ body: pc.green(`${cli._name} ${formatCommandName(subcommand.name)}`)
74
77
  });
75
78
  }
76
79
  sections.push({
@@ -89,30 +92,22 @@ const generateExamples = (sections, examples) => {
89
92
  body: splitTable(...examplesFormatted)
90
93
  });
91
94
  };
92
- const showHelp = (ctx, notes, examples) => {
95
+ const generateHelp = (ctx, notes, examples) => {
93
96
  const { cli } = ctx;
94
97
  const sections = [];
95
98
  generateCliDetail(sections, cli);
96
- if (ctx.isSingleCommand) {
97
- sections.push({
98
- title: "Usage:",
99
- body: [pc.magenta(`$ ${cli._name} [flags]`)]
100
- });
101
- } else {
102
- sections.push({
103
- title: "Usage:",
104
- body: [pc.magenta(`$ ${cli._name} [command] [flags]`)]
105
- });
106
- }
107
- if (!ctx.isSingleCommand) {
108
- sections.push({
109
- title: "Commands:",
110
- body: splitTable(...Object.values(cli._commands).map((command) => {
111
- const commandNameWithAlias = [command.name, ...toArray(command.alias || [])].join(", ");
112
- return [pc.cyan(commandNameWithAlias), DELIMITER, command.description];
113
- }))
114
- });
115
- }
99
+ sections.push({
100
+ title: "Usage:",
101
+ body: [pc.magenta(`$ ${cli._name} [command] [flags]`)]
102
+ });
103
+ const commands = [...ctx.hasSingleCommand ? [cli._commands[SingleCommand]] : [], ...Object.values(cli._commands)].map((command) => {
104
+ const commandNameWithAlias = [typeof command.name === "symbol" ? "" : command.name, ...toArray(command.alias || [])].map((n) => `${cli._name} ${n}`).join(", ");
105
+ return [pc.cyan(commandNameWithAlias), DELIMITER, command.description];
106
+ });
107
+ sections.push({
108
+ title: "Commands:",
109
+ body: splitTable(...commands)
110
+ });
116
111
  if (notes) {
117
112
  sections.push({
118
113
  title: "Notes:",
@@ -122,21 +117,21 @@ const showHelp = (ctx, notes, examples) => {
122
117
  if (examples) {
123
118
  generateExamples(sections, examples);
124
119
  }
125
- process.stdout.write(render(sections));
120
+ return render(sections);
126
121
  };
127
- const showSubcommandHelp = (ctx, command) => {
122
+ const generateSubcommandHelp = (ctx, command) => {
128
123
  var _a;
129
124
  const { cli } = ctx;
130
125
  const subcommand = resolveCommand(cli._commands, command);
131
- if (!subcommand) {
126
+ if (!subcommand || subcommand.name === SingleCommand) {
132
127
  throw new NoSuchCommandError(formatCommandName(command));
133
128
  }
134
129
  const sections = [];
135
- generateCliDetail(sections, cli, ctx.isSingleCommand ? void 0 : subcommand);
130
+ generateCliDetail(sections, cli, subcommand);
136
131
  const parameters = ((_a = subcommand.parameters) == null ? void 0 : _a.join(" ")) || void 0;
137
132
  sections.push({
138
133
  title: "Usage:",
139
- body: [pc.magenta(`$ ${cli._name}${ctx.isSingleCommand ? "" : ` ${formatCommandName(subcommand.name)}`}${parameters ? ` ${parameters}` : ""} [flags]`)]
134
+ body: [pc.magenta(`$ ${cli._name}${ctx.name === SingleCommand ? "" : ` ${formatCommandName(subcommand.name)}`}${parameters ? ` ${parameters}` : ""} [flags]`)]
140
135
  });
141
136
  if (subcommand.flags) {
142
137
  sections.push({
@@ -169,7 +164,7 @@ const showSubcommandHelp = (ctx, command) => {
169
164
  if (subcommand.examples) {
170
165
  generateExamples(sections, subcommand.examples);
171
166
  }
172
- process.stdout.write(render(sections));
167
+ return render(sections);
173
168
  };
174
169
  const helpPlugin = ({
175
170
  command = true,
@@ -195,23 +190,27 @@ const helpPlugin = ({
195
190
  ]
196
191
  }).on("help", (ctx) => {
197
192
  if (ctx.parameters.command.length) {
198
- showSubcommandHelp(ctx, ctx.parameters.command);
193
+ print(generateSubcommandHelp(ctx, ctx.parameters.command));
199
194
  } else {
200
- showHelp(ctx, notes, examples);
195
+ print(generateHelp(ctx, notes, examples));
201
196
  }
202
197
  });
203
198
  }
204
199
  cli.inspector((ctx, next) => {
205
- if (!ctx.isSingleCommand && !ctx.raw._.length && showHelpWhenNoCommand) {
206
- showHelp(ctx, notes, examples);
200
+ if (!ctx.hasSingleCommand && !ctx.raw._.length && showHelpWhenNoCommand) {
201
+ let str = "No command supplied.\n\n";
202
+ str += generateHelp(ctx, notes, examples);
203
+ str += "\n";
204
+ print(str);
205
+ process.exit(1);
207
206
  } else if (ctx.raw.mergedFlags.h || ctx.raw.mergedFlags.help) {
208
- if (ctx.raw._.length) {
209
- showSubcommandHelp(ctx, ctx.raw._);
207
+ if (ctx.raw._.length && ctx.name !== SingleCommand) {
208
+ print(generateSubcommandHelp(ctx, ctx.raw._));
210
209
  } else {
211
- if (!ctx.isSingleCommand) {
212
- showHelp(ctx, notes, examples);
210
+ if (ctx.hasSingleCommand) {
211
+ print(generateHelp(ctx, notes, examples));
213
212
  } else {
214
- showSubcommandHelp(ctx, SingleCommand);
213
+ print(generateSubcommandHelp(ctx, SingleCommand));
215
214
  }
216
215
  }
217
216
  } else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clerc/plugin-help",
3
- "version": "0.18.0",
3
+ "version": "0.20.0",
4
4
  "author": "Ray <nn_201312@163.com> (https://github.com/so1ve)",
5
5
  "description": "Clerc plugin help",
6
6
  "keywords": [
@@ -53,11 +53,11 @@
53
53
  "dependencies": {
54
54
  "get-func-name": "^2.0.0",
55
55
  "picocolors": "^1.0.0",
56
- "@clerc/toolkit": "0.18.0",
57
- "@clerc/utils": "0.18.0"
56
+ "@clerc/toolkit": "0.20.0",
57
+ "@clerc/utils": "0.20.0"
58
58
  },
59
59
  "devDependencies": {
60
- "@clerc/core": "0.18.0"
60
+ "@clerc/core": "0.20.0"
61
61
  },
62
62
  "scripts": {
63
63
  "build": "puild",