1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.findFilesInCircularPath = exports.checkCircularPath = exports.pathExists = exports.getPath = void 0;
|
4 | const reach = {
|
5 | graph: null,
|
6 | matrix: null,
|
7 | adjList: null,
|
8 | };
|
9 | function buildMatrix(graph) {
|
10 | const nodes = Object.keys(graph.nodes);
|
11 | const nodesLength = nodes.length;
|
12 | const adjList = {};
|
13 | const matrix = {};
|
14 |
|
15 | for (let i = 0; i < nodesLength; i++) {
|
16 | const v = nodes[i];
|
17 | adjList[v] = [];
|
18 |
|
19 | matrix[v] = {};
|
20 | }
|
21 | const projectsWithDependencies = Object.keys(graph.dependencies);
|
22 | for (let i = 0; i < projectsWithDependencies.length; i++) {
|
23 | const dependencies = graph.dependencies[projectsWithDependencies[i]];
|
24 | for (let j = 0; j < dependencies.length; j++) {
|
25 | const dependency = dependencies[j];
|
26 | if (graph.nodes[dependency.target]) {
|
27 | adjList[dependency.source].push(dependency.target);
|
28 | }
|
29 | }
|
30 | }
|
31 | const traverse = (s, v) => {
|
32 | matrix[s][v] = true;
|
33 | const adjListLength = adjList[v].length;
|
34 | for (let i = 0; i < adjListLength; i++) {
|
35 | const adj = adjList[v][i];
|
36 | if (!matrix[s][adj]) {
|
37 | traverse(s, adj);
|
38 | }
|
39 | }
|
40 | };
|
41 | for (let i = 0; i < nodesLength; i++) {
|
42 | const v = nodes[i];
|
43 | traverse(v, v);
|
44 | }
|
45 | return {
|
46 | matrix,
|
47 | adjList,
|
48 | };
|
49 | }
|
50 | function getPath(graph, sourceProjectName, targetProjectName) {
|
51 | if (sourceProjectName === targetProjectName)
|
52 | return [];
|
53 | if (reach.graph !== graph) {
|
54 | const { matrix, adjList } = buildMatrix(graph);
|
55 | reach.graph = graph;
|
56 | reach.matrix = matrix;
|
57 | reach.adjList = adjList;
|
58 | }
|
59 | const adjList = reach.adjList;
|
60 | let path = [];
|
61 | const queue = [[sourceProjectName, path]];
|
62 | const visited = [sourceProjectName];
|
63 | while (queue.length > 0) {
|
64 | const [current, p] = queue.pop();
|
65 | path = [...p, current];
|
66 | if (current === targetProjectName)
|
67 | break;
|
68 | if (!adjList[current])
|
69 | break;
|
70 | adjList[current]
|
71 | .filter((adj) => visited.indexOf(adj) === -1)
|
72 | .filter((adj) => reach.matrix[adj][targetProjectName])
|
73 | .forEach((adj) => {
|
74 | visited.push(adj);
|
75 | queue.push([adj, [...path]]);
|
76 | });
|
77 | }
|
78 | if (path.length > 1) {
|
79 | return path.map((n) => graph.nodes[n]);
|
80 | }
|
81 | else {
|
82 | return [];
|
83 | }
|
84 | }
|
85 | exports.getPath = getPath;
|
86 | function pathExists(graph, sourceProjectName, targetProjectName) {
|
87 | if (sourceProjectName === targetProjectName)
|
88 | return true;
|
89 | if (reach.graph !== graph) {
|
90 | const { matrix, adjList } = buildMatrix(graph);
|
91 | reach.graph = graph;
|
92 | reach.matrix = matrix;
|
93 | reach.adjList = adjList;
|
94 | }
|
95 | return !!reach.matrix[sourceProjectName][targetProjectName];
|
96 | }
|
97 | exports.pathExists = pathExists;
|
98 | function checkCircularPath(graph, sourceProject, targetProject) {
|
99 | if (!graph.nodes[targetProject.name])
|
100 | return [];
|
101 | return getPath(graph, targetProject.name, sourceProject.name);
|
102 | }
|
103 | exports.checkCircularPath = checkCircularPath;
|
104 | function findFilesInCircularPath(circularPath) {
|
105 | const filePathChain = [];
|
106 | for (let i = 0; i < circularPath.length - 1; i++) {
|
107 | const next = circularPath[i + 1].name;
|
108 | const files = circularPath[i].data.files;
|
109 | filePathChain.push(Object.keys(files)
|
110 | .filter((key) => files[key].deps && files[key].deps.indexOf(next) !== -1)
|
111 | .map((key) => files[key].file));
|
112 | }
|
113 | return filePathChain;
|
114 | }
|
115 | exports.findFilesInCircularPath = findFilesInCircularPath;
|
116 |
|
\ | No newline at end of file |