warn-mixin.js 697 B

123456789101112131415161718192021
  1. 'use strict'
  2. module.exports = Base => class extends Base {
  3. warn (code, message, data = {}) {
  4. if (this.file)
  5. data.file = this.file
  6. if (this.cwd)
  7. data.cwd = this.cwd
  8. data.code = message instanceof Error && message.code || code
  9. data.tarCode = code
  10. if (!this.strict && data.recoverable !== false) {
  11. if (message instanceof Error) {
  12. data = Object.assign(message, data)
  13. message = message.message
  14. }
  15. this.emit('warn', data.tarCode, message, data)
  16. } else if (message instanceof Error)
  17. this.emit('error', Object.assign(message, data))
  18. else
  19. this.emit('error', Object.assign(new Error(`${code}: ${message}`), data))
  20. }
  21. }