forked from oclif/plugin-plugins
-
Notifications
You must be signed in to change notification settings - Fork 1
/
uninstall.ts
83 lines (68 loc) · 2.36 KB
/
uninstall.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
import { Command, flags } from "@oclif/command";
import { Plugin } from "@oclif/config";
import chalk from "chalk";
import cli from "cli-ux";
import Plugins from "../../modules/plugins";
import { ColorifyConstants } from "vtex";
export default class PluginsUninstall extends Command {
static description = "Removes a plugin from the CLI";
static usage = "plugins uninstall PLUGIN";
static help = `${ColorifyConstants.COMMAND_OR_VTEX_REF(
"vtex plugins uninstall"
)} autoupdate`;
static variableArgs = true;
static args = [{ name: "plugin", description: "Plugin to uninstall." }];
static flags = {
help: flags.help({ char: "h" }),
verbose: flags.boolean({ char: "v" }),
};
static aliases = ["plugins:unlink", "plugins:remove"];
plugins = new Plugins(this.config);
// In this case we want these operations to happen
// sequentially so the `no-await-in-loop` rule is ugnored
/* eslint-disable no-await-in-loop */
async run() {
const { flags, argv } = this.parse(PluginsUninstall);
this.plugins = new Plugins(this.config);
if (flags.verbose) this.plugins.verbose = true;
if (argv.length === 0) argv.push(".");
for (const plugin of argv) {
const friendly = this.removeTags(this.plugins.friendlyName(plugin));
cli.action.start(`Uninstalling ${friendly}`);
const unfriendly = await this.plugins.hasPlugin(this.removeTags(plugin));
if (!unfriendly) {
const p = this.config.plugins.find((p) => p.name === plugin) as
| Plugin
| undefined;
if (p) {
if (p && p.parent)
return this.error(
`${friendly} is installed via plugin ${
p.parent!.name
}, uninstall ${p.parent!.name} instead`
);
}
return this.error(`${friendly} is not installed`);
}
try {
await this.plugins.uninstall(unfriendly.name);
} catch (error) {
cli.action.stop(chalk.bold.red("failed"));
throw error;
}
cli.action.stop();
}
}
/* eslint-enable no-await-in-loop */
private removeTags(plugin: string) {
if (plugin.includes("@")) {
const chunked = plugin.split("@");
const last = chunked[chunked.length - 1];
if (!last.includes("/") && chunked.length > 1) {
chunked.pop();
}
return chunked.join("@");
}
return plugin;
}
}