-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdot-graph.js
More file actions
87 lines (81 loc) · 2.19 KB
/
Copy pathdot-graph.js
File metadata and controls
87 lines (81 loc) · 2.19 KB
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
import * as fs from "fs";
const defaultConfig = {
baseDir: null,
excludeRegExp: false,
fileExtensions: ["js"],
includeNpm: false,
requireConfig: null,
webpackConfig: null,
tsConfig: null,
rankdir: "LR",
layout: "dot",
fontName: "Arial",
fontSize: "14px",
backgroundColor: "#111111",
nodeColor: "#c6c5fe",
nodeShape: "box",
nodeStyle: "rounded",
noDependencyColor: "#cfffac",
cyclicNodeColor: "#ff6c60",
edgeColor: "#757575",
graphVizOptions: false,
graphVizPath: false,
dependencyFilter: false,
};
export function generateDotGraph(dependencies, includeModules, prefix) {
const components = dependencies.filter((d) => !d.name.includes("module"));
const modules = dependencies.filter((d) => d.name.includes("module"));
const config = defaultConfig;
const dotFile = `
digraph G {
overlap=false
pad=0.3
rankdir=${config.rankdir}
layout=${config.layout}
bgcolor="${config.backgroundColor}"
edge [color="${config.edgeColor}"]
node [color="${config.nodeColor}", shape=${config.nodeShape}, style=${
config.nodeStyle
}, height=0, fontcolor="${config.nodeColor}"]
${
!includeModules
? ""
: modules
.map((module) => {
return `subgraph cluster_${module.name.replace(/[.-]/g, "")} {
label="${module.name}";
${
module.children.length
? module.children.map((c) => `"${c}"`).join(",") + ";"
: ""
}
color="${config.nodeColor}"
fontcolor="${config.nodeColor}"
}`;
})
.join("\n")
}
${components
.filter((component) => component.children.length)
.map((component) => {
return component.children
.map(
(child) =>
` "${component.name.replace(prefix, "")}" -> "${child.replace(
prefix,
""
)}";`
)
.join("\n");
})
.join("\n")}
}`;
return dotFile;
}
export function saveToDotFile(filename, components, includeModules, prefix) {
fs.writeFileSync(
filename,
generateDotGraph(components, includeModules, prefix)
);
console.log(`✔ Dot file written to ${filename}`);
}