-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathcommand.ts
More file actions
414 lines (348 loc) · 13.8 KB
/
command.ts
File metadata and controls
414 lines (348 loc) · 13.8 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import ansis from 'ansis'
import {Command} from '../command'
import * as Interfaces from '../interfaces'
import {ensureArgObject} from '../util/ensure-arg-object'
import {toConfiguredId, toStandardizedId} from '../util/ids'
import {castArray, compact, sortBy} from '../util/util'
import {colorize} from '../ux/theme'
import {DocOpts} from './docopts'
import {HelpFormatter, HelpSection, HelpSectionRenderer} from './formatter'
// Don't use os.EOL because we need to ensure that a string
// written on any platform, that may use \r\n or \n, will be
// split on any platform, not just the os specific EOL at runtime.
const POSSIBLE_LINE_FEED = /\r\n|\n/
/**
* Determines the sort order of flags. Will default to alphabetical if not set or set to an invalid value.
*/
function determineSortOrder(
flagSortOrder: HelpFormatter['opts']['flagSortOrder'],
): NonNullable<HelpFormatter['opts']['flagSortOrder']> {
if (flagSortOrder === 'alphabetical') return 'alphabetical'
if (flagSortOrder === 'none') return 'none'
return 'alphabetical'
}
export class CommandHelp extends HelpFormatter {
constructor(
public command: Command.Loadable,
public config: Interfaces.Config,
public opts: Interfaces.HelpOptions,
) {
super(config, opts)
}
protected aliases(aliases: string[] | undefined): string | undefined {
if (!aliases || aliases.length === 0) return
const body = aliases
.map((a) =>
[
colorize(this.config?.theme?.dollarSign, '$'),
colorize(this.config?.theme?.bin, this.config.bin),
colorize(this.config?.theme?.alias, a),
].join(' '),
)
.join('\n')
return body
}
protected arg(arg: Command.Arg.Any): string {
const name = arg.name.toUpperCase()
if (arg.required) return `${name}`
return `[${name}]`
}
protected args(args: Command.Arg.Any[]): [string, string | undefined][] | undefined {
if (args.filter((a) => a.description).length === 0) return
return args.map((a) => {
// Add ellipsis to indicate that the argument takes multiple values if strict is false
let name = this.command.strict === false ? `${a.name.toUpperCase()}...` : a.name.toUpperCase()
name = a.required ? `${name}` : `[${name}]`
let description = a.description || ''
if (a.default)
description = `${colorize(this.config?.theme?.flagDefaultValue, `[default: ${a.default}]`)} ${description}`
if (a.options)
description = `${colorize(this.config?.theme?.flagOptions, `(${a.options.join('|')})`)} ${description}`
return [
colorize(this.config?.theme?.flag, name),
description ? colorize(this.config?.theme?.sectionDescription, description) : undefined,
]
})
}
protected defaultUsage(): string {
// Docopts by default
if (this.opts.docopts === undefined || this.opts.docopts) {
return DocOpts.generate(this.command)
}
return compact([
this.command.id,
Object.values(this.command.args ?? {})
?.filter((a) => !a.hidden)
.map((a) => this.arg(a))
.join(' '),
]).join(' ')
}
protected description(): string | undefined {
const cmd = this.command
let description: string[] | undefined
if (this.opts.hideCommandSummaryInDescription) {
description = [(cmd.description || '').split(POSSIBLE_LINE_FEED).at(-1) ?? '']
} else if (cmd.description) {
const summary = cmd.summary ? `${cmd.summary}\n` : null
description = summary
? [...summary.split(POSSIBLE_LINE_FEED), ...(cmd.description || '').split(POSSIBLE_LINE_FEED)]
: (cmd.description || '').split(POSSIBLE_LINE_FEED)
}
if (description) {
return this.wrap(description.join('\n'))
}
}
protected examples(examples: Command.Example[] | string | undefined): string | undefined {
if (!examples || examples.length === 0) return
const body = castArray(examples)
.map((a) => {
let description
let commands
if (typeof a === 'string') {
const lines = a.split(POSSIBLE_LINE_FEED).filter(Boolean)
// If the example is <description>\n<command> then format correctly
if (lines.length >= 2 && !this.isCommand(lines[0]) && lines.slice(1).every((i) => this.isCommand(i))) {
description = lines[0]
commands = lines.slice(1)
} else {
return lines.map((line) => this.formatIfCommand(line)).join('\n')
}
} else {
description = a.description
commands = [a.command]
}
const multilineSeparator =
this.config.platform === 'win32' ? (this.config.shell.includes('powershell') ? '`' : '^') : '\\'
// The command will be indented in the section, which is also indented
const finalIndentedSpacing = this.indentSpacing * 2
const multilineCommands = commands
.map((c) =>
// First indent keeping room for escaped newlines
this.indent(this.wrap(this.formatIfCommand(c), finalIndentedSpacing + 4))
// Then add the escaped newline
.split(POSSIBLE_LINE_FEED)
.join(` ${multilineSeparator}\n `),
)
.join('\n')
return `${this.wrap(description, finalIndentedSpacing)}\n\n${multilineCommands}`
})
.join('\n\n')
return body
}
protected flagHelpLabel(flag: Command.Flag.Any, showOptions = false): string {
let label = flag.helpLabel
if (!label) {
const labels = []
labels.push(flag.char ? `-${flag.char[0]}` : ' ')
if (flag.name) {
if (flag.type === 'boolean' && flag.allowNo) {
labels.push(`--[no-]${flag.name.trim()}`)
} else {
labels.push(`--${flag.name.trim()}`)
}
}
label = labels.join(flag.char ? colorize(this.config?.theme?.flagSeparator, ', ') : ' ')
}
if (flag.type === 'option') {
let value = DocOpts.formatUsageType(
flag,
this.opts.showFlagNameInTitle ?? false,
this.opts.showFlagOptionsInTitle ?? showOptions,
)
if (!value.includes('|')) value = colorize('underline', value)
label += `=${value}`
}
return colorize(this.config.theme?.flag, label)
}
protected flags(flags: Array<Command.Flag.Any>): [string, string | undefined][] | undefined {
if (flags.length === 0) return
const noChar = flags.reduce((previous, current) => previous && current.char === undefined, true)
// eslint-disable-next-line complexity
return flags.map((flag) => {
let left = this.flagHelpLabel(flag)
if (noChar) left = left.replace(' ', '')
let right = flag.summary || flag.description || ''
// Build metadata string (default, env, or both)
const metadata: string[] = []
const canBeCached = !(this.opts.respectNoCacheDefault === true && flag.noCacheDefault === true)
if (flag.type === 'option') {
if (flag.default && canBeCached) {
metadata.push(`default: ${flag.default}`)
}
if (flag.env) {
metadata.push(`env: ${flag.env}`)
}
if ((flag as any).min !== undefined) {
metadata.push(`min: ${(flag as any).min}`)
}
if ((flag as any).max !== undefined) {
metadata.push(`max: ${(flag as any).max}`)
}
} else if (flag.type === 'boolean' && flag.env) {
metadata.push(`env: ${flag.env}`)
}
if (metadata.length > 0) {
right = `${colorize(this.config?.theme?.flagDefaultValue, `[${metadata.join(', ')}]`)} ${right}`
}
if (flag.required) right = `${colorize(this.config?.theme?.flagRequired, '(required)')} ${right}`
if (flag.type === 'option' && flag.options && !flag.helpValue && !this.opts.showFlagOptionsInTitle) {
right += colorize(this.config?.theme?.flagOptions, `\n<options: ${flag.options.join('|')}>`)
}
return [left, colorize(this.config?.theme?.sectionDescription, right.trim())]
})
}
protected flagsDescriptions(flags: Array<Command.Flag.Any>): string | undefined {
const flagsWithExtendedDescriptions = flags.filter((flag) => flag.summary && flag.description)
if (flagsWithExtendedDescriptions.length === 0) return
const body = flagsWithExtendedDescriptions
.map((flag) => {
// Guaranteed to be set because of the filter above, but make ts happy
const summary = flag.summary || ''
let flagHelp = this.flagHelpLabel(flag, true)
if (!flag.char) flagHelp = flagHelp.replace(' ', '')
flagHelp +=
flagHelp.length + summary.length + 2 < this.opts.maxWidth
? ' ' + summary
: '\n\n' + this.indent(this.wrap(summary, this.indentSpacing * 2))
return `${flagHelp}\n\n${this.indent(this.wrap(flag.description || '', this.indentSpacing * 2))}`
})
.join('\n\n')
return body
}
generate(): string {
const cmd = this.command
const unsortedFlags = Object.entries(cmd.flags || {})
.filter(([, v]) => !v.hidden)
.map(([k, v]) => {
v.name = k
return v
})
const flags =
determineSortOrder(this.opts.flagSortOrder) === 'alphabetical'
? sortBy(unsortedFlags, (f) => [!f.char, f.char, f.name])
: unsortedFlags
const args = Object.values(ensureArgObject(cmd.args)).filter((a) => !a.hidden)
const output = compact(
this.sections().map(({generate, header}) => {
const body = generate({args, cmd, flags}, header)
// Generate can return a list of sections
if (Array.isArray(body)) {
return body
.map((helpSection) => helpSection && helpSection.body && this.section(helpSection.header, helpSection.body))
.join('\n\n')
}
return body && this.section(header, body)
}),
).join('\n\n')
return output
}
protected groupFlags(flags: Array<Command.Flag.Any>): {
flagGroups: {[name: string]: Array<Command.Flag.Any>}
mainFlags: Array<Command.Flag.Any>
} {
const mainFlags: Array<Command.Flag.Any> = []
const flagGroups: {[index: string]: Array<Command.Flag.Any>} = {}
for (const flag of flags) {
const group = flag.helpGroup
if (group) {
if (!flagGroups[group]) flagGroups[group] = []
flagGroups[group].push(flag)
} else {
mainFlags.push(flag)
}
}
return {flagGroups, mainFlags}
}
protected sections(): Array<{generate: HelpSectionRenderer; header: string}> {
const sections: Array<{generate: HelpSectionRenderer; header: string}> = [
{
generate: () => this.usage(),
header: this.opts.usageHeader || 'USAGE',
},
{
generate: ({args}, header) => [{body: this.args(args), header}],
header: 'ARGUMENTS',
},
{
generate: ({flags}, header) => {
const {flagGroups, mainFlags} = this.groupFlags(flags)
const flagSections: HelpSection[] = []
const mainFlagBody = this.flags(mainFlags)
if (mainFlagBody) flagSections.push({body: mainFlagBody, header})
for (const [name, flags] of Object.entries(flagGroups)) {
const body = this.flags(flags)
if (body) flagSections.push({body, header: `${name.toUpperCase()} ${header}`})
}
return compact<HelpSection>(flagSections)
},
header: 'FLAGS',
},
{
generate: () => this.description(),
header: 'DESCRIPTION',
},
{
generate: ({cmd}) => this.aliases(cmd.aliases),
header: 'ALIASES',
},
{
generate: ({cmd}) => {
const examples = cmd.examples || (cmd as any).example
return this.examples(examples)
},
header: 'EXAMPLES',
},
{
generate: ({flags}) => this.flagsDescriptions(flags),
header: 'FLAG DESCRIPTIONS',
},
]
const allowedSections = this.opts.sections?.map((s) => s.toLowerCase())
return sections.filter(({header}) => !allowedSections || allowedSections.includes(header.toLowerCase()))
}
protected usage(): string {
const {id, usage} = this.command
const standardId = toStandardizedId(id, this.config)
const configuredId = toConfiguredId(id, this.config)
const body = (usage ? castArray(usage) : [this.defaultUsage()])
.map((u) => {
const allowedSpacing = this.opts.maxWidth - this.indentSpacing
const dollarSign = colorize(this.config?.theme?.dollarSign, '$')
const bin = colorize(this.config?.theme?.bin, this.config.bin)
const command = colorize(this.config?.theme?.command, '<%= command.id %>')
const commandDescription = colorize(
this.config?.theme?.sectionDescription,
u
.replace('<%= command.id %>', '')
.replace(new RegExp(`^${standardId}`), '')
.replace(new RegExp(`^${configuredId}`), '')
.trim(),
)
const line = `${dollarSign} ${bin} ${command} ${commandDescription}`.trim()
if (line.length > allowedSpacing) {
const splitIndex = line.slice(0, Math.max(0, allowedSpacing)).lastIndexOf(' ')
return (
line.slice(0, Math.max(0, splitIndex)) +
'\n' +
this.indent(this.wrap(line.slice(Math.max(0, splitIndex)), this.indentSpacing * 2))
)
}
return this.wrap(line)
})
.join('\n')
return body
}
private formatIfCommand(example: string): string {
example = this.render(example)
const dollarSign = colorize(this.config?.theme?.dollarSign, '$')
if (example.startsWith(this.config.bin)) return `${dollarSign} ${example}`
if (example.startsWith(`$ ${this.config.bin}`)) return `${dollarSign}${example.replace(`$`, '')}`
return example
}
private isCommand(example: string): boolean {
return ansis
.strip(this.formatIfCommand(example))
.startsWith(`${colorize(this.config?.theme?.dollarSign, '$')} ${this.config.bin}`)
}
}
export default CommandHelp