forked from CycloneDX/cdxgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.js
102 lines (98 loc) · 2.8 KB
/
display.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
import { table } from "table";
// https://github.com/yangshun/tree-node-cli/blob/master/src/index.js
const SYMBOLS_ANSI = {
BRANCH: "├── ",
EMPTY: "",
INDENT: " ",
LAST_BRANCH: "└── ",
VERTICAL: "│ "
};
export const printTable = (bomJson) => {
const data = [["Group", "Name", "Version", "Scope"]];
for (const comp of bomJson.components) {
data.push([comp.group || "", comp.name, comp.version, comp.scope || ""]);
}
const config = {
header: {
alignment: "center",
content: "Software Bill-of-Materials\nGenerated by @cyclonedx/cdxgen"
}
};
console.log(table(data, config));
console.log(
"BOM includes",
bomJson.components.length,
"components and",
bomJson.dependencies.length,
"dependencies"
);
};
export const printDependencyTree = (bomJson) => {
const dependencies = bomJson.dependencies || [];
if (!dependencies.length) {
return;
}
const depMap = {};
for (const d of dependencies) {
if (d.dependsOn && d.dependsOn.length) {
depMap[d.ref] = d.dependsOn;
}
}
const shownList = [];
const treeGraphics = [];
recursePrint(depMap, dependencies, 0, shownList, treeGraphics);
if (treeGraphics && treeGraphics.length) {
// Patch up the last line
treeGraphics[treeGraphics.length - 1] = treeGraphics[
treeGraphics.length - 1
].replace(SYMBOLS_ANSI.BRANCH, SYMBOLS_ANSI.LAST_BRANCH);
if (treeGraphics[treeGraphics.length - 1].includes(SYMBOLS_ANSI.VERTICAL)) {
treeGraphics[treeGraphics.length - 1] = treeGraphics[
treeGraphics.length - 1
]
.replace(SYMBOLS_ANSI.VERTICAL, SYMBOLS_ANSI.LAST_BRANCH)
.replace(
SYMBOLS_ANSI.INDENT + SYMBOLS_ANSI.LAST_BRANCH,
SYMBOLS_ANSI.LAST_BRANCH
);
}
}
const config = {
header: {
alignment: "center",
content: "Dependency Tree\nGenerated with \u2665 by cdxgen"
}
};
console.log(table([[treeGraphics.join("\n")]], config));
};
const levelPrefix = (level) => {
if (level === 0) {
return SYMBOLS_ANSI.EMPTY;
}
let prefix = `${SYMBOLS_ANSI.BRANCH}`;
for (let i = 0; i < level - 1; i++) {
prefix = `${SYMBOLS_ANSI.VERTICAL}${SYMBOLS_ANSI.INDENT}${prefix}`;
}
return prefix;
};
const recursePrint = (depMap, subtree, level, shownList, treeGraphics) => {
const listToUse = Array.isArray(subtree) ? subtree : [subtree];
for (const l of listToUse) {
const refStr = l.ref || l;
if (!shownList.includes(refStr.toLowerCase())) {
shownList.push(refStr.toLowerCase());
treeGraphics.push(`${levelPrefix(level)}${refStr}`);
if (l && depMap[refStr]) {
if (level < 3) {
recursePrint(
depMap,
depMap[refStr],
level + 1,
shownList,
treeGraphics
);
}
}
}
}
};