-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
remove.ts
76 lines (61 loc) Β· 2.86 KB
/
remove.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
import {BaseCommand} from '@yarnpkg/cli';
import {Configuration, MessageName, Project, StreamReport, formatUtils, structUtils} from '@yarnpkg/core';
import {PortablePath, ppath, xfs} from '@yarnpkg/fslib';
import {Command, Option, Usage, UsageError} from 'clipanion';
// eslint-disable-next-line arca/no-default-export
export default class PluginRemoveCommand extends BaseCommand {
static paths = [
[`plugin`, `remove`],
];
static usage: Usage = Command.Usage({
category: `Plugin-related commands`,
description: `remove a plugin`,
details: `
This command deletes the specified plugin from the .yarn/plugins folder and removes it from the configuration.
**Note:** The plugins have to be referenced by their name property, which can be obtained using the \`yarn plugin runtime\` command. Shorthands are not allowed.
`,
examples: [[
`Remove a plugin imported from the Yarn repository`,
`$0 plugin remove @yarnpkg/plugin-typescript`,
], [
`Remove a plugin imported from a local file`,
`$0 plugin remove my-local-plugin`,
]],
});
name = Option.String();
async execute() {
const configuration = await Configuration.find(this.context.cwd, this.context.plugins);
const {project} = await Project.find(configuration, this.context.cwd);
const report = await StreamReport.start({
configuration,
stdout: this.context.stdout,
}, async report => {
const pluginName = this.name;
const pluginIdent = structUtils.parseIdent(pluginName);
if (!configuration.plugins.has(pluginName))
throw new UsageError(`${structUtils.prettyIdent(configuration, pluginIdent)} isn't referenced by the current configuration`);
const relativePath = `.yarn/plugins/${pluginName}.cjs` as PortablePath;
const absolutePath = ppath.resolve(project.cwd, relativePath);
if (xfs.existsSync(absolutePath)) {
report.reportInfo(MessageName.UNNAMED, `Removing ${formatUtils.pretty(configuration, relativePath, formatUtils.Type.PATH)}...`);
await xfs.removePromise(absolutePath);
}
report.reportInfo(MessageName.UNNAMED, `Updating the configuration...`);
await Configuration.updateConfiguration(project.cwd, {
plugins: plugins => {
if (!Array.isArray(plugins))
return plugins;
const filteredPlugins = plugins.filter((plugin: {path: string}) => {
return plugin.path !== relativePath;
});
if (filteredPlugins.length === 0)
return Configuration.deleteProperty;
if (filteredPlugins.length === plugins.length)
return plugins;
return filteredPlugins;
},
});
});
return report.exitCode();
}
}