-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmd.mjs
53 lines (49 loc) · 1.53 KB
/
mmd.mjs
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
import { readFileSync } from "fs";
function skbomMmd(data) {
// note to self: mermaid renderer dosn't like leading whitespace
return `---
title: ${data.name}:${data.version}
---
flowchart LR
subgraph masters["Masters"]
${data.controlPlane.nodes.reduce((acc, n) => acc + printNode(n) + "\n", "")}
end
subgraph controlplane["Control Plane"]
${data.controlPlane.components.reduce((acc, r) => acc + printResource(r) + "\n", "")}
end
subgraph nodes["Workers"]
${data.nodes.reduce((acc, n) => acc + printNode(n) + "\n", "")}
end
subgraph components["Components"]
${data.components.reduce((acc, r) => acc+ printResource(r) + "\n", "")}
end
classDef node fill:Bisque
classDef section stroke:MidnightBlue,fill:none
classDef os fill:LightSteelBlue
class masters,controlplane,nodes,components section
class nodes nodes
`;
}
function printNode(n) {
return `subgraph ${n.id}[${n.name}]
${n.resources.reduce((acc, r) => acc + " " + printResource(r) + "\n", "")}
${n.id}-os{{"${n.os.name}:${n.os.version}"}}:::os
end`;
}
function printResource(r) {
let res = `${r.id}["\`**${r.name}**\n${r.version}\`"]`;
res += r.resources.reduce((acc, v) => acc+`\n${v.id}["\`**${v.name}**\n${v.version}\`"]\n${r.id} --> ${v.id}`, "");
return res;
}
(() => {
const filePath = process.argv[2];
let data;
try {
const text = readFileSync(filePath, 'utf8');
data = JSON.parse(text);
} catch (err) {
console.error(err);
}
let mmd = skbomMmd(data);
console.log(mmd);
})();