forked from oclif/plugin-plugins
-
Notifications
You must be signed in to change notification settings - Fork 1
/
list.ts
67 lines (57 loc) · 1.94 KB
/
list.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
import color from "@oclif/color";
import { Command, flags } from "@oclif/command";
import { Plugin } from "@oclif/config";
import { cli } from "cli-ux";
import Plugins from "../../modules/plugins";
import { sortBy } from "../../modules/util";
import { ColorifyConstants } from "vtex";
export default class PluginsIndex extends Command {
static flags = {
core: flags.boolean({ description: "Shows core plugins." }),
};
static description = "Lists all plugins installed on your machine.";
static examples = [
`${ColorifyConstants.COMMAND_OR_VTEX_REF("vtex plugins list")}`,
];
plugins = new Plugins(this.config);
async run() {
const { flags } = 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(this.log);
}
}
}
private createTree(plugin: Plugin) {
const tree = cli.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;
}
}