-
Notifications
You must be signed in to change notification settings - Fork 248
/
mermaid.js
172 lines (148 loc) · 4.86 KB
/
mermaid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* eslint-disable security/detect-object-injection */
const ACORN_DUMMY_VALUE = "✖";
const REPORT_DEFAULTS = {
minify: true,
};
const renderNode = (pNode, pText) =>
`${pNode}["${pText.length > 0 ? pText : " "}"]`;
const renderEdge = (pFrom, pTo) => `${pFrom.node}-->${pTo.node}`;
const renderEdges = (pEdges) =>
pEdges.map((pEdge) => renderEdge(pEdge.from, pEdge.to)).join("\n");
/**
* @param {import('../..').ICruiseResult} pCruiseResult
* @param {Map<string, string>} pNamesHashMap
*/
function convertEdgeSources(pCruiseResult, pNamesHashMap) {
return pCruiseResult.modules.flatMap((pModule) => {
const lFromNode = pNamesHashMap.get(pModule.source);
const lFrom = {
node: lFromNode,
text: pModule.source.split("/").slice(-1)[0],
};
return pModule.dependencies.map((pDependency) => {
const lToNode = pNamesHashMap.get(pDependency.resolved);
return {
from: lFrom,
to: {
node: lToNode,
text: pDependency.resolved.split("/").slice(-1)[0],
},
};
});
});
}
const indent = (pDepth = 0, pMinify) => (pMinify ? "" : " ".repeat(pDepth));
const renderSubgraph = (pNode, pText, pChildren, pIndent) =>
`${pIndent}subgraph ${renderNode(pNode, pText)}
${pChildren}
${pIndent}end`;
function renderSubgraphs(pSource, pOptions, pDepth = 0) {
return Object.keys(pSource)
.map((pName) => {
const lIndent = indent(pDepth, pOptions.minify);
const source = pSource[pName];
const children = renderSubgraphs(source.children, pOptions, pDepth + 1);
if (children === "")
return `${lIndent}${renderNode(source.node, source.text)}`;
return renderSubgraph(source.node, source.text, children, lIndent);
})
.join("\n");
}
/**
* @param {import('../..').ICruiseResult} pCruiseResult
* @param {Map<string, string>} pNamesHashMap
*/
function convertSubgraphSources(pCruiseResult, pNamesHashMap) {
let lTree = {};
pCruiseResult.modules.forEach((pModule) => {
const lPaths = pModule.source.split("/");
lPaths.reduce((pChildren, pCurrentPath, pIndex) => {
if (!pChildren[pCurrentPath]) {
const node = lPaths.slice(0, pIndex + 1).join("/");
pChildren[pCurrentPath] = {
node: pNamesHashMap.get(node),
text: pCurrentPath,
children: {},
};
}
return pChildren[pCurrentPath].children;
}, lTree);
});
return lTree;
}
/**
* @param {import("../../types/cruise-result").IModule[]} pModules
* @param {Map<string, string>} pNamesHashMap
*/
function focusHighlights(pModules, pNamesHashMap) {
const lHighLightStyle = "fill:lime,color:black";
return pModules
.filter(
(pModule) =>
pModule.matchesFocus ||
pModule.matchesReaches ||
pModule.matchesHighlight
)
.reduce((pAll, pModule) => {
const lSource = pNamesHashMap.get(pModule.source);
return `${pAll}\nstyle ${lSource} ${lHighLightStyle}`;
}, "");
}
const hashToReadableNodeName = (pNode) =>
pNode
.replace(ACORN_DUMMY_VALUE, "__unknown__")
.replace(/^\.$|^\.\//g, "__currentPath__")
.replace(/^\.{2}$|^\.{2}\//g, "__prevPath__")
.replace(/[[\]/.@~-]/g, "_");
/**
* @param {import("../../types/cruise-result").IModule[]} pModules
* @param {Boolean} pMinify
* @return {Map<string, string>}
*/
function hashModuleNames(pModules, pMinify) {
const lBase = 36;
const lNamesHashMap = new Map();
let lCount = 0;
pModules.forEach((pModule) => {
const lPaths = pModule.source.split("/");
for (let lIndex = 0; lIndex < lPaths.length; lIndex += 1) {
const lName = lPaths.slice(0, lIndex + 1).join("/");
if (!lNamesHashMap.has(lName)) {
if (pMinify) {
lNamesHashMap.set(lName, lCount.toString(lBase));
lCount += 1;
} else {
lNamesHashMap.set(lName, hashToReadableNodeName(lName));
}
}
}
});
return lNamesHashMap;
}
/**
* @param {import('../..').ICruiseResult} pCruiseResult
* @param {import("../../types/reporter-options").IMermaidReporterOptions} pOptions
*/
function renderMermaidSource(pCruiseResult, pOptions) {
const lOptions = { ...REPORT_DEFAULTS, ...(pOptions || {}) };
const lNamesHashMap = hashModuleNames(pCruiseResult.modules, lOptions.minify);
const lSubgraphs = convertSubgraphSources(pCruiseResult, lNamesHashMap);
const lEdges = convertEdgeSources(pCruiseResult, lNamesHashMap);
return `flowchart LR
${renderSubgraphs(lSubgraphs, lOptions)}
${renderEdges(lEdges)}
${focusHighlights(pCruiseResult.modules, lNamesHashMap)}`;
}
/**
* mermaid reporter
*
* @param {import('../..').ICruiseResult} pCruiseResult
* @param {import("../../types/reporter-options").IMermaidReporterOptions} pOptions
* @return {import('../..').IReporterOutput}
*/
module.exports = function mermaid(pCruiseResult, pOptions) {
return {
output: renderMermaidSource(pCruiseResult, pOptions),
exitCode: 0,
};
};