summaryrefslogtreecommitdiff
path: root/desktop/node_modules/galactus/lib/DestroyerOfModules.js
diff options
context:
space:
mode:
authorRaindropsSys <raindrops@equestria.dev>2023-10-24 17:43:37 +0200
committerRaindropsSys <raindrops@equestria.dev>2023-10-24 17:43:37 +0200
commitae187b6d75c8079da0be1dc288613bad8466fe61 (patch)
tree5ea0d34185a2270f29ffaa65e1f5258028d7d5d0 /desktop/node_modules/galactus/lib/DestroyerOfModules.js
downloadmist-ae187b6d75c8079da0be1dc288613bad8466fe61.tar.gz
mist-ae187b6d75c8079da0be1dc288613bad8466fe61.tar.bz2
mist-ae187b6d75c8079da0be1dc288613bad8466fe61.zip
Initial commit
Diffstat (limited to 'desktop/node_modules/galactus/lib/DestroyerOfModules.js')
-rw-r--r--desktop/node_modules/galactus/lib/DestroyerOfModules.js69
1 files changed, 69 insertions, 0 deletions
diff --git a/desktop/node_modules/galactus/lib/DestroyerOfModules.js b/desktop/node_modules/galactus/lib/DestroyerOfModules.js
new file mode 100644
index 0000000..f1d1a09
--- /dev/null
+++ b/desktop/node_modules/galactus/lib/DestroyerOfModules.js
@@ -0,0 +1,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 \ No newline at end of file