Walker.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 debug = require("debug");
  39. var fs = require("fs-extra");
  40. var path = require("path");
  41. var depTypes_1 = require("./depTypes");
  42. var d = debug('flora-colossus');
  43. var Walker = /** @class */ (function () {
  44. function Walker(modulePath) {
  45. this.walkHistory = new Set();
  46. this.cache = null;
  47. if (!modulePath || typeof modulePath !== 'string') {
  48. throw new Error('modulePath must be provided as a string');
  49. }
  50. d("creating walker with rootModule=" + modulePath);
  51. this.rootModule = modulePath;
  52. }
  53. Walker.prototype.relativeModule = function (rootPath, moduleName) {
  54. return path.resolve(rootPath, 'node_modules', moduleName);
  55. };
  56. Walker.prototype.loadPackageJSON = function (modulePath) {
  57. return __awaiter(this, void 0, void 0, function () {
  58. var pJPath, pJ;
  59. return __generator(this, function (_a) {
  60. switch (_a.label) {
  61. case 0:
  62. pJPath = path.resolve(modulePath, 'package.json');
  63. return [4 /*yield*/, fs.pathExists(pJPath)];
  64. case 1:
  65. if (!_a.sent()) return [3 /*break*/, 3];
  66. return [4 /*yield*/, fs.readJson(pJPath)];
  67. case 2:
  68. pJ = _a.sent();
  69. if (!pJ.dependencies)
  70. pJ.dependencies = {};
  71. if (!pJ.devDependencies)
  72. pJ.devDependencies = {};
  73. if (!pJ.optionalDependencies)
  74. pJ.optionalDependencies = {};
  75. return [2 /*return*/, pJ];
  76. case 3: return [2 /*return*/, null];
  77. }
  78. });
  79. });
  80. };
  81. Walker.prototype.walkDependenciesForModuleInModule = function (moduleName, modulePath, depType) {
  82. return __awaiter(this, void 0, void 0, function () {
  83. var testPath, discoveredPath, lastRelative;
  84. return __generator(this, function (_a) {
  85. switch (_a.label) {
  86. case 0:
  87. testPath = modulePath;
  88. discoveredPath = null;
  89. lastRelative = null;
  90. _a.label = 1;
  91. case 1:
  92. if (!(!discoveredPath && this.relativeModule(testPath, moduleName) !== lastRelative)) return [3 /*break*/, 3];
  93. lastRelative = this.relativeModule(testPath, moduleName);
  94. return [4 /*yield*/, fs.pathExists(lastRelative)];
  95. case 2:
  96. if (_a.sent()) {
  97. discoveredPath = lastRelative;
  98. }
  99. else {
  100. if (path.basename(path.dirname(testPath)) !== 'node_modules') {
  101. testPath = path.dirname(testPath);
  102. }
  103. testPath = path.dirname(path.dirname(testPath));
  104. }
  105. return [3 /*break*/, 1];
  106. case 3:
  107. // If we can't find it the install is probably buggered
  108. if (!discoveredPath && depType !== depTypes_1.DepType.OPTIONAL && depType !== depTypes_1.DepType.DEV_OPTIONAL) {
  109. throw new Error("Failed to locate module \"" + moduleName + "\" from \"" + modulePath + "\"\n\n This normally means that either you have deleted this package already somehow (check your ignore settings if using electron-packager). Or your module installation failed.");
  110. }
  111. if (!discoveredPath) return [3 /*break*/, 5];
  112. return [4 /*yield*/, this.walkDependenciesForModule(discoveredPath, depType)];
  113. case 4:
  114. _a.sent();
  115. _a.label = 5;
  116. case 5: return [2 /*return*/];
  117. }
  118. });
  119. });
  120. };
  121. Walker.prototype.walkDependenciesForModule = function (modulePath, depType) {
  122. return __awaiter(this, void 0, void 0, function () {
  123. var existingModule, pJ, _a, _b, _i, moduleName, _c, _d, _e, moduleName, _f, _g, _h, moduleName;
  124. return __generator(this, function (_j) {
  125. switch (_j.label) {
  126. case 0:
  127. d('walk reached:', modulePath, ' Type is:', depTypes_1.DepType[depType]);
  128. // We have already traversed this module
  129. if (this.walkHistory.has(modulePath)) {
  130. d('already walked this route');
  131. existingModule = this.modules.find(function (module) { return module.path === modulePath; });
  132. // If the depType we are traversing with now is higher than the
  133. // last traversal then update it (prod superseeds dev for instance)
  134. if (depTypes_1.depTypeGreater(depType, existingModule.depType)) {
  135. d("existing module has a type of \"" + existingModule.depType + "\", new module type would be \"" + depType + "\" therefore updating");
  136. existingModule.depType = depType;
  137. }
  138. return [2 /*return*/];
  139. }
  140. return [4 /*yield*/, this.loadPackageJSON(modulePath)];
  141. case 1:
  142. pJ = _j.sent();
  143. // If the module doesn't have a package.json file it is probably a
  144. // dead install from yarn (they dont clean up for some reason)
  145. if (!pJ) {
  146. d('walk hit a dead end, this module is incomplete');
  147. return [2 /*return*/];
  148. }
  149. // Record this module as being traversed
  150. this.walkHistory.add(modulePath);
  151. this.modules.push({
  152. depType: depType,
  153. path: modulePath,
  154. name: pJ.name,
  155. });
  156. _a = [];
  157. for (_b in pJ.dependencies)
  158. _a.push(_b);
  159. _i = 0;
  160. _j.label = 2;
  161. case 2:
  162. if (!(_i < _a.length)) return [3 /*break*/, 5];
  163. moduleName = _a[_i];
  164. // npm decides it's a funny thing to put optional dependencies in the "dependencies" section
  165. // after install, because that makes perfect sense
  166. if (moduleName in pJ.optionalDependencies) {
  167. d("found " + moduleName + " in prod deps of " + modulePath + " but it is also marked optional");
  168. return [3 /*break*/, 4];
  169. }
  170. return [4 /*yield*/, this.walkDependenciesForModuleInModule(moduleName, modulePath, depTypes_1.childDepType(depType, depTypes_1.DepType.PROD))];
  171. case 3:
  172. _j.sent();
  173. _j.label = 4;
  174. case 4:
  175. _i++;
  176. return [3 /*break*/, 2];
  177. case 5:
  178. if (!(depType === depTypes_1.DepType.ROOT)) return [3 /*break*/, 9];
  179. d('we\'re still at the beginning, walking down the dev route');
  180. _c = [];
  181. for (_d in pJ.devDependencies)
  182. _c.push(_d);
  183. _e = 0;
  184. _j.label = 6;
  185. case 6:
  186. if (!(_e < _c.length)) return [3 /*break*/, 9];
  187. moduleName = _c[_e];
  188. return [4 /*yield*/, this.walkDependenciesForModuleInModule(moduleName, modulePath, depTypes_1.childDepType(depType, depTypes_1.DepType.DEV))];
  189. case 7:
  190. _j.sent();
  191. _j.label = 8;
  192. case 8:
  193. _e++;
  194. return [3 /*break*/, 6];
  195. case 9:
  196. _f = [];
  197. for (_g in pJ.optionalDependencies)
  198. _f.push(_g);
  199. _h = 0;
  200. _j.label = 10;
  201. case 10:
  202. if (!(_h < _f.length)) return [3 /*break*/, 13];
  203. moduleName = _f[_h];
  204. return [4 /*yield*/, this.walkDependenciesForModuleInModule(moduleName, modulePath, depTypes_1.childDepType(depType, depTypes_1.DepType.OPTIONAL))];
  205. case 11:
  206. _j.sent();
  207. _j.label = 12;
  208. case 12:
  209. _h++;
  210. return [3 /*break*/, 10];
  211. case 13: return [2 /*return*/];
  212. }
  213. });
  214. });
  215. };
  216. Walker.prototype.walkTree = function () {
  217. return __awaiter(this, void 0, void 0, function () {
  218. var _this = this;
  219. return __generator(this, function (_a) {
  220. switch (_a.label) {
  221. case 0:
  222. d('starting tree walk');
  223. if (!this.cache) {
  224. this.cache = new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
  225. var err_1;
  226. return __generator(this, function (_a) {
  227. switch (_a.label) {
  228. case 0:
  229. this.modules = [];
  230. _a.label = 1;
  231. case 1:
  232. _a.trys.push([1, 3, , 4]);
  233. return [4 /*yield*/, this.walkDependenciesForModule(this.rootModule, depTypes_1.DepType.ROOT)];
  234. case 2:
  235. _a.sent();
  236. return [3 /*break*/, 4];
  237. case 3:
  238. err_1 = _a.sent();
  239. reject(err_1);
  240. return [2 /*return*/];
  241. case 4:
  242. resolve(this.modules);
  243. return [2 /*return*/];
  244. }
  245. });
  246. }); });
  247. }
  248. else {
  249. d('tree walk in progress / completed already, waiting for existing walk to complete');
  250. }
  251. return [4 /*yield*/, this.cache];
  252. case 1: return [2 /*return*/, _a.sent()];
  253. }
  254. });
  255. });
  256. };
  257. Walker.prototype.getRootModule = function () {
  258. return this.rootModule;
  259. };
  260. return Walker;
  261. }());
  262. exports.Walker = Walker;
  263. //# sourceMappingURL=Walker.js.map