/
summarize-folders.mjs
46 lines (42 loc) · 1.28 KB
/
summarize-folders.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
import { findRuleByName } from "#graph-utl/rule-set.mjs";
function classifyViolation(pRule, pRuleSet) {
const lRule = findRuleByName(pRuleSet, pRule.name);
if (Object.hasOwn(lRule?.to ?? {}, "moreUnstable")) {
return "instability";
}
if (Object.hasOwn(lRule?.to ?? {}, "circular")) {
return "cycle";
}
return "folder";
}
function getViolations(pFolder, pRuleSet) {
return pFolder.dependencies
.filter((pDependency) => !pDependency.valid)
.flatMap((pDependency) =>
pDependency.rules.map((pRule) => {
const lViolationType = classifyViolation(pRule, pRuleSet);
return {
type: lViolationType,
from: pFolder.name,
to: pDependency.name,
rule: pRule,
...(lViolationType === "instability"
? {
metrics: {
from: { instability: pFolder.instability },
to: { instability: pDependency.instability },
},
}
: {}),
...(lViolationType === "cycle"
? {
cycle: pDependency.cycle,
}
: {}),
};
}),
);
}
export default function summarizeFolders(pFolders, pRuleSet) {
return pFolders.flatMap((pFolder) => getViolations(pFolder, pRuleSet));
}