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