UNPKG

1.95 kBJavaScriptView Raw
1'use strict';
2
3var grunt = require('../grunt');
4
5// The module to be exported.
6var fail = module.exports = {};
7
8// Error codes.
9fail.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// DRY it up!
19function writeln(e, mode) {
20 grunt.log.muted = false;
21 var msg = String(e.message || e);
22 if (!grunt.option('no-color')) { msg += '\x07'; } // Beep!
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// If --stack is enabled, log the appropriate error stack (if it exists).
34function 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// A fatal error occurred. Abort immediately.
45fail.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// Keep track of error and warning counts.
52fail.errorcount = 0;
53fail.warncount = 0;
54
55// A warning occurred. Abort immediately unless -f or --force was used.
56fail.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
68// This gets called at the very end.
69fail.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};