UNPKG

@teambit/isolator

Version:
100 lines (96 loc) 3.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; function _graph() { const data = require("@teambit/graph.cleargraph"); _graph = function () { return data; }; return data; } function _path() { const data = require("path"); _path = function () { return data; }; return data; } class CapsuleList extends Array { getCapsule(id) { return this.find(capsule => capsule.component.id.isEqual(id)); } getCapsuleByLegacyId(id) { return this.find(capsule => capsule.component.id.isEqual(id)); } getCapsuleIgnoreVersion(id) { return this.find(capsule => capsule.component.id.isEqual(id, { ignoreVersion: true })); } getAllCapsuleDirs() { return this.map(capsule => capsule.path); } getIdByPathInCapsule(pathInCapsule) { const normalizedPathInCapsule = (0, _path().normalize)(pathInCapsule); const found = this.find(capsule => normalizedPathInCapsule === (0, _path().normalize)(capsule.path)); return found ? found.component.id : null; } getAllComponents() { return this.map(c => c.component); } getAllComponentIDs() { return this.map(c => c.component.id); } getGraphIds() { const components = this.getAllComponents(); const graph = new (_graph().Graph)(); components.forEach(comp => graph.setNode(new (_graph().Node)(comp.id.toString(), comp))); const compIdsStr = components.map(c => c.id.toString()); components.forEach(comp => { const deps = comp.getDependencies(); deps.forEach(dep => { if (compIdsStr.includes(dep.id)) { graph.setEdge(new (_graph().Edge)(comp.id.toString(), dep.id, dep.type)); } }); }); return graph; } // Sort the capsules by their dependencies. The capsules with no dependencies will be first. Returns a new array. async toposort(depResolver) { const components = this.getAllComponents(); const graph = new (_graph().Graph)(); // Build a graph with all the components from the current capsule list components.forEach(comp => graph.setNode(new (_graph().Node)(depResolver.getPackageName(comp), comp))); // Add edges between the components according to their interdependencies for (const node of graph.nodes) { // eslint-disable-next-line no-await-in-loop const deps = await depResolver.getDependencies(node.attr); deps.forEach(dep => { const depPkgName = dep.getPackageName?.(); if (depPkgName && graph.hasNode(depPkgName)) { graph.setEdge(new (_graph().Edge)(node.id, depPkgName, dep.lifecycle)); } }); } const sortedSeeders = graph.toposort(true); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const sortedCapsules = sortedSeeders.map(node => this.getCapsule(node.attr.id)); return CapsuleList.fromArray(sortedCapsules); } static fromArray(capsules) { return new CapsuleList(...capsules); } /** * determines whether or not a capsule can theoretically use the dists saved in the last snap, rather than re-compile them. * practically, this optimization is used for components that have typescript as their compiler. */ static async capsuleUsePreviouslySavedDists(component) { const isModified = await component.isModified(); return component.buildStatus === 'succeed' && !isModified; } } exports.default = CapsuleList; //# sourceMappingURL=capsule-list.js.map