1 | 'use strict';
|
2 |
|
3 | var grunt = require('../grunt');
|
4 |
|
5 |
|
6 | var fail = module.exports = {};
|
7 |
|
8 |
|
9 | fail.code = {
|
10 | FATAL_ERROR: 1,
|
11 | MISSING_GRUNTFILE: 2,
|
12 | TASK_FAILURE: 3,
|
13 | TEMPLATE_ERROR: 4,
|
14 | INVALID_AUTOCOMPLETE: 5,
|
15 | WARNING: 6,
|
16 | };
|
17 |
|
18 |
|
19 | function writeln(e, mode) {
|
20 | grunt.log.muted = false;
|
21 | var msg = String(e.message || e);
|
22 | if (!grunt.option('no-color')) { msg += '\x07'; }
|
23 | if (mode === 'warn') {
|
24 | msg = 'Warning: ' + msg + ' ';
|
25 | msg += (grunt.option('force') ? 'Used --force, continuing.'.underline : 'Use --force to continue.');
|
26 | msg = msg.yellow;
|
27 | } else {
|
28 | msg = ('Fatal error: ' + msg).red;
|
29 | }
|
30 | grunt.log.writeln(msg);
|
31 | }
|
32 |
|
33 |
|
34 | function dumpStack(e) {
|
35 | if (grunt.option('stack')) {
|
36 | if (e.origError && e.origError.stack) {
|
37 | console.log(e.origError.stack);
|
38 | } else if (e.stack) {
|
39 | console.log(e.stack);
|
40 | }
|
41 | }
|
42 | }
|
43 |
|
44 |
|
45 | fail.fatal = function(e, errcode) {
|
46 | writeln(e, 'fatal');
|
47 | dumpStack(e);
|
48 | grunt.util.exit(typeof errcode === 'number' ? errcode : fail.code.FATAL_ERROR);
|
49 | };
|
50 |
|
51 |
|
52 | fail.errorcount = 0;
|
53 | fail.warncount = 0;
|
54 |
|
55 |
|
56 | fail.warn = function(e, errcode) {
|
57 | var message = typeof e === 'string' ? e : e.message;
|
58 | fail.warncount++;
|
59 | writeln(message, 'warn');
|
60 |
|
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 |
|
68 |
|
69 | fail.report = function() {
|
70 | if (fail.warncount > 0) {
|
71 | grunt.log.writeln().fail('Done, but with warnings.');
|
72 | } else {
|
73 | grunt.log.writeln().success('Done.');
|
74 | }
|
75 | };
|