spalger / gulp-jshint

JSHint plugin for gulp
MIT License
419 stars 67 forks source link

Fail reporter doesn't stop the following tasks when using plumber #121

Open ghost opened 8 years ago

ghost commented 8 years ago

Hi I have an issue with the fail reporter. My use case is that I want jshint to make the task fail so the following pipes and tasks are not executed but I am also using it inside a watch and I don't want the watch to stop working on failure. I've managed to reach that behaviour for less files but it doesn't work with jshint.

// Error handlers
var onError = function(error) {
    notify.onError({
        message: error.message
    })(error);
    this.emit('end');
};

// JS validation
gulp.task('jshint', function() {
    return gulp
        .src(paths.jsDev + '/**/*.js')
        .pipe(plumber({errorHandler: onError}))
        .pipe(jshint())
        .pipe(jshint.reporter(stylish))
        .pipe(jshint.reporter('fail'));
});

// JS build
gulp.task('js', ['jshint'], function() {
    return gulp
        .src([
            paths.bowerComp + '/fancybox/source/jquery.fancybox.pack.js',
            paths.jsDev + '/**/*.js'
        ])
        .pipe(plumber({errorHandler: onError}))
        .pipe(concat('script.js'))
        .pipe(uglify())
        .pipe(gulp.dest(paths.js));
});

// Watch
gulp.task('watch', function() {

    // Watch .js files
    gulp.watch(paths.jsDev + '/**/*.js', ['js']);
});

So basically on failure the "js" task shouldn't be executed but the watch should keep working.

But this is the output I get:

$ gulp js
[13:36:44] Using gulpfile ~/PhpStorm/Projects/****/gulpfile.js
[13:36:44] Starting 'jshint'...

/****/src/js/script.js
  line 24  col 53  Missing semicolon.

  ⚠  1 warning

[13:36:44] gulp-notify: [Error running Gulp] JSHint failed for: /****/src/js/script.js
[13:36:44] Finished 'jshint' after 100 ms
[13:36:44] Starting 'js'...
[13:36:44] Finished 'js' after 651 ms
spalger commented 8 years ago

I'm open to suggestions on how this should work. How did you get it to work with your less task?

ghost commented 8 years ago

Well I'm far from being a Gulp or Node.js expert. Just using it in my web development workflow.

Here is my less task if it can help you:

// CSS
gulp.task('less', function() {
    return gulp.src(paths.less + '/style.less')
        .pipe(plumber({errorHandler: onError}))
        .pipe(less())
        .pipe(autoprefixer('> 1%', 'last 2 versions', 'ie >= 8'))
        .pipe(minifycss(minifyOptions))
        .pipe(gulp.dest(paths.css));
});

// Watch
gulp.task('watch', function() {

    // Watch .less files
    gulp.watch(paths.less + '/**/*.less', ['less']);

    // Watch .js files
    gulp.watch(paths.jsDev + '/**/*.js', ['js']);
});

In this case if the less() call fails, the following pipe (autoprefixer) is not executed but the watch task keeps working. I'm handling the error the same way as with the jshint task so I guess the errors are not triggered the same way by the less plugin and your plugin.