DestroyerOfModules.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. return new (P || (P = Promise))(function (resolve, reject) {
  4. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  5. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  6. function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
  7. step((generator = generator.apply(thisArg, _arguments || [])).next());
  8. });
  9. };
  10. var __generator = (this && this.__generator) || function (thisArg, body) {
  11. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  12. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  13. function verb(n) { return function (v) { return step([n, v]); }; }
  14. function step(op) {
  15. if (f) throw new TypeError("Generator is already executing.");
  16. while (_) try {
  17. if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
  18. if (y = 0, t) op = [0, t.value];
  19. switch (op[0]) {
  20. case 0: case 1: t = op; break;
  21. case 4: _.label++; return { value: op[1], done: false };
  22. case 5: _.label++; y = op[1]; op = [0]; continue;
  23. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  24. default:
  25. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  26. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  27. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  28. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  29. if (t[2]) _.ops.pop();
  30. _.trys.pop(); continue;
  31. }
  32. op = body.call(thisArg, _);
  33. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  34. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  35. }
  36. };
  37. Object.defineProperty(exports, "__esModule", { value: true });
  38. var fs = require("fs-extra");
  39. var path = require("path");
  40. var flora_colossus_1 = require("flora-colossus");
  41. var DestroyerOfModules = /** @class */ (function () {
  42. function DestroyerOfModules(_a) {
  43. var rootDirectory = _a.rootDirectory, walker = _a.walker, shouldKeepModuleTest = _a.shouldKeepModuleTest;
  44. if (rootDirectory) {
  45. this.walker = new flora_colossus_1.Walker(rootDirectory);
  46. }
  47. else if (walker) {
  48. this.walker = walker;
  49. }
  50. else {
  51. throw new Error('Must either provide rootDirectory or walker argument');
  52. }
  53. if (shouldKeepModuleTest) {
  54. this.shouldKeepFn = shouldKeepModuleTest;
  55. }
  56. }
  57. DestroyerOfModules.prototype.destroyModule = function (modulePath, moduleMap) {
  58. return __awaiter(this, void 0, void 0, function () {
  59. var module, nodeModulesPath, _i, _a, subModuleName, _b, _c, subScopedModuleName;
  60. return __generator(this, function (_d) {
  61. switch (_d.label) {
  62. case 0:
  63. module = moduleMap.get(modulePath);
  64. if (!module) return [3 /*break*/, 13];
  65. nodeModulesPath = path.resolve(modulePath, 'node_modules');
  66. return [4 /*yield*/, fs.pathExists(nodeModulesPath)];
  67. case 1:
  68. if (!(_d.sent())) {
  69. return [2 /*return*/];
  70. }
  71. _i = 0;
  72. return [4 /*yield*/, fs.readdir(nodeModulesPath)];
  73. case 2:
  74. _a = _d.sent();
  75. _d.label = 3;
  76. case 3:
  77. if (!(_i < _a.length)) return [3 /*break*/, 12];
  78. subModuleName = _a[_i];
  79. if (!subModuleName.startsWith('@')) return [3 /*break*/, 9];
  80. _b = 0;
  81. return [4 /*yield*/, fs.readdir(path.resolve(nodeModulesPath, subModuleName))];
  82. case 4:
  83. _c = _d.sent();
  84. _d.label = 5;
  85. case 5:
  86. if (!(_b < _c.length)) return [3 /*break*/, 8];
  87. subScopedModuleName = _c[_b];
  88. return [4 /*yield*/, this.destroyModule(path.resolve(nodeModulesPath, subModuleName, subScopedModuleName), moduleMap)];
  89. case 6:
  90. _d.sent();
  91. _d.label = 7;
  92. case 7:
  93. _b++;
  94. return [3 /*break*/, 5];
  95. case 8: return [3 /*break*/, 11];
  96. case 9: return [4 /*yield*/, this.destroyModule(path.resolve(nodeModulesPath, subModuleName), moduleMap)];
  97. case 10:
  98. _d.sent();
  99. _d.label = 11;
  100. case 11:
  101. _i++;
  102. return [3 /*break*/, 3];
  103. case 12: return [3 /*break*/, 15];
  104. case 13: return [4 /*yield*/, fs.remove(modulePath)];
  105. case 14:
  106. _d.sent();
  107. _d.label = 15;
  108. case 15: return [2 /*return*/];
  109. }
  110. });
  111. });
  112. };
  113. DestroyerOfModules.prototype.collectKeptModules = function (_a) {
  114. var _b = _a.relativePaths, relativePaths = _b === void 0 ? false : _b;
  115. return __awaiter(this, void 0, void 0, function () {
  116. var modules, moduleMap, rootPath, _i, modules_1, module_1, modulePath;
  117. return __generator(this, function (_c) {
  118. switch (_c.label) {
  119. case 0: return [4 /*yield*/, this.walker.walkTree()];
  120. case 1:
  121. modules = _c.sent();
  122. moduleMap = new Map();
  123. rootPath = path.resolve(this.walker.getRootModule());
  124. for (_i = 0, modules_1 = modules; _i < modules_1.length; _i++) {
  125. module_1 = modules_1[_i];
  126. if (this.shouldKeepModule(module_1)) {
  127. modulePath = module_1.path;
  128. if (relativePaths) {
  129. modulePath = modulePath.replace("" + rootPath + path.sep, '');
  130. }
  131. moduleMap.set(modulePath, module_1);
  132. }
  133. }
  134. return [2 /*return*/, moduleMap];
  135. }
  136. });
  137. });
  138. };
  139. DestroyerOfModules.prototype.destroy = function () {
  140. return __awaiter(this, void 0, void 0, function () {
  141. var _a, _b;
  142. return __generator(this, function (_c) {
  143. switch (_c.label) {
  144. case 0:
  145. _a = this.destroyModule;
  146. _b = [this.walker.getRootModule()];
  147. return [4 /*yield*/, this.collectKeptModules({ relativePaths: false })];
  148. case 1: return [4 /*yield*/, _a.apply(this, _b.concat([_c.sent()]))];
  149. case 2:
  150. _c.sent();
  151. return [2 /*return*/];
  152. }
  153. });
  154. });
  155. };
  156. DestroyerOfModules.prototype.shouldKeepModule = function (module) {
  157. var isDevDep = module.depType === flora_colossus_1.DepType.DEV || module.depType === flora_colossus_1.DepType.DEV_OPTIONAL;
  158. var shouldKeep = this.shouldKeepFn ? this.shouldKeepFn(module, isDevDep) : !isDevDep;
  159. return shouldKeep;
  160. };
  161. return DestroyerOfModules;
  162. }());
  163. exports.DestroyerOfModules = DestroyerOfModules;
  164. //# sourceMappingURL=DestroyerOfModules.js.map