-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
83 lines (68 loc) · 1.84 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* Cli `meow` Helper.
*
* Generate automatically formatted help text for `meow` CLI helper
*
* @author Awais <https://twitter.com/MrAhmadAwais/>
*/
const chalk = require('chalk');
const createTable = require('./utils/createTable');
const getDefaultValue = require('./utils/getDefaultValue');
const dim = chalk.dim;
const greenInverse = chalk.bold.inverse.green;
const boldInverse = chalk.bold.inverse;
const cyanInverse = chalk.bold.inverse.cyan;
const yellowInverse = chalk.bold.inverse.yellow;
module.exports = ({
name = `(CLI name undefined)`,
desc,
commands = {},
flags = {},
defaults = true,
header,
footer
}) => {
let help = '';
const spacer = `\n\n`;
if (header) {
help += `${header}${spacer}`;
}
if (desc) {
help += `${desc}${spacer}`;
}
// Usage.
help += `${greenInverse(` USAGE `)} ${spacer}`;
help += chalk`{gray $} {green ${name}} {cyan <command>} {yellow [option]}`;
// Commands.
help += `\n\n${cyanInverse(` COMMANDS `)} ${spacer}`;
const tableCommands = createTable();
const commandKeys = Object.keys(commands);
for (const command of commandKeys) {
let options = commands[command];
const defaultValue = getDefaultValue(defaults, options);
tableCommands.push([
chalk`{cyan ${command}}`,
`${options.desc} ${dim(defaultValue)}`
]);
}
help += tableCommands.toString();
// Flags.
help += `\n\n${yellowInverse(` OPTIONS `)} ${spacer}`;
const tableFlags = createTable();
const flagKeys = Object.keys(flags);
for (const flag of flagKeys) {
let options = flags[flag];
let alias = options.alias ? `-${options.alias}, ` : ``;
const defaultValue = getDefaultValue(defaults, options);
tableFlags.push([
chalk`{yellow ${alias}--${flag}}`,
`${options.desc} ${dim(defaultValue)}`
]);
}
help += tableFlags.toString();
help += `\n`;
if (footer) {
help += `\n${footer}\n`;
}
return help;
};