-
Notifications
You must be signed in to change notification settings - Fork 67
/
index.ts
326 lines (270 loc) · 10.2 KB
/
index.ts
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
import stripAnsi = require('strip-ansi')
import * as Interfaces from '../interfaces'
import {error} from '../errors'
import CommandHelp from './command'
import RootHelp from './root'
import {compact, sortBy, uniqBy} from '../util'
import {formatCommandDeprecationWarning, getHelpFlagAdditions, standardizeIDFromArgv, toConfiguredId} from './util'
import {HelpFormatter} from './formatter'
import {toCached} from '../config/config'
export {CommandHelp} from './command'
export {standardizeIDFromArgv, loadHelpClass, getHelpFlagAdditions} from './util'
function getHelpSubject(args: string[], config: Interfaces.Config): string | undefined {
// for each help flag that starts with '--' create a new flag with same name sans '--'
const mergedHelpFlags = getHelpFlagAdditions(config)
for (const arg of args) {
if (arg === '--') return
if (mergedHelpFlags.includes(arg) || arg === 'help') continue
if (arg.startsWith('-')) return
return arg
}
}
export abstract class HelpBase extends HelpFormatter {
constructor(config: Interfaces.Config, opts: Partial<Interfaces.HelpOptions> = {}) {
super(config, opts)
if (!config.topicSeparator) config.topicSeparator = ':' // back-support @oclif/config
}
/**
* Show help, used in multi-command CLIs
* @param args passed into your command, useful for determining which type of help to display
*/
public abstract showHelp(argv: string[]): Promise<void>;
/**
* Show help for an individual command
* @param command
* @param topics
*/
public abstract showCommandHelp(command: Interfaces.Command, topics: Interfaces.Topic[]): Promise<void>;
}
export class Help extends HelpBase {
protected CommandHelpClass: typeof CommandHelp = CommandHelp
/*
* _topics is to work around Interfaces.topics mistakenly including commands that do
* not have children, as well as topics. A topic has children, either commands or other topics. When
* this is fixed upstream config.topics should return *only* topics with children,
* and this can be removed.
*/
private get _topics(): Interfaces.Topic[] {
return this.config.topics.filter((topic: Interfaces.Topic) => {
// it is assumed a topic has a child if it has children
const hasChild = this.config.topics.some(subTopic => subTopic.name.includes(`${topic.name}:`))
return hasChild
})
}
protected get sortedCommands() {
let commands = this.config.commands
commands = commands.filter(c => this.opts.all || !c.hidden)
commands = sortBy(commands, c => c.id)
commands = uniqBy(commands, c => c.id)
return commands
}
protected get sortedTopics() {
let topics = this._topics
topics = topics.filter(t => this.opts.all || !t.hidden)
topics = sortBy(topics, t => t.name)
topics = uniqBy(topics, t => t.name)
return topics
}
constructor(config: Interfaces.Config, opts: Partial<Interfaces.HelpOptions> = {}) {
super(config, opts)
}
public async showHelp(argv: string[]) {
const originalArgv = argv.slice(1)
argv = argv.filter(arg => !getHelpFlagAdditions(this.config).includes(arg))
if (this.config.topicSeparator !== ':') argv = standardizeIDFromArgv(argv, this.config)
const subject = getHelpSubject(argv, this.config)
if (!subject) {
if (this.config.pjson.oclif.default) {
const rootCmd = this.config.findCommand(this.config.pjson.oclif.default)
if (rootCmd) {
await this.showCommandHelp(rootCmd)
return
}
}
await this.showRootHelp()
return
}
const command = this.config.findCommand(subject)
if (command) {
if (command.hasDynamicHelp) {
const dynamicCommand = await toCached(await command.load())
await this.showCommandHelp(dynamicCommand)
} else {
await this.showCommandHelp(command)
}
return
}
const topic = this.config.findTopic(subject)
if (topic) {
await this.showTopicHelp(topic)
return
}
if (this.config.flexibleTaxonomy) {
const matches = this.config.findMatches(subject, originalArgv)
if (matches.length > 0) {
const result = await this.config.runHook('command_incomplete', {id: subject, argv: originalArgv, matches})
if (result.successes.length > 0) return
}
}
error(`Command ${subject} not found.`)
}
public async showCommandHelp(command: Interfaces.Command) {
const name = command.id
const depth = name.split(':').length
const subTopics = this.sortedTopics.filter(t => t.name.startsWith(name + ':') && t.name.split(':').length === depth + 1)
const subCommands = this.sortedCommands.filter(c => c.id.startsWith(name + ':') && c.id.split(':').length === depth + 1)
const plugin = this.config.plugins.find(p => p.name === command.pluginName)
const state = this.config.pjson?.oclif?.state || plugin?.pjson?.oclif?.state || command.state
if (state) {
this.log(
state === 'deprecated' ?
`${formatCommandDeprecationWarning(toConfiguredId(name, this.config), command.deprecationOptions)}` :
`This command is in ${state}.\n`,
)
}
const summary = this.summary(command)
if (summary) {
this.log(summary + '\n')
}
this.log(this.formatCommand(command))
this.log('')
if (subTopics.length > 0) {
this.log(this.formatTopics(subTopics))
this.log('')
}
if (subCommands.length > 0) {
const aliases:string[] = []
const uniqueSubCommands:Interfaces.Command.Loadable[] = subCommands.filter(p => {
aliases.push(...p.aliases)
return !aliases.includes(p.id)
})
this.log(this.formatCommands(uniqueSubCommands))
this.log('')
}
}
protected async showRootHelp() {
let rootTopics = this.sortedTopics
let rootCommands = this.sortedCommands
const state = this.config.pjson?.oclif?.state
if (state) {
this.log(
state === 'deprecated' ?
`${this.config.bin} is deprecated` :
`${this.config.bin} is in ${state}.\n`,
)
}
this.log(this.formatRoot())
this.log('')
if (!this.opts.all) {
rootTopics = rootTopics.filter(t => !t.name.includes(':'))
rootCommands = rootCommands.filter(c => !c.id.includes(':'))
}
if (rootTopics.length > 0) {
this.log(this.formatTopics(rootTopics))
this.log('')
}
if (rootCommands.length > 0) {
rootCommands = rootCommands.filter(c => c.id)
this.log(this.formatCommands(rootCommands))
this.log('')
}
}
protected async showTopicHelp(topic: Interfaces.Topic) {
const name = topic.name
const depth = name.split(':').length
const subTopics = this.sortedTopics.filter(t => t.name.startsWith(name + ':') && t.name.split(':').length === depth + 1)
const commands = this.sortedCommands.filter(c => c.id.startsWith(name + ':') && c.id.split(':').length === depth + 1)
const state = this.config.pjson?.oclif?.state
if (state) this.log(`This topic is in ${state}.\n`)
this.log(this.formatTopic(topic))
if (subTopics.length > 0) {
this.log(this.formatTopics(subTopics))
this.log('')
}
if (commands.length > 0) {
this.log(this.formatCommands(commands))
this.log('')
}
}
protected formatRoot(): string {
const help = new RootHelp(this.config, this.opts)
return help.root()
}
protected formatCommand(command: Interfaces.Command): string {
if (this.config.topicSeparator !== ':') {
command.id = command.id.replace(/:/g, this.config.topicSeparator)
command.aliases = command.aliases && command.aliases.map(a => a.replace(/:/g, this.config.topicSeparator))
}
const help = this.getCommandHelpClass(command)
return help.generate()
}
protected getCommandHelpClass(command: Interfaces.Command) {
return new this.CommandHelpClass(command, this.config, this.opts)
}
protected formatCommands(commands: Interfaces.Command[]): string {
if (commands.length === 0) return ''
const body = this.renderList(commands.map(c => {
if (this.config.topicSeparator !== ':') c.id = c.id.replace(/:/g, this.config.topicSeparator)
return [
c.id,
this.summary(c),
]
}), {
spacer: '\n',
stripAnsi: this.opts.stripAnsi,
indentation: 2,
})
return this.section('COMMANDS', body)
}
protected summary(c: Interfaces.Command): string | undefined {
if (c.summary) return this.render(c.summary.split('\n')[0])
return c.description && this.render(c.description).split('\n')[0]
}
protected description(c: Interfaces.Command): string {
const description = this.render(c.description || '')
if (c.summary) {
return description
}
return description.split('\n').slice(1).join('\n')
}
protected formatTopic(topic: Interfaces.Topic): string {
let description = this.render(topic.description || '')
const summary = description.split('\n')[0]
description = description.split('\n').slice(1).join('\n')
let topicID = `${topic.name}:COMMAND`
if (this.config.topicSeparator !== ':') topicID = topicID.replace(/:/g, this.config.topicSeparator)
let output = compact([
summary,
this.section(this.opts.usageHeader || 'USAGE', `$ ${this.config.bin} ${topicID}`),
description && this.section('DESCRIPTION', this.wrap(description)),
]).join('\n\n')
if (this.opts.stripAnsi) output = stripAnsi(output)
return output + '\n'
}
protected formatTopics(topics: Interfaces.Topic[]): string {
if (topics.length === 0) return ''
const body = this.renderList(topics.map(c => {
if (this.config.topicSeparator !== ':') c.name = c.name.replace(/:/g, this.config.topicSeparator)
return [
c.name,
c.description && this.render(c.description.split('\n')[0]),
]
}), {
spacer: '\n',
stripAnsi: this.opts.stripAnsi,
indentation: 2,
})
return this.section('TOPICS', body)
}
/**
* @deprecated used for readme generation
* @param {object} command The command to generate readme help for
* @return {string} the readme help string for the given command
*/
protected command(command: Interfaces.Command) {
return this.formatCommand(command)
}
protected log(...args: string[]) {
console.log(...args)
}
}