-
Notifications
You must be signed in to change notification settings - Fork 248
/
text.js
92 lines (83 loc) · 2.47 KB
/
text.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
const figures = require("figures");
const { underline: highlight } = require("chalk");
const DEFAULT_OPTIONS = {
highlightFocused: false,
};
/**
*
* @param {import("../../types/cruise-result").IModule} pModule
* @param {Set<string>} pModulesInFocus
* @param {boolean} pHighlightFocused
* @returns {any}
*/
function toFlatModuleDependencies(pModule, pModulesInFocus, pHighlightFocused) {
return pModule.dependencies.map((pDependency) => ({
from: {
name: pModule.source,
highlight: pHighlightFocused && Boolean(pModule.matchesFocus),
},
to: {
name: pDependency.resolved,
highlight: pHighlightFocused && pModulesInFocus.has(pDependency.resolved),
},
}));
}
function toFlatDependencies(pModules, pModulesInFocus, pHighlightFocused) {
return pModules.reduce(
(pAll, pModule) =>
pAll.concat(
toFlatModuleDependencies(pModule, pModulesInFocus, pHighlightFocused)
),
[]
);
}
function stringifyModule(pModule) {
return pModule.highlight ? highlight(pModule.name) : pModule.name;
}
function stringify(pFlatDependency) {
return `${stringifyModule(pFlatDependency.from)} ${
figures.arrowRight
} ${stringifyModule(pFlatDependency.to)}`;
}
/**
*
* @param {import("../../types/cruise-result").IModule[]} pModules
* @returns {Set<string>}
*/
function getModulesInFocus(pModules) {
return new Set(
pModules
.filter((pModule) => pModule.matchesFocus || pModule.matchesReaches)
.map((pModule) => pModule.source)
);
}
/**
*
* @param {import("../../types/cruise-result").ICruiseResult} pResults
* @param {import("../../types/reporter-options").ITextReporterOptions} pOptions
* @returns {string}
*/
function report(pResults, pOptions) {
const lOptions = { ...DEFAULT_OPTIONS, ...pOptions };
return toFlatDependencies(
pResults.modules,
getModulesInFocus(pResults.modules),
lOptions.highlightFocused === true
).reduce(
(pAll, pDependency) => pAll.concat(stringify(pDependency)).concat("\n"),
""
);
}
/**
* Returns the results of a cruise in a text only format
* - for each dependency the from and the two, separated by an arrow.
* @param {import("../../types/cruise-result").ICruiseResult} pResults
* @param {import("../../types/reporter-options").ITextReporterOptions} pOptions
* @returns {import("../../types/dependency-cruiser").IReporterOutput}
*/
module.exports = function text(pResults, pOptions) {
return {
output: report(pResults, pOptions || {}),
exitCode: 0,
};
};