-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathsummary.js
More file actions
142 lines (121 loc) · 3.89 KB
/
summary.js
File metadata and controls
142 lines (121 loc) · 3.89 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
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
// Import Node.js Dependencies
import fs from "node:fs/promises";
import path from "node:path";
// Import Third-party Dependencies
import cliui from "@topcli/cliui";
import kleur from "kleur";
import * as i18n from "@nodesecure/i18n";
import { formatBytes } from "@nodesecure/utils";
// VARS
const { yellow, grey, white, green, cyan, red } = kleur;
function separatorLine() {
return grey("-".repeat(80));
}
export async function main(json = "nsecure-result.json") {
await i18n.getLocalLang();
const dataFilePath = path.join(process.cwd(), json);
const rawAnalysis = await fs.readFile(dataFilePath, { encoding: "utf-8" });
const { rootDependency, dependencies } = JSON.parse(rawAnalysis);
const ui = cliui({ width: 80 });
const title = `${white().bold(`${i18n.getTokenSync("ui.stats.title")}:`)} ${cyan().bold(rootDependency.name)}`;
ui.div(
{ text: title, width: 50 }
);
ui.div({ text: separatorLine() });
if (dependencies) {
const {
packagesCount,
packageWithIndirectDeps,
totalSize,
extensionMap,
licenceMap
} = extractAnalysisData(dependencies);
ui.div(
{ text: white().bold(`${i18n.getTokenSync("ui.stats.total_packages")}:`), width: 60 },
{ text: green().bold(`${packagesCount}`), width: 20, align: "right" }
);
ui.div(
{ text: white().bold(`${i18n.getTokenSync("ui.stats.total_size")}:`), width: 60 },
{ text: green().bold(`${formatBytes(totalSize)}`), width: 20, align: "right" }
);
ui.div(
{ text: white().bold(`${i18n.getTokenSync("ui.stats.indirect_deps")}:`), width: 60 },
{ text: green().bold(`${packageWithIndirectDeps}`), width: 20, align: "right" }
);
ui.div("");
ui.div(
{ text: white().bold(`${i18n.getTokenSync("ui.stats.extensions")}:`), width: 40 }
);
const extensionEntries = Object.entries(extensionMap);
ui.div(
{
text: `${extensionEntries.reduce(buildStringFromEntries, "")}`
}
);
ui.div("");
ui.div(
{ text: white().bold(`${i18n.getTokenSync("ui.stats.licenses")}:`), width: 40 }
);
const licenceEntries = Object.entries(licenceMap);
ui.div(
{
text: yellow().bold(`${licenceEntries.reduce(buildStringFromEntries, "")}`)
}
);
}
else {
ui.div(
{ text: red().bold("Error:"), width: 20 },
{ text: yellow().bold("No dependencies"), width: 30 }
);
}
ui.div({ text: separatorLine() });
console.log(ui.toString());
return void 0;
}
// eslint-disable-next-line max-params
function buildStringFromEntries(accumulator, [extension, count], index, sourceArray) {
// eslint-disable-next-line no-param-reassign
accumulator += `(${yellow(count)}) ${white().bold(extension)} `;
if (index !== sourceArray.length - 1) {
// eslint-disable-next-line no-param-reassign
accumulator += cyan("- ");
}
return accumulator;
}
function extractAnalysisData(dependencies) {
const analysisAggregator = {
packagesCount: 0,
totalSize: 0,
packageWithIndirectDeps: 0,
extensionMap: {},
licenceMap: {}
};
for (const { versions } of Object.values(dependencies)) {
for (const version of Object.values(versions)) {
extractVersionData(version, analysisAggregator);
}
analysisAggregator.packagesCount += 1;
}
return analysisAggregator;
}
function extractVersionData(version, analysisAggregator) {
for (const extension of version.composition.extensions) {
addOccurrences(analysisAggregator.extensionMap, extension);
}
for (const licence of version.uniqueLicenseIds) {
addOccurrences(analysisAggregator.licenceMap, licence);
}
if (version.flags && version.flags.includes("hasIndirectDependencies")) {
analysisAggregator.packageWithIndirectDeps++;
}
analysisAggregator.totalSize += version.size;
}
function addOccurrences(aggregator, key) {
if (aggregator[key]) {
aggregator[key]++;
}
else {
aggregator[key] = 1;
}
}