简体   繁体   English

当 gulp-eslint 失败时抛出 gulp-notify 消息

[英]Throw a gulp-notify message when gulp-eslint fails

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.当我的 ESlint 任务检测到错误时,我尝试使用 gulp-notify,但我无法使其工作,因为需要在某些 Node.js 流之后使用管道引用 gulp-notify。

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:我可以使用以下语法获取 gulp-eslint 中的错误:

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.当出现错误时,它会返回 console.log,我需要的是让 gulp-notify 在上面的代码中发送通知。 Could someone help me?有人可以帮助我吗?

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!快乐的棉绒!

gulp-notify documentation gives a few examples. gulp-notify 文档给出了一些例子。 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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM