1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.DepGraphBuilder = void 0;
|
4 | const graphlib = require("../graphlib");
|
5 | const dep_graph_1 = require("./dep-graph");
|
6 | class DepGraphBuilder {
|
7 | constructor(pkgManager, rootPkg) {
|
8 | this._pkgs = {};
|
9 | this._pkgNodes = {};
|
10 | const graph = new graphlib.Graph({
|
11 | directed: true,
|
12 | multigraph: false,
|
13 | compound: false,
|
14 | });
|
15 | if (!rootPkg) {
|
16 | rootPkg = {
|
17 | name: '_root',
|
18 | version: '0.0.0',
|
19 | };
|
20 | }
|
21 | this._rootNodeId = 'root-node';
|
22 | this._rootPkgId = DepGraphBuilder._getPkgId(rootPkg);
|
23 | this._pkgs[this._rootPkgId] = rootPkg;
|
24 | graph.setNode(this._rootNodeId, { pkgId: this._rootPkgId });
|
25 | this._pkgNodes[this._rootPkgId] = new Set([this._rootNodeId]);
|
26 | this._graph = graph;
|
27 | this._pkgManager = pkgManager;
|
28 | }
|
29 | get rootNodeId() {
|
30 | return this._rootNodeId;
|
31 | }
|
32 | static _getPkgId(pkg) {
|
33 | return `${pkg.name}@${pkg.version || ''}`;
|
34 | }
|
35 | getPkgs() {
|
36 | return Object.values(this._pkgs);
|
37 | }
|
38 |
|
39 | addPkgNode(pkgInfo, nodeId, nodeInfo) {
|
40 | if (nodeId === this._rootNodeId) {
|
41 | throw new Error('DepGraphBuilder.addPkgNode() cant override root node');
|
42 | }
|
43 | const pkgId = DepGraphBuilder._getPkgId(pkgInfo);
|
44 | this._pkgs[pkgId] = pkgInfo;
|
45 | this._pkgNodes[pkgId] = this._pkgNodes[pkgId] || new Set();
|
46 | this._pkgNodes[pkgId].add(nodeId);
|
47 | this._graph.setNode(nodeId, { pkgId, info: nodeInfo });
|
48 | return this;
|
49 | }
|
50 |
|
51 | connectDep(parentNodeId, depNodeId) {
|
52 | if (!this._graph.hasNode(parentNodeId)) {
|
53 | throw new Error('parentNodeId does not exist');
|
54 | }
|
55 | if (!this._graph.hasNode(depNodeId)) {
|
56 | throw new Error('depNodeId does not exist');
|
57 | }
|
58 | this._graph.setEdge(parentNodeId, depNodeId);
|
59 | return this;
|
60 | }
|
61 | build() {
|
62 | return new dep_graph_1.DepGraphImpl(this._graph, this._rootNodeId, this._pkgs, this._pkgNodes, this._pkgManager);
|
63 | }
|
64 | }
|
65 | exports.DepGraphBuilder = DepGraphBuilder;
|
66 |
|
\ | No newline at end of file |