Allenph
Allenph

Reputation: 2015

Don't run tasks if lint fails with Gulp?

I have a few Gulp tasks...

gulp.task('checkScssSyntax', function() {
    return gulp.src(scssFiles)
        .pipe(sassLint())
        .pipe(sassLint.format());
});

gulp.task('checkJavascriptSyntax', function() {
  var jsStream = gulp.src(jsFiles);
  return jsStream.pipe(jsHint())
    .pipe(jsHint.reporter('jshint-stylish'));
});

gulp.task('default', function() {
    runSequence(['checkJavascriptSyntax', 'checkScssSyntax'], function(error) {
      if (error) {
        console.log(error);
      } else {
        runSequence('compileJS', 'compileCSS', 'watch');
      }
    });
});

This doesn't seem to be working. Gulp simply runs the second runSequence regardless of what happens.

I would like Gulp to only run the second sequence of tasks only if there are no warnings, and no errors in either of the syntax checks. I would also like to avoid outputting ANYTHING to the console other than what the linters print to the console.

How can this be accomplished?

Upvotes: 0

Views: 467

Answers (1)

Sven Schoenung
Sven Schoenung

Reputation: 30564

Your checkScssSyntax and checkJavascriptSyntax tasks don't emit error events, so run-sequence doesn't notice when one of them has failed. Both gulp-sass-lint and gulp-jshint don't emit error events by default, but provide functions that instruct them to do so.

In your checkScssSyntax task use sassLint.failOnError():

gulp.task('checkScssSyntax', function() {
    return gulp.src(scssFiles)
        .pipe(sassLint())
        .pipe(sassLint.format())
        .pipe(sassLint.failOnError());
});

In your checkJavascriptSyntax task use jsHint.reporter('fail'):

gulp.task('checkJavascriptSyntax', function() {
  var jsStream = gulp.src(jsFiles);
  return jsStream.pipe(jsHint())
    .pipe(jsHint.reporter('jshint-stylish'))
    .pipe(jsHint.reporter('fail'));
});

I would also like to avoid outputting ANYTHING to the console other than what the linters print to the console

Gulp has a CLI flag that let's you suppress all gulp logging:

gulp --silent

Upvotes: 1

Related Questions