Throw a gulp-notify message when gulp-eslint fails

1.4k views Asked by At

I'm trying to use gulp-notify when my ESlint task detect errors, but I can't make it work, since gulp-notify needs to be referenced with a pipe after some Node.js stream.

I can "make it work" partially with the following code:

return gulp.src([config.js.all, '!app/assets/scripts/vendor/**'])
.pipe(eslint())
.pipe(plumber())
.pipe(eslint.format())
.pipe(notify('Error!!!'))
.pipe(eslint.failAfterError());

However that throws the message always, not only when I have errors.

I can get the errors in gulp-eslint using the following syntax:

return gulp.src([config.js.all, '!app/assets/scripts/vendor/**'])
.pipe(eslint())
.pipe(plumber())
.pipe(eslint.format())
.pipe(notify('Error!!!'))
.pipe(eslint.result(function (result) {
    if(result.errorCount > 0){
        console.log('Error');
    }
}))
.pipe(eslint.failAfterError());

That returns me the console.log when there are errors, what I need is to make gulp-notify to send a notification inside the above code. Could someone help me?

2

There are 2 answers

0
Mathieu On

gulp-notify documentation gives a few examples. in your case, it should look like this:

return gulp.src([config.js.all, '!app/assets/scripts/vendor/**'])
.pipe(eslint())
.pipe(plumber())
.pipe(eslint.format())
.on("error",notify.onError('Error!!!'))
.pipe(eslint.failAfterError());
1
technophobia On

Mathieu's answer is almost right. Here's how to fix it:

return gulp.src([config.js.all, '!app/assets/scripts/vendor/**'])
  .pipe(eslint())
  .pipe(plumber())
  .pipe(eslint.format())
  .pipe(eslint.failAfterError())
  .on("error", notify.onError('Error!!!')); // <-- notify after eslint.failAfterError

Simple enough. Happy linting!