Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

gulp-jshint: How to fail the build?

I want my Gulp build to fail, if there are errors in JSHint.

According to the documentation of gulp-jshint I can use the "fail reporter".

However the following does not work:

gulp.task("lint", function() {
     return gulp.src(JS_SOURCES)
        .pipe(jshint())
        .pipe(jshint.reporter("jshint-stylish"))
        .pipe(jshint.reporter("fail"));
});

The task above always returns with exit code 0, even when there are errors in JSHint.

I am using gulp 3.8.10 and gulp-jshint 1.9.0.

There are discussions in the github issues of gulp-jshint here and here ... but according those discussions I gather that above code should work with the latest versions of gulp and gulp-jshint. However it does not ...

Has anybody figured out how to fail the build properly with gulp-jshint?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

TLDR; Until GulpJS comes with a good solution in a stable release, use the workaround as suggested by Bahmutov on GitHub.

He creates a workaround, using his own filter:

var map = require('map-stream');
var exitOnJshintError = map(function (file, cb) {
  if (!file.jshint.success) {
    console.error('jshint failed');
    process.exit(1);
  }
});
gulp.task('lint', function() {
  gulp.src('example.js')
    .pipe(jshint())
    .pipe(jshint.reporter('jshint-stylish'))
    .pipe(exitOnJshintError);
});

Long answer

This question has been posted as an issue on GitHub: How to fail gulp build? #6 . Pay special attention to Bahmutov's comment.

The solution (hack) he proposes is to add his own filter and do a process.exit(1); when there are hinting errors, which looks like this:

var map = require('map-stream');
var exitOnJshintError = map(function (file, cb) {
  if (!file.jshint.success) {
    console.error('jshint failed');
    process.exit(1);
  }
});

gulp.task('lint', function() {
  gulp.src('example.js')
    .pipe(jshint())
    .pipe(jshint.reporter('jshint-stylish'))
    .pipe(exitOnJshintError);
});

This issue links to another issue Plugin doesn't fail build #10. What they say here basically, is that Gulp should take care of the build failing. This results in another issue which has been reported on GulpJS: Controlling failing builds #113. Which on his turn has been move to "finish then fail" #20. The latter one has been fixed and the Gulp JS release can be tracked on: changing this #347.

So, we'll have to wait for it to be released...

In the mean time, we can use the workaround as mentioned at the top of my post in the TLDR;

I've implemented it my gulpfile.js in task scripts-app.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...