events.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. 'use strict';
  2. var log = require('gulplog');
  3. var prettyTime = require('pretty-hrtime');
  4. var ansi = require('../../../shared/ansi');
  5. var formatError = require('../format-error');
  6. // Wire up logging events
  7. function logEvents(gulpInst) {
  8. var loggedErrors = [];
  9. gulpInst.on('start', function(evt) {
  10. /* istanbul ignore next */
  11. // TODO: batch these
  12. // so when 5 tasks start at once it only logs one time with all 5
  13. var level = evt.branch ? 'debug' : 'info';
  14. log[level]('Starting', '\'' + ansi.cyan(evt.name) + '\'...');
  15. });
  16. gulpInst.on('stop', function(evt) {
  17. var time = prettyTime(evt.duration);
  18. /* istanbul ignore next */
  19. var level = evt.branch ? 'debug' : 'info';
  20. log[level](
  21. 'Finished', '\'' + ansi.cyan(evt.name) + '\'',
  22. 'after', ansi.magenta(time)
  23. );
  24. });
  25. gulpInst.on('error', function(evt) {
  26. var msg = formatError(evt);
  27. var time = prettyTime(evt.duration);
  28. var level = evt.branch ? 'debug' : 'error';
  29. log[level](
  30. '\'' + ansi.cyan(evt.name) + '\'',
  31. ansi.red('errored after'),
  32. ansi.magenta(time)
  33. );
  34. // If we haven't logged this before, log it and add to list
  35. if (loggedErrors.indexOf(evt.error) === -1) {
  36. log.error(msg);
  37. loggedErrors.push(evt.error);
  38. }
  39. });
  40. }
  41. module.exports = logEvents;