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