60 lines
7.3 KiB
JavaScript
60 lines
7.3 KiB
JavaScript
|
"use strict";
|
||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||
|
};
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
const graphlib_1 = require("graphlib");
|
||
|
const node_modules_folder_name_1 = __importDefault(require("../../utils/node-modules-folder-name"));
|
||
|
class ModulesGraph {
|
||
|
constructor() {
|
||
|
this.graph = new graphlib_1.Graph();
|
||
|
}
|
||
|
_updateChildren(node, cache) {
|
||
|
const cached = cache[node];
|
||
|
if (!cached)
|
||
|
return;
|
||
|
const outEdges = this.graph.outEdges(node) || [];
|
||
|
outEdges.forEach(edge => this.graph.removeEdge(edge.v, edge.w));
|
||
|
const children = cached && cached.children.map(child => child.id);
|
||
|
if (!children)
|
||
|
return;
|
||
|
children.filter(child => !child.includes(node_modules_folder_name_1.default))
|
||
|
.forEach(child => {
|
||
|
this.addNode(child, cache);
|
||
|
this.graph.setEdge(node, child);
|
||
|
});
|
||
|
}
|
||
|
addNode(node, cache) {
|
||
|
if (this.graph.hasNode(node))
|
||
|
return;
|
||
|
const cached = cache[node];
|
||
|
if (cached)
|
||
|
this.graph.setNode(node);
|
||
|
const parent = cached && cached.parent;
|
||
|
if (parent && !parent.id.includes(node_modules_folder_name_1.default)) {
|
||
|
this.addNode(parent.id, cache);
|
||
|
this.graph.setEdge(parent.id, node);
|
||
|
}
|
||
|
this._updateChildren(node, cache);
|
||
|
}
|
||
|
build(cache, nodes) {
|
||
|
nodes.forEach(node => this.addNode(node, cache, true));
|
||
|
}
|
||
|
rebuildNode(cache, node) {
|
||
|
this._updateChildren(node, cache);
|
||
|
}
|
||
|
clearParentsCache(cache, node) {
|
||
|
if (!cache[node])
|
||
|
return;
|
||
|
delete cache[node];
|
||
|
const parentEdges = this.graph.inEdges(node);
|
||
|
if (!parentEdges || !parentEdges.length)
|
||
|
return;
|
||
|
parentEdges
|
||
|
.map(edge => edge.v)
|
||
|
.forEach(parent => this.clearParentsCache(cache, parent));
|
||
|
}
|
||
|
}
|
||
|
exports.default = ModulesGraph;
|
||
|
module.exports = exports.default;
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibW9kdWxlcy1ncmFwaC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy9saXZlL2ZpbGUtd2F0Y2hlci9tb2R1bGVzLWdyYXBoLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7O0FBQUEsdUNBQWlDO0FBQ2pDLG9HQUFnRTtBQUVoRSxNQUFxQixZQUFZO0lBQzdCO1FBQ0ksSUFBSSxDQUFDLEtBQUssR0FBRyxJQUFJLGdCQUFLLEVBQUUsQ0FBQztJQUM3QixDQUFDO0lBRUQsZUFBZSxDQUFFLElBQUksRUFBRSxLQUFLO1FBQ3hCLE1BQU0sTUFBTSxHQUFHLEtBQUssQ0FBQyxJQUFJLENBQUMsQ0FBQztRQUUzQixJQUFJLENBQUMsTUFBTTtZQUNQLE9BQU87UUFFWCxNQUFNLFFBQVEsR0FBRyxJQUFJLENBQUMsS0FBSyxDQUFDLFFBQVEsQ0FBQyxJQUFJLENBQUMsSUFBSSxFQUFFLENBQUM7UUFFakQsUUFBUSxDQUFDLE9BQU8sQ0FBQyxJQUFJLENBQUMsRUFBRSxDQUFDLElBQUksQ0FBQyxLQUFLLENBQUMsVUFBVSxDQUFDLElBQUksQ0FBQyxDQUFDLEVBQUUsSUFBSSxDQUFDLENBQUMsQ0FBQyxDQUFDLENBQUM7UUFFaEUsTUFBTSxRQUFRLEdBQUcsTUFBTSxJQUFJLE1BQU0sQ0FBQyxRQUFRLENBQUMsR0FBRyxDQUFDLEtBQUssQ0FBQyxFQUFFLENBQUMsS0FBSyxDQUFDLEVBQUUsQ0FBQyxDQUFDO1FBRWxFLElBQUksQ0FBQyxRQUFRO1lBQ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