'use strict'

module.exports = function handleErrors(err, stats, done) {
  if (err) {
    console.error(err.stack || err)
    if (err.details) {
      console.error(err.details)
    }
    done(err)
    return
  }

  const info = stats.toJson()

  if (stats.hasErrors()) {
    console.error(info.errors)
    done(info.errors)
  }

  if (stats.hasWarnings()) {
    console.warn(info.warnings)
    done(info.warnings)
  }
}

'use strict'

function concatChildProps(parent, property) {
  let children = parent.children || []
  let childProps = children.map(c => concatChildProps(c, property))
  return Array.prototype.concat.apply(parent[property] || [], childProps)
}

module.exports = function handleErrors(err, stats, done) {
  if (err) {
    console.error(err.stack || err)
    if (err.details) {
      console.error(err.details)
    }
    done(err)
    return
  }

  const info = stats.toJson()
  const allErrors = concatChildProps(info, 'errors')
  const allWarnings = concatChildProps(info, 'warnings')

  if (allErrors.length) {
    console.error(allErrors)
    done(new Error(allErrors[0]))
    return
  }

  if (allWarnings.length) {
    console.warn(allWarnings)
    // Don't fail the test suite because of warnings, at least until we can get a consistent lack of warnings from other packages
    // (at time of writing, webpack-chunk-hash still emits warnings in webpack 4)
    return
  }
}

Include child-compilation errors when handling test errors
Include child-compilation errors when handling test errors

when `stats.hasErrors()` returns true, we might have errors in `info.errors`, or
we might have them in, say, `info.children[0].errors`
