Forked from noahmiller/gulpfile-error-handling.js
Last active
August 29, 2015 14:24
-
-
Save z3bbster/e9c436f6dfb93dfa2669 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var gulp = require('gulp'); | |
var gutil = require('gulp-util'); | |
var jshint = require('gulp-jshint'); | |
// Command line option: | |
// --fatal=[warning|error|off] | |
var fatalLevel = require('yargs').argv.fatal; | |
var ERROR_LEVELS = ['error', 'warning']; | |
// Return true if the given level is equal to or more severe than | |
// the configured fatality error level. | |
// If the fatalLevel is 'off', then this will always return false. | |
// Defaults the fatalLevel to 'error'. | |
function isFatal(level) { | |
return ERROR_LEVELS.indexOf(level) <= ERROR_LEVELS.indexOf(fatalLevel || 'error'); | |
} | |
// Handle an error based on its severity level. | |
// Log all levels, and exit the process for fatal levels. | |
function handleError(level, error) { | |
gutil.log(error.message); | |
if (isFatal(level)) { | |
process.exit(1); | |
} | |
} | |
// Convenience handler for error-level errors. | |
function onError(error) { handleError.call(this, 'error', error);} | |
// Convenience handler for warning-level errors. | |
function onWarning(error) { handleError.call(this, 'warning', error);} | |
var testfiles = ['error.js', 'warning.js']; | |
// Task that emits an error that's treated as a warning. | |
gulp.task('warning', function() { | |
gulp.src(testfiles). | |
pipe(jshint()). | |
pipe(jshint.reporter('fail')). | |
on('error', onWarning); | |
}); | |
// Task that emits an error that's treated as an error. | |
gulp.task('error', function() { | |
gulp.src(testfiles). | |
pipe(jshint()). | |
pipe(jshint.reporter('fail')). | |
on('error', onError); | |
}); | |
gulp.task('watch', function() { | |
// By default, errors during watch should not be fatal. | |
fatalLevel = fatalLevel || 'off'; | |
gulp.watch(testfiles, ['error']); | |
}); | |
gulp.task('default', ['watch']); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment