Sin descripción

fail.js 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * grunt
  3. * http://gruntjs.com/
  4. *
  5. * Copyright (c) 2014 "Cowboy" Ben Alman
  6. * Licensed under the MIT license.
  7. * https://github.com/gruntjs/grunt/blob/master/LICENSE-MIT
  8. */
  9. 'use strict';
  10. var grunt = require('../grunt');
  11. // The module to be exported.
  12. var fail = module.exports = {};
  13. // Error codes.
  14. fail.code = {
  15. FATAL_ERROR: 1,
  16. MISSING_GRUNTFILE: 2,
  17. TASK_FAILURE: 3,
  18. TEMPLATE_ERROR: 4,
  19. INVALID_AUTOCOMPLETE: 5,
  20. WARNING: 6,
  21. };
  22. // DRY it up!
  23. function writeln(e, mode) {
  24. grunt.log.muted = false;
  25. var msg = String(e.message || e);
  26. if (!grunt.option('no-color')) { msg += '\x07'; } // Beep!
  27. if (mode === 'warn') {
  28. msg = 'Warning: ' + msg + ' ';
  29. msg += (grunt.option('force') ? 'Used --force, continuing.'.underline : 'Use --force to continue.');
  30. msg = msg.yellow;
  31. } else {
  32. msg = ('Fatal error: ' + msg).red;
  33. }
  34. grunt.log.writeln(msg);
  35. }
  36. // If --stack is enabled, log the appropriate error stack (if it exists).
  37. function dumpStack(e) {
  38. if (grunt.option('stack')) {
  39. if (e.origError && e.origError.stack) {
  40. console.log(e.origError.stack);
  41. } else if (e.stack) {
  42. console.log(e.stack);
  43. }
  44. }
  45. }
  46. // A fatal error occurred. Abort immediately.
  47. fail.fatal = function(e, errcode) {
  48. writeln(e, 'fatal');
  49. dumpStack(e);
  50. grunt.util.exit(typeof errcode === 'number' ? errcode : fail.code.FATAL_ERROR);
  51. };
  52. // Keep track of error and warning counts.
  53. fail.errorcount = 0;
  54. fail.warncount = 0;
  55. // A warning occurred. Abort immediately unless -f or --force was used.
  56. fail.warn = function(e, errcode) {
  57. var message = typeof e === 'string' ? e : e.message;
  58. fail.warncount++;
  59. writeln(message, 'warn');
  60. // If -f or --force aren't used, stop script processing.
  61. if (!grunt.option('force')) {
  62. dumpStack(e);
  63. grunt.log.writeln().fail('Aborted due to warnings.');
  64. grunt.util.exit(typeof errcode === 'number' ? errcode : fail.code.WARNING);
  65. }
  66. };
  67. // This gets called at the very end.
  68. fail.report = function() {
  69. if (fail.warncount > 0) {
  70. grunt.log.writeln().fail('Done, but with warnings.');
  71. } else {
  72. grunt.log.writeln().success('Done, without errors.');
  73. }
  74. };