UNPKG

1.83 kBJavaScriptView Raw
1const {Command} = require('heroku-cli-command')
2const util = require('../lib/util')
3const config = require('../lib/config')
4
5class Help extends Command {
6 get plugins () { return require('../lib/plugins') }
7
8 async run () {
9 const argv0 = config.bin
10 let cmd = this.args.find(arg => !['help', '-h', '--help'].includes(arg))
11 if (!cmd) return this.topics({argv0})
12 let topicName = cmd.split(':')[0]
13 let topic = this.plugins.topics[topicName]
14 let matchedCommand = this.plugins.commands[cmd]
15 if (!topic && !matchedCommand) throw new Error(`command ${cmd} not found`)
16 if (!topic) topic = {name: topicName, fetch: () => { }}
17 let Topic = topic.fetch()
18 if (typeof Topic !== 'function') {
19 Topic = class extends require('heroku-cli-command').Topic {}
20 Topic.topic = topic.topic
21 Topic.description = topic.description
22 }
23 let commands = this.plugins.commandList.filter(c => c.topic === topicName)
24 topic = new Topic({flags: this.flags, commands})
25 await topic.help({args: this.args, matchedCommand, argv0})
26 }
27
28 topics ({argv0}) {
29 const max = require('lodash.maxby')
30 const S = require('string')
31
32 this.log(`Usage: ${argv0} COMMAND [--app APP] [command-specific-options]
33
34 Help topics, type ${this.color.cmd(argv0 + ' help TOPIC')} for more details:
35 `)
36 let topics = Object.keys(this.plugins.topics).map(t => this.plugins.topics[t])
37 topics = topics.filter(t => !t.hidden)
38 topics.sort(util.compare('topic'))
39 let maxlength = max(topics, 'topic.length').topic.length
40 for (let topic of topics) {
41 this.log(` ${argv0} ${S(topic.topic).padRight(maxlength)}${topic.description ? ' # ' + topic.description : ''}`)
42 }
43
44 this.log()
45 }
46}
47
48Help.topic = 'help'
49Help.description = 'display help'
50Help.variableArgs = true
51
52module.exports = Help