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