-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathremove.js
83 lines (67 loc) · 2.37 KB
/
remove.js
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
/* @flow */
import type {Reporter} from '../../reporters/index.js';
import type Config from '../../config.js';
import Lockfile from '../../lockfile/wrapper.js';
import {registries} from '../../registries/index.js';
import {Install} from './install.js';
import {MessageError} from '../../errors.js';
import {NoopReporter} from '../../reporters/index.js';
import * as fs from '../../util/fs.js';
import * as constants from '../../constants.js';
const path = require('path');
export const requireLockfile = true;
export async function run(
config: Config,
reporter: Reporter,
flags: Object,
args: Array<string>,
): Promise<void> {
if (!args.length) {
throw new MessageError(reporter.lang('tooFewArguments', 1));
}
const totalSteps = args.length + 1;
let step = 0;
// load manifests
const lockfile = await Lockfile.fromDirectory(config.cwd);
const rootManifests = await config.getRootManifests();
const manifests = [];
for (const name of args) {
reporter.step(++step, totalSteps, `Removing module ${name}`);
let found = false;
for (const registryName of Object.keys(registries)) {
const registry = config.registries[registryName];
const object = rootManifests[registryName].object;
for (const type of constants.DEPENDENCY_TYPES) {
const deps = object[type];
if (deps && deps[name]) {
found = true;
delete deps[name];
}
}
const possibleManifestLoc = path.join(config.cwd, registry.folder, name);
if (await fs.exists(possibleManifestLoc)) {
manifests.push([
possibleManifestLoc,
await config.readManifest(possibleManifestLoc, registryName),
]);
}
}
if (!found) {
throw new MessageError(reporter.lang('moduleNotInManifest'));
}
}
// save manifests
await config.saveRootManifests(rootManifests);
// run hooks - npm runs these one after another
for (const action of ['preuninstall', 'uninstall', 'postuninstall']) {
for (const [loc] of manifests) {
await config.executeLifecycleScript(action, loc);
}
}
// reinstall so we can get the updated lockfile
reporter.step(++step, totalSteps, reporter.lang('uninstallRegenerate'));
const reinstall = new Install({force: true, ...flags}, config, new NoopReporter(), lockfile);
await reinstall.init();
//
reporter.success(reporter.lang('uninstalledPackages'));
}