当 gulp-jshint 失败时,无法使 gulp-notify 弹出错误消息

2022-01-12 00:00:00 node.js javascript gulp jshint gulp-notify

所以,我们整天都在研究新的 FE 工作流程,主要想法是通过观察者运行一些任务,我们将拥有我们的 IDE 和一个窗口,每次发生变化时都会刷新浏览器(scss、js, html 等).

So, was the whole day working on our new FE workflow, the main idea is to run a few tasks through a watcher, we will have our IDE's and a window with the browsers getting refreshed each time something change (scss, js, html, etc).

因此,如果一切顺利,我们将什么也看不到:浏览器将重新加载,我们将继续工作.

So, we wont see nothing if all went good: the browser will get reload and we will keep working.

但是,我们想使用 gulp notify 来处理错误,所以万一我们有问题,会弹出通知程序,此时您可以检查控制台并查看错误.

But, we want to use gulp notify for errors, so in case we have something wrong, the notifier pop up will appear, and at that point, you can check the console and see the error.

失败时的 sass 任务示例:

Example for our sass task when fail:

对于我们的样式任务,一切正常,但我无法为 JS 文件完成此操作.

For our styles tasks, everything works fine, but Im not able to accomplish this for JS files.

主要是,我不能让 gulp-jshint 像我想要的那样工作.

Mainly, I cant make gulp-jshint works as I want.

这将是这样的:你保存你的 js 文件,如果失败,弹出窗口,当你看到这个时,你进入控制台,你会看到 jshint-stylish 的输出.

Which will be like this: you save your js file, if it fails, the popup appear and when you see this, you go to the console and you see the output of jshint-stylish.

有人可以帮我吗?

这是我的(非工作)解决方案:

This is my (non working) solution:

gulp.task( 'js-hint', function() {
    return gulp.src( config.source )
        .pipe( plumber() )
        .pipe( jshint( config.jsHintRules ) )
        .pipe( jshint.reporter( 'jshint-stylish' ) )
        .on('error', notify.onError( { message: 'JS hint fail' } ) );
});

显然,我的概念有问题,所以,有人能帮我指明正确的方向吗?

Obviously, I have a problem with concepts, so, will somebody can please put me on the right direction ?

请注意,我只想一直发出相同的消息JS提示失败"

Note that I just want to emit the same message all the time "JS hint fail"

然后,控制台(感谢 jshint-stylish)将为我们提供更多信息.

Then, the console (thanks to jshint-stylish) will gave us more info.

此外,这将在一个观察者身上.JS提示通过后,会执行更多的任务,比如borwserify,所以重要的是如果它停止了,等待"直到我完成修复然后继续.

Also, this will be on a watcher. After JS hint pass, will execute more tasks, like borwserify, so its important that if its stop, "wait" till I finish fixing it and then keep going.

谢谢!

PS,我正在使用:

吞咽 3.8.10吞咽-jshint 1.8.6吞咽通知 2.0.1

gulp 3.8.10 gulp-jshint 1.8.6 gulp-notify 2.0.1

通过 OSX 10.9.4

Over a OSX 10.9.4

推荐答案

gulp 中的 error 事件仅在流中发生错误时触发.

The error event in gulp only fires when an error in the stream occurs.

要将错误报告器添加到 gulp-jshint 将其传递给 fail 报告器:

To add an error reporter to gulp-jshint pass it through the fail reporter:

 gulp.task('js-hint', function() {
      return gulp.src(config.source)
        .pipe(plumber())
        .pipe(jshint( config.jsHintRules))
        .pipe(jshint.reporter('jshint-stylish'))
        .pipe(jshint.reporter('fail'))
        .on('error', notify.onError({ message: 'JS hint fail'}));
  });

相关文章