File: /home/frenchy/www/french-american.org/current/node_modules/@snyk/dep-graph/dist/core/builder.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var graphlib = require("graphlib");
var dep_graph_1 = require("./dep-graph");
var DepGraphBuilder = /** @class */ (function () {
function DepGraphBuilder(pkgManager, rootPkg) {
this._pkgs = {};
this._pkgNodes = {};
var graph = new graphlib.Graph({
directed: true,
multigraph: false,
compound: false,
});
if (!rootPkg) {
rootPkg = {
name: '_root',
version: '0.0.0',
};
}
this._rootNodeId = 'root-node';
this._rootPkgId = DepGraphBuilder._getPkgId(rootPkg);
this._pkgs[this._rootPkgId] = rootPkg;
graph.setNode(this._rootNodeId, { pkgId: this._rootPkgId });
this._pkgNodes[this._rootPkgId] = new Set([this._rootNodeId]);
this._graph = graph;
this._pkgManager = pkgManager;
}
Object.defineProperty(DepGraphBuilder.prototype, "rootNodeId", {
get: function () {
return this._rootNodeId;
},
enumerable: true,
configurable: true
});
DepGraphBuilder._getPkgId = function (pkg) {
return pkg.name + "@" + (pkg.version || '');
};
// TODO: this can create disconnected nodes
DepGraphBuilder.prototype.addPkgNode = function (pkgInfo, nodeId, nodeInfo) {
if (nodeId === this._rootNodeId) {
throw new Error('DepGraphBuilder.addPkgNode() cant override root node');
}
var pkgId = DepGraphBuilder._getPkgId(pkgInfo);
this._pkgs[pkgId] = pkgInfo;
this._pkgNodes[pkgId] = this._pkgNodes[pkgId] || new Set();
this._pkgNodes[pkgId].add(nodeId);
this._graph.setNode(nodeId, { pkgId: pkgId, info: nodeInfo });
};
// TODO: this can create cycles
DepGraphBuilder.prototype.connectDep = function (parentNodeId, depNodeId) {
if (!this._graph.hasNode(parentNodeId)) {
throw new Error('parentNodeId does not exist');
}
if (!this._graph.hasNode(depNodeId)) {
throw new Error('depNodeId does not exist');
}
this._graph.setEdge(parentNodeId, depNodeId);
};
DepGraphBuilder.prototype.build = function () {
return new dep_graph_1.DepGraphImpl(this._graph, this._rootNodeId, this._pkgs, this._pkgNodes, this._pkgManager);
};
return DepGraphBuilder;
}());
exports.DepGraphBuilder = DepGraphBuilder;
//# sourceMappingURL=builder.js.map