-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.ts
64 lines (52 loc) · 1.75 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
import color from '@oclif/color'
import {Command, Flags, Plugin, CliUx} from '@oclif/core'
import Plugins from '../../plugins'
import {sortBy} from '../../util'
export default class PluginsIndex extends Command {
static flags = {
core: Flags.boolean({description: 'Show core plugins.'}),
}
static description = 'List installed plugins.'
static examples = ['$ <%- config.bin %> plugins']
plugins = new Plugins(this.config)
async run(): Promise<void> {
const {flags} = await this.parse(PluginsIndex)
let plugins = this.config.plugins
sortBy(plugins, p => this.plugins.friendlyName(p.name))
if (!flags.core) {
plugins = plugins.filter(p => p.type !== 'core' && p.type !== 'dev')
}
if (plugins.length === 0) {
this.log('No plugins installed.')
return
}
this.display(plugins as Plugin[])
}
private display(plugins: Plugin[]) {
for (const plugin of plugins.filter((p: Plugin) => !p.parent)) {
this.log(this.formatPlugin(plugin))
if (plugin.children && plugin.children.length > 0) {
const tree = this.createTree(plugin)
tree.display()
}
}
}
private createTree(plugin: Plugin) {
const tree = CliUx.ux.tree()
for (const p of plugin.children) {
const name = this.formatPlugin(p)
tree.insert(name, this.createTree(p))
}
return tree
}
private formatPlugin(plugin: any): string {
let output = `${this.plugins.friendlyName(plugin.name)} ${color.dim(plugin.version)}`
if (plugin.type !== 'user')
output += color.dim(` (${plugin.type})`)
if (plugin.type === 'link')
output += ` ${plugin.root}`
else if (plugin.tag && plugin.tag !== 'latest')
output += color.dim(` (${String(plugin.tag)})`)
return output
}
}