1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const os = require("os");
|
4 | const utils_1 = require("./utils");
|
5 | const EOL = os.EOL;
|
6 | const GAP = 4;
|
7 | const __ = ' ';
|
8 |
|
9 | exports.sentences = (str = '') => {
|
10 | return str.replace(/([.?!])\s*(?=[A-Z])/g, '$1|').split('|');
|
11 | };
|
12 | const noop = (s) => {
|
13 | return s;
|
14 | };
|
15 | const maxLen = (arr) => {
|
16 | let c = 0;
|
17 | let d = 0;
|
18 | let l = 0;
|
19 | let i = arr.length;
|
20 | if (i) {
|
21 | while (i--) {
|
22 | d = arr[i].length;
|
23 | if (d > c) {
|
24 | l = i;
|
25 | c = d;
|
26 | }
|
27 | }
|
28 | }
|
29 | return arr[l].length;
|
30 | };
|
31 | const formatTable = (arr) => {
|
32 | if (!arr.length) {
|
33 | return '';
|
34 | }
|
35 | if (Array.isArray(arr)) {
|
36 | const len = maxLen(arr.map(x => x[0])) + GAP;
|
37 | const join = a => a[0] + ' '.repeat(len - a[0].length) + a[1] + (a[2] == null ? '' : ` ${a[2]}`);
|
38 | return arr.map(join);
|
39 | }
|
40 | };
|
41 | exports.section = (str, content, fn) => {
|
42 | const arr = Array.isArray(content) ? content : [content];
|
43 | fn = fn || noop;
|
44 | if (!arr || !arr.length) {
|
45 | return '';
|
46 | }
|
47 | let i = 0;
|
48 | let out = '';
|
49 | out += EOL + __ + str;
|
50 | for (; i < arr.length; i++) {
|
51 | out += EOL + __ + __ + fn(arr[i]);
|
52 | }
|
53 | return out + EOL;
|
54 | };
|
55 | const buildUsage = (parsed) => {
|
56 | let args = [];
|
57 | if (parsed.parsedCommandName) {
|
58 | args = (parsed.command.arguments || []).map(a => {
|
59 | return `${a.isOptional ? '[' : '<'}${a.name}${a.isOptional ? ']' : '>'}`;
|
60 | });
|
61 | return `${parsed.parsedCommandName}${args.length > 0 ? ` ${args.join(' ')}` : ''} [options]`;
|
62 | }
|
63 | else {
|
64 | return '<command> [args] [options]';
|
65 | }
|
66 | };
|
67 | exports.buildUsageSection = (parsed) => {
|
68 | const pfx = `$ ${parsed.program.name}`;
|
69 | const prefix = s => `${pfx} ${s}`;
|
70 | return exports.section('Usage', `${buildUsage(parsed)}`, prefix);
|
71 | };
|
72 | exports.helpFormatter = (parsed) => {
|
73 | const rootDef = parsed.program;
|
74 | const cmdDefs = rootDef.commands;
|
75 | let out = '';
|
76 | const pfx = `$ ${rootDef.name}`;
|
77 | const prefix = s => `${pfx} ${s}`;
|
78 |
|
79 | out += exports.section('Description', parsed.parsedCommandName ? parsed.command.description : rootDef.description);
|
80 |
|
81 | out += exports.buildUsageSection(parsed);
|
82 | let argumentsContent = [];
|
83 | if (!parsed.parsedCommandName) {
|
84 |
|
85 | const cmdAndDesc = cmdDefs.map(def => [def.name, def.description || '']);
|
86 | out += exports.section('Available Commands', formatTable(cmdAndDesc));
|
87 | out += EOL + __ + 'For more info, run any command with the `--help` flag';
|
88 | cmdDefs.forEach(def => {
|
89 | out += EOL + __ + __ + `${pfx} ${def.name} --help`;
|
90 | });
|
91 | out += EOL;
|
92 | argumentsContent = [
|
93 | [
|
94 | 'command',
|
95 | `Command Name${rootDef.defaultCommandName ? ` (default: '${rootDef.defaultCommandName}')` : ''}`,
|
96 | ],
|
97 | ];
|
98 | }
|
99 | else {
|
100 |
|
101 | argumentsContent = parsed.command.arguments.map(a => [
|
102 | `${a.name} ${a.isOptional ? '(optional)' : '(required)'}`,
|
103 | `${a.description || ''}${a.default ? ` (default: '${a.default}')` : ''}`,
|
104 | ]);
|
105 | }
|
106 | const cmdDef = parsed.parsedCommandName ? parsed.command : rootDef;
|
107 |
|
108 | out += exports.section('Arguments', formatTable(argumentsContent));
|
109 |
|
110 | const globalOptionsContent = rootDef.options.map(o => [
|
111 | `--${utils_1.dashCase(o.name)}${o.flag ? `, -${o.flag}` : ''}`,
|
112 | o.description || '',
|
113 | ]);
|
114 | out += exports.section('Global Options', formatTable(globalOptionsContent));
|
115 |
|
116 | if (parsed.parsedCommandName) {
|
117 | const cmdOptionsContent = parsed.command.options.map(o => [
|
118 | `--${utils_1.dashCase(o.name)}${o.flag ? `, -${o.flag}` : ''}`,
|
119 | o.description || '',
|
120 | ]);
|
121 | out += exports.section(`${parsed.command.name} Command Options`, formatTable(cmdOptionsContent));
|
122 | }
|
123 |
|
124 | out += exports.section('Examples', (cmdDef.examples || []).map(prefix));
|
125 | return out;
|
126 | };
|
127 |
|
\ | No newline at end of file |