| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- "use strict";
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
- return new (P || (P = Promise))(function (resolve, reject) {
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
- function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
- step((generator = generator.apply(thisArg, _arguments || [])).next());
- });
- };
- var __generator = (this && this.__generator) || function (thisArg, body) {
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
- function verb(n) { return function (v) { return step([n, v]); }; }
- function step(op) {
- if (f) throw new TypeError("Generator is already executing.");
- while (_) try {
- if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
- if (y = 0, t) op = [0, t.value];
- switch (op[0]) {
- case 0: case 1: t = op; break;
- case 4: _.label++; return { value: op[1], done: false };
- case 5: _.label++; y = op[1]; op = [0]; continue;
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
- default:
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
- if (t[2]) _.ops.pop();
- _.trys.pop(); continue;
- }
- op = body.call(thisArg, _);
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
- }
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- var debug = require("debug");
- var fs = require("fs-extra");
- var path = require("path");
- var depTypes_1 = require("./depTypes");
- var d = debug('flora-colossus');
- var Walker = /** @class */ (function () {
- function Walker(modulePath) {
- this.walkHistory = new Set();
- this.cache = null;
- if (!modulePath || typeof modulePath !== 'string') {
- throw new Error('modulePath must be provided as a string');
- }
- d("creating walker with rootModule=" + modulePath);
- this.rootModule = modulePath;
- }
- Walker.prototype.relativeModule = function (rootPath, moduleName) {
- return path.resolve(rootPath, 'node_modules', moduleName);
- };
- Walker.prototype.loadPackageJSON = function (modulePath) {
- return __awaiter(this, void 0, void 0, function () {
- var pJPath, pJ;
- return __generator(this, function (_a) {
- switch (_a.label) {
- case 0:
- pJPath = path.resolve(modulePath, 'package.json');
- return [4 /*yield*/, fs.pathExists(pJPath)];
- case 1:
- if (!_a.sent()) return [3 /*break*/, 3];
- return [4 /*yield*/, fs.readJson(pJPath)];
- case 2:
- pJ = _a.sent();
- if (!pJ.dependencies)
- pJ.dependencies = {};
- if (!pJ.devDependencies)
- pJ.devDependencies = {};
- if (!pJ.optionalDependencies)
- pJ.optionalDependencies = {};
- return [2 /*return*/, pJ];
- case 3: return [2 /*return*/, null];
- }
- });
- });
- };
- Walker.prototype.walkDependenciesForModuleInModule = function (moduleName, modulePath, depType) {
- return __awaiter(this, void 0, void 0, function () {
- var testPath, discoveredPath, lastRelative;
- return __generator(this, function (_a) {
- switch (_a.label) {
- case 0:
- testPath = modulePath;
- discoveredPath = null;
- lastRelative = null;
- _a.label = 1;
- case 1:
- if (!(!discoveredPath && this.relativeModule(testPath, moduleName) !== lastRelative)) return [3 /*break*/, 3];
- lastRelative = this.relativeModule(testPath, moduleName);
- return [4 /*yield*/, fs.pathExists(lastRelative)];
- case 2:
- if (_a.sent()) {
- discoveredPath = lastRelative;
- }
- else {
- if (path.basename(path.dirname(testPath)) !== 'node_modules') {
- testPath = path.dirname(testPath);
- }
- testPath = path.dirname(path.dirname(testPath));
- }
- return [3 /*break*/, 1];
- case 3:
- // If we can't find it the install is probably buggered
- if (!discoveredPath && depType !== depTypes_1.DepType.OPTIONAL && depType !== depTypes_1.DepType.DEV_OPTIONAL) {
- 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.");
- }
- if (!discoveredPath) return [3 /*break*/, 5];
- return [4 /*yield*/, this.walkDependenciesForModule(discoveredPath, depType)];
- case 4:
- _a.sent();
- _a.label = 5;
- case 5: return [2 /*return*/];
- }
- });
- });
- };
- Walker.prototype.walkDependenciesForModule = function (modulePath, depType) {
- return __awaiter(this, void 0, void 0, function () {
- var existingModule, pJ, _a, _b, _i, moduleName, _c, _d, _e, moduleName, _f, _g, _h, moduleName;
- return __generator(this, function (_j) {
- switch (_j.label) {
- case 0:
- d('walk reached:', modulePath, ' Type is:', depTypes_1.DepType[depType]);
- // We have already traversed this module
- if (this.walkHistory.has(modulePath)) {
- d('already walked this route');
- existingModule = this.modules.find(function (module) { return module.path === modulePath; });
- // If the depType we are traversing with now is higher than the
- // last traversal then update it (prod superseeds dev for instance)
- if (depTypes_1.depTypeGreater(depType, existingModule.depType)) {
- d("existing module has a type of \"" + existingModule.depType + "\", new module type would be \"" + depType + "\" therefore updating");
- existingModule.depType = depType;
- }
- return [2 /*return*/];
- }
- return [4 /*yield*/, this.loadPackageJSON(modulePath)];
- case 1:
- pJ = _j.sent();
- // If the module doesn't have a package.json file it is probably a
- // dead install from yarn (they dont clean up for some reason)
- if (!pJ) {
- d('walk hit a dead end, this module is incomplete');
- return [2 /*return*/];
- }
- // Record this module as being traversed
- this.walkHistory.add(modulePath);
- this.modules.push({
- depType: depType,
- path: modulePath,
- name: pJ.name,
- });
- _a = [];
- for (_b in pJ.dependencies)
- _a.push(_b);
- _i = 0;
- _j.label = 2;
- case 2:
- if (!(_i < _a.length)) return [3 /*break*/, 5];
- moduleName = _a[_i];
- // npm decides it's a funny thing to put optional dependencies in the "dependencies" section
- // after install, because that makes perfect sense
- if (moduleName in pJ.optionalDependencies) {
- d("found " + moduleName + " in prod deps of " + modulePath + " but it is also marked optional");
- return [3 /*break*/, 4];
- }
- return [4 /*yield*/, this.walkDependenciesForModuleInModule(moduleName, modulePath, depTypes_1.childDepType(depType, depTypes_1.DepType.PROD))];
- case 3:
- _j.sent();
- _j.label = 4;
- case 4:
- _i++;
- return [3 /*break*/, 2];
- case 5:
- if (!(depType === depTypes_1.DepType.ROOT)) return [3 /*break*/, 9];
- d('we\'re still at the beginning, walking down the dev route');
- _c = [];
- for (_d in pJ.devDependencies)
- _c.push(_d);
- _e = 0;
- _j.label = 6;
- case 6:
- if (!(_e < _c.length)) return [3 /*break*/, 9];
- moduleName = _c[_e];
- return [4 /*yield*/, this.walkDependenciesForModuleInModule(moduleName, modulePath, depTypes_1.childDepType(depType, depTypes_1.DepType.DEV))];
- case 7:
- _j.sent();
- _j.label = 8;
- case 8:
- _e++;
- return [3 /*break*/, 6];
- case 9:
- _f = [];
- for (_g in pJ.optionalDependencies)
- _f.push(_g);
- _h = 0;
- _j.label = 10;
- case 10:
- if (!(_h < _f.length)) return [3 /*break*/, 13];
- moduleName = _f[_h];
- return [4 /*yield*/, this.walkDependenciesForModuleInModule(moduleName, modulePath, depTypes_1.childDepType(depType, depTypes_1.DepType.OPTIONAL))];
- case 11:
- _j.sent();
- _j.label = 12;
- case 12:
- _h++;
- return [3 /*break*/, 10];
- case 13: return [2 /*return*/];
- }
- });
- });
- };
- Walker.prototype.walkTree = function () {
- return __awaiter(this, void 0, void 0, function () {
- var _this = this;
- return __generator(this, function (_a) {
- switch (_a.label) {
- case 0:
- d('starting tree walk');
- if (!this.cache) {
- this.cache = new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
- var err_1;
- return __generator(this, function (_a) {
- switch (_a.label) {
- case 0:
- this.modules = [];
- _a.label = 1;
- case 1:
- _a.trys.push([1, 3, , 4]);
- return [4 /*yield*/, this.walkDependenciesForModule(this.rootModule, depTypes_1.DepType.ROOT)];
- case 2:
- _a.sent();
- return [3 /*break*/, 4];
- case 3:
- err_1 = _a.sent();
- reject(err_1);
- return [2 /*return*/];
- case 4:
- resolve(this.modules);
- return [2 /*return*/];
- }
- });
- }); });
- }
- else {
- d('tree walk in progress / completed already, waiting for existing walk to complete');
- }
- return [4 /*yield*/, this.cache];
- case 1: return [2 /*return*/, _a.sent()];
- }
- });
- });
- };
- Walker.prototype.getRootModule = function () {
- return this.rootModule;
- };
- return Walker;
- }());
- exports.Walker = Walker;
- //# sourceMappingURL=Walker.js.map
|