1 | const commandLineUsage = require('command-line-usage');
|
2 | const map = require('lodash/map');
|
3 | const kebabCase = require('lodash/kebabCase');
|
4 | const compact = require('lodash/compact');
|
5 | const behaviorHelpSections = require('./behaviorHelpSections');
|
6 |
|
7 | const displayAppHelp = (app) => {
|
8 |
|
9 | const desc = 'Description not provided.';
|
10 |
|
11 | const intro = [{
|
12 | header: `${app.appName} ${app.version}`,
|
13 | content: app.description || desc
|
14 | }, {
|
15 | header: 'Usage',
|
16 | content: `${app.commandName} <command> [args ...] [options ...]`
|
17 | }];
|
18 |
|
19 | const commands = Object.keys(app.commands).length ? [{
|
20 | header: 'Commands',
|
21 | content: map(app.commands, (commandModule, name) => {
|
22 | return { name, summary: require(commandModule).description || desc };
|
23 | })
|
24 | }, {
|
25 | content: `Run \`${app.commandName} <command> --help\` \
|
26 | for help with a specific command.`,
|
27 | }] : [{
|
28 | header: 'Commands',
|
29 | content: `${app.appName} doesn't have any commands yet.`
|
30 | }];
|
31 |
|
32 | const options = app.options.length ? {
|
33 | header: 'Global options',
|
34 | optionList: map(app.options, (option) => {
|
35 | return { ...option, name: kebabCase(option.name) };
|
36 | })
|
37 | } : undefined;
|
38 |
|
39 | const behaviors = behaviorHelpSections(app);
|
40 |
|
41 | console.log(
|
42 | commandLineUsage(compact([...intro, ...commands, options, ...behaviors]))
|
43 | );
|
44 | };
|
45 |
|
46 | module.exports = displayAppHelp;
|