2

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?

  • I just found the solution indirectly from this Jshint similar question: [JShint question](http://stackoverflow.com/questions/28115226/cant-make-gulp-notify-to-pop-up-a-error-message-when-gulp-jshint-fail) ``` gulp.task('eslint', () => { return gulp.src([config.js.all, '!app/assets/scripts/vendor/**']) .pipe(eslint()) .pipe(plumber()) .pipe(eslint.format()) .pipe(eslint.failAfterError()) .on('error', notify.onError({ message: 'JS error'})); }); ``` – Jose Pablo Granados Mar 24 '16 at 18:36

2 Answers2

2

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!

Community
  • 1
  • 1
technophobia
  • 2,644
  • 1
  • 20
  • 29
0

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());
Mathieu
  • 5,495
  • 2
  • 31
  • 48