forked from sverweij/dependency-cruiser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats-reporter-plugin.js
82 lines (77 loc) · 2.57 KB
/
stats-reporter-plugin.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
const MEDIAN = 0.5;
const P75 = 0.75;
const DEFAULT_JSON_INDENT = 2;
function doMagic(pCruiseResult) {
let lReturnValue = {};
if (pCruiseResult.modules.some((pModule) => pModule.dependents)) {
const lDependentCounts = pCruiseResult.modules
.map((pModule) => pModule.dependents.length)
.sort();
lReturnValue = {
minDependentsPerModule: lDependentCounts[0] || 0,
maxDependentsPerModule:
lDependentCounts[Math.max(lDependentCounts.length - 1, 0)] || 0,
meanDependentsPerModule:
lDependentCounts.reduce((pAll, pCurrent) => pAll + pCurrent, 0) /
pCruiseResult.summary.totalCruised,
medianDependentsPerModule:
lDependentCounts[
Math.max(0, Math.floor(lDependentCounts.length * MEDIAN))
],
p75DependentsPerModule:
lDependentCounts[
Math.max(0, Math.floor(lDependentCounts.length * P75))
],
};
}
return lReturnValue;
}
/**
* returns an object with some stats from the ICruiseResult pCruiseResult it
* got passed
*
* @param {import('../../types/dependency-cruiser').ICruiseResult} pCruiseResult - a result from a cruise.
* @return {string} an object with some stats
*/
function samplePluginReporter(pCruiseResult) {
const lDependencyCounts = pCruiseResult.modules
.map((pModule) => pModule.dependencies.length)
.sort();
return {
moduleCount: pCruiseResult.summary.totalCruised,
dependencyCount: pCruiseResult.summary.totalDependenciesCruised,
minDependenciesPerModule: lDependencyCounts[0] || 0,
maxDependenciesPerModule:
lDependencyCounts[Math.max(lDependencyCounts.length - 1, 0)] || 0,
meanDependenciesPerModule:
pCruiseResult.summary.totalDependenciesCruised /
pCruiseResult.summary.totalCruised,
medianDependenciesPerModule:
lDependencyCounts[
Math.max(0, Math.floor(lDependencyCounts.length * MEDIAN))
],
p75DependenciesPerModule:
lDependencyCounts[
Math.max(0, Math.floor(lDependencyCounts.length * P75))
],
...doMagic(pCruiseResult),
};
}
/**
* Sample plugin
*
* @param {import('../../types/dependency-cruiser').ICruiseResult} pCruiseResult -
* the output of a dependency-cruise adhering to dependency-cruiser's
* cruise result schema
* @return {import('../../types/dependency-cruiser').IReporterOutput} -
* output: some stats on modules and dependencies in json format
* exitCode: 0
*/
module.exports = (pCruiseResult) => ({
output: JSON.stringify(
samplePluginReporter(pCruiseResult),
null,
DEFAULT_JSON_INDENT
),
exitCode: 0,
});