UNPKG

4.71 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var index_1 = require("./Output/index");
4var util_1 = require("./util");
5var chalk_1 = require("chalk");
6function renderList(items) {
7 var S = require('string');
8 var max = require('lodash.maxby');
9 var maxLength = max(items, '[0].length')[0].length;
10 var lines = items
11 .map(function (i) {
12 var left = i[0];
13 var right = i[1];
14 if (!right) {
15 return left;
16 }
17 left = "" + S(left).padRight(maxLength);
18 right = util_1.linewrap(maxLength + 2, right);
19 return left + " " + right;
20 });
21 return lines.join('\n');
22}
23// TODO find proper typing for this flow definition: Class<Command<*>>
24function buildUsage(command) {
25 if (command.usage) {
26 return command.usage.trim();
27 }
28 var cmd = command.id.replace(/:/g, ' ');
29 if (!command.args) {
30 return chalk_1.default.bold(cmd.trim());
31 }
32 var args = command.args.map(renderArg);
33 return (chalk_1.default.bold(cmd) + " " + args.join(' ')).trim();
34}
35function renderArg(arg) {
36 var name = arg.name.toUpperCase();
37 if (arg.required) {
38 return "" + name;
39 }
40 else {
41 return "[" + name + "]";
42 }
43}
44var Help = (function () {
45 function Help(config, output) {
46 this.config = config;
47 this.out = output || new index_1.Output(config);
48 }
49 // TODO: command (cmd: Class<Command<*>>): string {
50 Help.prototype.command = function (cmd) {
51 var color = this.out.color;
52 var flags = Object.keys(cmd.flags || {}).map(function (f) { return [f, cmd.flags[f]]; }).filter(function (f) { return !f[1].hidden; });
53 var args = (cmd.args || []).filter(function (a) { return !a.hidden; });
54 var hasFlags = flags.length ? " " + color.green('[flags]') : '';
55 var usage = color.bold('Usage:') + " " + this.config.bin + " " + buildUsage(cmd) + hasFlags + "\n";
56 return [usage, cmd.description ? "\n" + color.bold(cmd.description.trim()) + "\n" : '',
57 this.renderAliases(cmd.aliases),
58 this.renderArgs(args),
59 this.renderFlags(flags),
60 cmd.help ? "\n" + cmd.help.trim() + "\n" : ''
61 ].join('');
62 };
63 // TODO: commandLine (cmd: Class<Command<*>>): [string, ?string] {
64 Help.prototype.commandLine = function (cmd) {
65 return [
66 buildUsage(cmd),
67 cmd.description ? this.out.color.dim(cmd.description) : null
68 ];
69 };
70 Help.prototype.renderAliases = function (aliases) {
71 var _this = this;
72 if (!aliases || !aliases.length) {
73 return '';
74 }
75 var a = aliases.map(function (a) { return " $ " + _this.config.bin + " " + a; }).join('\n');
76 return "\n" + this.out.color.green('Aliases:') + "\n" + a + "\n";
77 };
78 Help.prototype.renderArgs = function (args) {
79 var _this = this;
80 if (!args.find(function (f) { return !!f.description; })) {
81 return '';
82 }
83 return '\n' + renderList(args.map(function (a) {
84 return [
85 a.name.toUpperCase(),
86 a.description ? _this.out.color.dim(a.description) : null
87 ];
88 })) + '\n';
89 };
90 // TODO renderFlags (flags: [string, Flag][]): string {
91 Help.prototype.renderFlags = function (flags) {
92 var _this = this;
93 if (!flags.length) {
94 return '';
95 }
96 flags.sort(function (a, b) {
97 if (a[1].char && !b[1].char) {
98 return -1;
99 }
100 if (b[1].char && !a[1].char) {
101 return 1;
102 }
103 if (a[0] < b[0]) {
104 return -1;
105 }
106 return b[0] < a[0] ? 1 : 0;
107 });
108 return "\n" + this.out.color.green('Flags:') + "\n" +
109 renderList(flags.map(function (_a) {
110 var name = _a[0], f = _a[1];
111 var label = [];
112 if (f.char) {
113 label.push("-" + f.char);
114 }
115 if (name) {
116 label.push(" --" + name);
117 }
118 var usage = f.parse ? " " + name.toUpperCase() : '';
119 var description = f.description || '';
120 if (f.required || f.optional === false) {
121 description = "(required) " + description;
122 }
123 return [
124 " " + label.join(',').trim() + usage,
125 description ? _this.out.color.dim(description) : null
126 ];
127 })) + '\n';
128 };
129 return Help;
130}());
131exports.default = Help;
132//# sourceMappingURL=Help.js.map
\No newline at end of file