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
|
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DestroyerOfModules = void 0;
const fs = require("fs-extra");
const path = require("path");
const flora_colossus_1 = require("flora-colossus");
class DestroyerOfModules {
constructor({ rootDirectory, walker, shouldKeepModuleTest, }) {
if (rootDirectory) {
this.walker = new flora_colossus_1.Walker(rootDirectory);
}
else if (walker) {
this.walker = walker;
}
else {
throw new Error('Must either provide rootDirectory or walker argument');
}
if (shouldKeepModuleTest) {
this.shouldKeepFn = shouldKeepModuleTest;
}
}
async destroyModule(modulePath, moduleMap) {
const module = moduleMap.get(modulePath);
if (module) {
const nodeModulesPath = path.resolve(modulePath, 'node_modules');
if (!await fs.pathExists(nodeModulesPath)) {
return;
}
for (const subModuleName of await fs.readdir(nodeModulesPath)) {
if (subModuleName.startsWith('@')) {
for (const subScopedModuleName of await fs.readdir(path.resolve(nodeModulesPath, subModuleName))) {
await this.destroyModule(path.resolve(nodeModulesPath, subModuleName, subScopedModuleName), moduleMap);
}
}
else {
await this.destroyModule(path.resolve(nodeModulesPath, subModuleName), moduleMap);
}
}
}
else {
await fs.remove(modulePath);
}
}
async collectKeptModules({ relativePaths = false }) {
const modules = await this.walker.walkTree();
const moduleMap = new Map();
const rootPath = path.resolve(this.walker.getRootModule());
for (const module of modules) {
if (this.shouldKeepModule(module)) {
let modulePath = module.path;
if (relativePaths) {
modulePath = modulePath.replace(`${rootPath}${path.sep}`, '');
}
moduleMap.set(modulePath, module);
}
}
return moduleMap;
}
async destroy() {
await this.destroyModule(this.walker.getRootModule(), await this.collectKeptModules({ relativePaths: false }));
}
shouldKeepModule(module) {
const isDevDep = module.depType === flora_colossus_1.DepType.DEV || module.depType === flora_colossus_1.DepType.DEV_OPTIONAL;
const shouldKeep = this.shouldKeepFn ? this.shouldKeepFn(module, isDevDep) : !isDevDep;
return shouldKeep;
}
}
exports.DestroyerOfModules = DestroyerOfModules;
//# sourceMappingURL=DestroyerOfModules.js.map
|