UNPKG

2.46 kBJavaScriptView Raw
1/*
2 * grunt
3 * https://github.com/cowboy/grunt
4 *
5 * Copyright (c) 2012 "Cowboy" Ben Alman
6 * Licensed under the MIT license.
7 * http://benalman.com/about/license/
8 */
9
10var grunt = require('../grunt');
11
12// The module to be exported.
13var fail = module.exports = {};
14
15// Error codes
16// 1. Generic error.
17// 2. Config file not found.
18// 3. Generic task failed.
19// 10. Uglify-JS error.
20// 11. Banner generation error.
21// 20. Init error.
22// 90-99. Nodeunit/QUnit errors.
23
24// DRY it up!
25function writeln(e, mode) {
26 grunt.log.muted = false;
27 // Pretty colors.
28 var tags = {
29 warn: ['<'.red + 'WARN'.yellow + '>'.red, '</'.red + 'WARN'.yellow + '>'.red],
30 fatal: ['<'.red + 'FATAL'.yellow + '>'.red, '</'.red + 'FATAL'.yellow + '>'.red]
31 };
32 var msg = String(e.message || e) + '\x07'; // Beep!
33 if (mode === 'warn') {
34 msg += ' ' + (grunt.option('force') ? 'Used --force, continuing.'.underline : 'Use --force to continue.');
35 }
36 grunt.log.writeln([tags[mode][0], msg.yellow, tags[mode][1]].join(' '));
37}
38
39// A fatal error occured. Abort immediately.
40fail.fatal = function(e, errcode) {
41 writeln(e, 'fatal');
42 process.reallyExit(typeof errcode === 'number' ? errcode : 1);
43};
44
45// Keep track of error and warning counts.
46fail.errorcount = 0;
47fail.warncount = 0;
48
49// Something (like the watch task) can override this to perform an alternate
50// action to exiting on warn.
51fail.warnAlternate = null;
52
53// A warning ocurred. Abort immediately unless -f or --force was used.
54fail.warn = function(e, errcode) {
55 var message = typeof e === 'string' ? e : e.message;
56 fail.warncount++;
57 writeln(message, 'warn');
58 // If -f or --force aren't used, stop script processing.
59 if (!grunt.option('force')) {
60 if (fail.warnAlternate) {
61 fail.warnAlternate();
62 } else {
63 // If --debug is enabled, log the appropriate error stack (if it exists).
64 if (grunt.option('debug') >= 9) {
65 if (e.origError && e.origError.stack) {
66 console.log(e.origError.stack);
67 } else if (e.stack) {
68 console.log(e.stack);
69 }
70 }
71 // Log and exit.
72 grunt.log.writeln().fail('Aborted due to warnings.');
73 process.reallyExit(typeof errcode === 'number' ? errcode : 2);
74 }
75 }
76};
77
78// This gets called at the very end.
79fail.report = function() {
80 if (fail.warncount > 0) {
81 grunt.log.writeln().fail('Done, but with warnings.');
82 } else {
83 grunt.log.writeln().success('Done, without errors.');
84 }
85};