-
Notifications
You must be signed in to change notification settings - Fork 23
/
uninstall.ts
87 lines (67 loc) · 2.42 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
84
85
86
87
/* eslint-disable no-await-in-loop */
import {Args, Command, Flags, ux} from '@oclif/core'
import {bold} from 'ansis'
import {determineLogLevel} from '../../log-level.js'
import Plugins from '../../plugins.js'
function removeTags(plugin: string): string {
if (plugin.includes('@')) {
const chunked = plugin.split('@')
const last = chunked.at(-1)
if (!last?.includes('/') && chunked.length > 1) {
chunked.pop()
}
return chunked.join('@')
}
return plugin
}
export default class PluginsUninstall extends Command {
static aliases = ['plugins:unlink', 'plugins:remove']
static args = {
plugin: Args.string({description: 'plugin to uninstall'}),
}
static description = 'Removes a plugin from the CLI.'
static examples = ['<%= config.bin %> <%= command.id %> <%- config.pjson.oclif.examplePlugin || "myplugin" %>']
static flags = {
help: Flags.help({char: 'h'}),
verbose: Flags.boolean({char: 'v'}),
}
static strict = false
// In this case we want these operations to happen
// sequentially so the `no-await-in-loop` rule is ignored
async run(): Promise<void> {
const {argv, flags} = await this.parse(PluginsUninstall)
const plugins = new Plugins({
config: this.config,
logLevel: determineLogLevel(this.config, flags, 'silent'),
})
const pluginNameToDelete: string[] = []
if (argv.length === 0) argv.push('.')
for (const plugin of argv as string[]) {
const friendly = removeTags(plugins.friendlyName(plugin))
const unfriendly = await plugins.hasPlugin(removeTags(plugin))
if (!unfriendly) {
const p = this.config.getPluginsList().find((p) => p.name === plugin)
if (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 {
const {name} = unfriendly
const displayName = friendly === '.' ? name : (friendly ?? name)
ux.action.start(`${this.config.name}: Uninstalling ${displayName}`)
await plugins.uninstall(name)
pluginNameToDelete.push(name)
} catch (error) {
ux.action.stop(bold.red('failed'))
throw error
}
ux.action.stop()
}
for (const p of pluginNameToDelete) {
this.config.plugins.delete(p)
}
await this.config.runHook('plugins:postuninstall', {})
}
}