-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.ts
108 lines (89 loc) · 3.46 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
import {Command, Flags, Interfaces, Plugin} from '@oclif/core'
import {dim} from 'ansis'
// @ts-expect-error because object-treeify does not have types: https://github.com/blackflux/object-treeify/issues/1077
import treeify from 'object-treeify'
import Plugins from '../../plugins.js'
import {sortBy} from '../../util.js'
type JitPlugin = {name: string; type: string; version: string}
type PluginsJson = Array<Interfaces.Plugin | JitPlugin>
interface RecursiveTree {
[key: string]: RecursiveTree | string
}
export default class PluginsIndex extends Command {
static description = 'List installed plugins.'
static enableJsonFlag = true
static examples = ['<%= config.bin %> <%= command.id %>']
static flags = {
core: Flags.boolean({description: 'Show core plugins.'}),
}
plugins!: Plugins
public async run(): Promise<PluginsJson> {
const {flags} = await this.parse(PluginsIndex)
this.plugins = new Plugins({
config: this.config,
})
let plugins = this.config.getPluginsList()
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.')
const results = this.config.getPluginsList()
const userAndLinkedPlugins = new Set(
results.filter((p) => p.type === 'user' || p.type === 'link').map((p) => p.name),
)
const jitPluginsConfig = this.config.pjson.oclif.jitPlugins ?? {}
const jitPlugins: JitPlugin[] = Object.entries(jitPluginsConfig)
.map(([name, version]) => ({name, type: 'jit', version}))
.filter((p) => !userAndLinkedPlugins.has(p.name))
sortBy(jitPlugins, (p) => p.name)
if (!this.jsonEnabled()) {
this.display(plugins as Plugin[])
this.displayJitPlugins(jitPlugins)
}
return [
...results
.filter((p) => !p.parent)
.map((p) => {
// @ts-expect-error because we are removing the config property, which may or may not be there.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const {config, ...rest} = p
return rest
}),
...jitPlugins,
]
}
private createTree(plugin: Plugin) {
const tree: RecursiveTree = {}
for (const p of plugin.children ?? []) {
tree[this.formatPlugin(p)] = this.createTree(p)
}
return tree
}
private display(plugins: Plugin[]) {
const rootPlugin = plugins.find((p) => p.root === this.config.root)
for (const plugin of plugins.filter((p: Plugin) => !p.parent)) {
// don't log the root plugin
if (plugin.name === rootPlugin?.name) continue
this.log(this.formatPlugin(plugin))
if (plugin.children && plugin.children.length > 0) {
const tree = this.createTree(plugin)
this.log(treeify(tree))
}
}
}
private displayJitPlugins(jitPlugins: JitPlugin[]) {
if (jitPlugins.length === 0) return
this.log(dim('\nUninstalled JIT Plugins:'))
for (const {name, version} of jitPlugins) {
this.log(`${this.plugins.friendlyName(name)} ${dim(version)}`)
}
}
private formatPlugin(plugin: Plugin): string {
let output = `${this.plugins.friendlyName(plugin.name)} ${dim(plugin.version)}`
if (plugin.type !== 'user') output += dim(` (${plugin.type})`)
if (plugin.type === 'link') output += ` ${plugin.root}`
else if (plugin.tag && plugin.tag !== 'latest') output += dim(` (${String(plugin.tag)})`)
return output
}
}