1
|
/*
|
2
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
3
|
Author Tobias Koppers @sokra
|
4
|
*/
|
5
|
"use strict";
|
6
|
|
7
|
const Stats = require("./Stats");
|
8
|
|
9
|
const optionOrFallback = (optionValue, fallbackValue) =>
|
10
|
optionValue !== undefined ? optionValue : fallbackValue;
|
11
|
|
12
|
class MultiStats {
|
13
|
constructor(stats) {
|
14
|
this.stats = stats;
|
15
|
this.hash = stats.map(stat => stat.hash).join("");
|
16
|
}
|
17
|
|
18
|
hasErrors() {
|
19
|
return this.stats
|
20
|
.map(stat => stat.hasErrors())
|
21
|
.reduce((a, b) => a || b, false);
|
22
|
}
|
23
|
|
24
|
hasWarnings() {
|
25
|
return this.stats
|
26
|
.map(stat => stat.hasWarnings())
|
27
|
.reduce((a, b) => a || b, false);
|
28
|
}
|
29
|
|
30
|
toJson(options, forToString) {
|
31
|
if (typeof options === "boolean" || typeof options === "string") {
|
32
|
options = Stats.presetToOptions(options);
|
33
|
} else if (!options) {
|
34
|
options = {};
|
35
|
}
|
36
|
const jsons = this.stats.map((stat, idx) => {
|
37
|
const childOptions = Stats.getChildOptions(options, idx);
|
38
|
const obj = stat.toJson(childOptions, forToString);
|
39
|
obj.name = stat.compilation && stat.compilation.name;
|
40
|
return obj;
|
41
|
});
|
42
|
const showVersion =
|
43
|
options.version === undefined
|
44
|
? jsons.every(j => j.version)
|
45
|
: options.version !== false;
|
46
|
const showHash =
|
47
|
options.hash === undefined
|
48
|
? jsons.every(j => j.hash)
|
49
|
: options.hash !== false;
|
50
|
if (showVersion) {
|
51
|
for (const j of jsons) {
|
52
|
delete j.version;
|
53
|
}
|
54
|
}
|
55
|
const obj = {
|
56
|
errors: jsons.reduce((arr, j) => {
|
57
|
return arr.concat(
|
58
|
j.errors.map(msg => {
|
59
|
return `(${j.name}) ${msg}`;
|
60
|
})
|
61
|
);
|
62
|
}, []),
|
63
|
warnings: jsons.reduce((arr, j) => {
|
64
|
return arr.concat(
|
65
|
j.warnings.map(msg => {
|
66
|
return `(${j.name}) ${msg}`;
|
67
|
})
|
68
|
);
|
69
|
}, [])
|
70
|
};
|
71
|
if (showVersion) obj.version = require("../package.json").version;
|
72
|
if (showHash) obj.hash = this.hash;
|
73
|
if (options.children !== false) obj.children = jsons;
|
74
|
return obj;
|
75
|
}
|
76
|
|
77
|
toString(options) {
|
78
|
if (typeof options === "boolean" || typeof options === "string") {
|
79
|
options = Stats.presetToOptions(options);
|
80
|
} else if (!options) {
|
81
|
options = {};
|
82
|
}
|
83
|
|
84
|
const useColors = optionOrFallback(options.colors, false);
|
85
|
|
86
|
const obj = this.toJson(options, true);
|
87
|
|
88
|
return Stats.jsonToString(obj, useColors);
|
89
|
}
|
90
|
}
|
91
|
|
92
|
module.exports = MultiStats;
|