MultiStats.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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) =>
  8. optionValue !== undefined ? optionValue : fallbackValue;
  9. class MultiStats {
  10. constructor(stats) {
  11. this.stats = stats;
  12. this.hash = stats.map(stat => stat.hash).join("");
  13. }
  14. hasErrors() {
  15. return this.stats
  16. .map(stat => stat.hasErrors())
  17. .reduce((a, b) => a || b, false);
  18. }
  19. hasWarnings() {
  20. return this.stats
  21. .map(stat => stat.hasWarnings())
  22. .reduce((a, b) => a || b, false);
  23. }
  24. toJson(options, forToString) {
  25. if (typeof options === "boolean" || typeof options === "string") {
  26. options = Stats.presetToOptions(options);
  27. } else if (!options) {
  28. options = {};
  29. }
  30. const jsons = this.stats.map((stat, idx) => {
  31. const childOptions = Stats.getChildOptions(options, idx);
  32. const obj = stat.toJson(childOptions, forToString);
  33. obj.name = stat.compilation && stat.compilation.name;
  34. return obj;
  35. });
  36. const showVersion =
  37. options.version === undefined
  38. ? jsons.every(j => j.version)
  39. : options.version !== false;
  40. const showHash =
  41. options.hash === undefined
  42. ? jsons.every(j => j.hash)
  43. : options.hash !== false;
  44. if (showVersion) {
  45. for (const j of jsons) {
  46. delete j.version;
  47. }
  48. }
  49. const obj = {
  50. errors: jsons.reduce((arr, j) => {
  51. return arr.concat(
  52. j.errors.map(msg => {
  53. return `(${j.name}) ${msg}`;
  54. })
  55. );
  56. }, []),
  57. warnings: jsons.reduce((arr, j) => {
  58. return arr.concat(
  59. j.warnings.map(msg => {
  60. return `(${j.name}) ${msg}`;
  61. })
  62. );
  63. }, [])
  64. };
  65. if (showVersion) obj.version = require("../package.json").version;
  66. if (showHash) obj.hash = this.hash;
  67. if (options.children !== false) obj.children = jsons;
  68. return obj;
  69. }
  70. toString(options) {
  71. if (typeof options === "boolean" || typeof options === "string") {
  72. options = Stats.presetToOptions(options);
  73. } else if (!options) {
  74. options = {};
  75. }
  76. const useColors = optionOrFallback(options.colors, false);
  77. const obj = this.toJson(options, true);
  78. return Stats.jsonToString(obj, useColors);
  79. }
  80. }
  81. module.exports = MultiStats;