Last active
December 31, 2016 05:44
-
-
Save alkah3st/c2189f105ecddf76ed17c89b2aa0930d to your computer and use it in GitHub Desktop.
Gulp Error Reporting with Jshint, Gulp-Notify, and Livereload
This file contains 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
/** | |
* init gulp plugins | |
*/ | |
var gulp = require('gulp'); | |
var plugins = require('gulp-load-plugins')(); | |
var map = require('map-stream'); | |
var events = require('events'); | |
var emitter = new events.EventEmitter(); | |
var path = require('path'); | |
var gutil = require('gulp-util'); | |
var currentTask = ''; | |
/** | |
* custom reporter for jshint-stylish | |
*/ | |
var jsHintErrorReporter = map(function (file, cb) { | |
if (!file.jshint.success) { | |
file.jshint.results.forEach(function (err) { | |
if (err) { | |
var msg = [ | |
path.basename(file.path), | |
'LINE: ' + err.error.line, | |
'ERROR: ' + err.error.reason | |
]; | |
// Emit this error event | |
emitter.emit('error', new Error(msg.join(" - "))); | |
} | |
}); | |
} | |
cb(null, file); | |
}); | |
/** | |
* error reporter object (for plugins) | |
*/ | |
var reportError = function (error) { | |
var lineNumber = (error.lineNumber) ? 'LINE ' + error.lineNumber + ' -- ' : ''; | |
var pluginName = (error.plugin) ? ': ['+error.plugin+']' : '['+currentTask+']'; | |
plugins.notify({ | |
title: 'Task Failed '+ pluginName, | |
message: lineNumber + 'See console.' | |
}).write(error); | |
gutil.beep(); | |
var report = ''; | |
var chalk = gutil.colors.white.bgRed; | |
report += chalk('TASK:') + pluginName+'\n'; | |
report += chalk('ERROR:') + ' ' + error.message + '\n'; | |
if (error.lineNumber) { report += chalk('LINE:') + ' ' + error.lineNumber + '\n'; } | |
if (error.fileName) { report += chalk('FILE:') + ' ' + error.fileName + '\n'; } | |
console.error(report); | |
this.emit('end'); | |
} | |
/** | |
* sass to css | |
*/ | |
gulp.task('sass', function () { | |
currentTask = 'sass'; | |
return gulp.src('styles/sass/screen.scss') | |
.pipe(plugins.plumber({ | |
errorHandler: reportError | |
})) | |
.pipe(plugins.sass()) | |
.pipe(gulp.dest('styles')) | |
.pipe(plugins.livereload()); | |
}); | |
/** | |
* jshints js/modules/*.js | |
*/ | |
gulp.task('jshint', function() { | |
return gulp.src(['js/modules/**/*.js']) | |
.pipe(plugins.jshint('.jshintrc')) | |
.pipe(plugins.jshint.reporter('jshint-stylish')) | |
.pipe(jsHintErrorReporter) | |
.on('error', plugins.notify.onError(function (error) { | |
return error.message; | |
} | |
)) | |
.pipe(plugins.livereload()); | |
}); | |
/** | |
* gulp watch | |
*/ | |
gulp.task('watch', function() { | |
plugins.livereload.listen(); | |
gulp.watch('styles/sass/**/**/*.scss', ['sass']); | |
gulp.watch(['js/modules/*.js'], ['jshint']); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment