2015-01-09 25 views
28

如果JSHint中存在錯誤,我希望我的Gulp構建失敗。根據documentation of gulp-jshint我可以使用「失敗記者」。gulp-jshint:如何使構建失敗?

但是以下不工作:

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

上面的任務總是以退出代碼返回0,即使當有JSHint錯誤。

我使用gulp 3.8.10和gulp-jshint 1.9.0。

github討論gulp-jshint herehere ......但根據這些討論,我收集了上面的代碼應該與最新版本的gulp和gulp-jshint一起工作。但它不...

有沒有人想出瞭如何使用gulp-jshint正確構建失敗?

+0

對我的作品與[email protected]和[email protected]。你正在使用哪些版本? – Ben

+0

@Ben我也在使用gulp 3.8.10和gulp-jshint 1.9.0(正如問題所述)。如果我忽略'return',它也可以工作,但是退出代碼總是0.也許我應該在返回流時監聽錯誤事件? – jbandi

+0

你當然可以做到這一點,但你不應該。 :(我的代碼看起來和你的一樣,除了我使用'default'記者而不是'jshint-stylish'記者。 – Ben

回答

16

TLDR; 直到GulpJS在穩定版本中提供了良好的解決方案之後,請使用Bahmutov on GitHub建議的解決方法。

他創建了一個解決辦法,用他自己的過濾器:

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); 
}); 

龍答案

這個問題已經被張貼在GitHub上的問題: How to fail gulp build? #6。請特別注意Bahmutov's comment

他提出解決方案(黑客)是添加自己的過濾器,做一個process.exit(1);當有提示錯誤,它看起來像這樣:

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); 
}); 

此問題鏈接到另一個問題Plugin doesn't fail build #10。 他們在這裏說的基本上是,Gulp應該照顧構建失敗。 這會導致另一個在GulpJS上報告的問題:Controlling failing builds #113。其中輪到他已經搬到"finish then fail" #20。 後者已經修復,Gulp JS版本可以在changing this #347上進行跟蹤。

因此,我們將不得不等待它被釋放......

與此同時,我們可以使用如我在TLDR柱的頂部中提到的解決方法;

我已經實現了它我的gulpfile.js in task scripts-app

+3

這不再是最新的gulp,jshint和gulp-jshint的解決方案今天你可以做: 'gulp.task('check-scripts',function(){ return gulp.src('src/static/stuff/scripts/someImportantScript.js', 'src /static/stuff/scripts/controllers/*.js' 'src/static/stuff/scripts/services/*。js', ' .pipe(jshint.reporter('default')) .pipe(jshint.reporter('fail')); });' Gulp將退出代碼1. –

+1

使用運行序列時,這仍然是正確的答案。 –

11

它適合我。我有同樣的一飲而盡任務:

return gulp.src(['./src/**/*.js', './docs_src/**/*.js']) 
    .pipe(jshint()) 
    .pipe(jshint.reporter('jshint-stylish')) 
    .pipe(jshint.reporter('fail')) 

和這裏的會發生什麼:

$ gulp --version 
[11:03:41] CLI version 3.9.0 
[11:03:41] Local version 3.9.0 

[14559:3392 - 0:2151] 11:03:41 [[email protected]:o +1] ~/work/solo/fsstatic2 (master) 
$ cat package.json 
{ 
    "name": "fsstatic2", 
    "version": "0.0.0", 
    "description": "fsstatic", 
    "author": "FreedomSponsors", 
    "devDependencies": { 
    "gulp": "~3.9.0", 
    "gulp-concat": "~2.5.2", 
    "gulp-linker": "~0.1.7", 
    "gulp-webserver": "~0.9.1", 
    "yargs": "~3.12.0", 
    "gulp-sass": "~2.0.1", 
    "gulp-ng-templates": "0.0.6", 
    "gulp-ngtemplate": "~0.2.5", 
    "gulp-htmlmin": "~1.1.3", 
    "merge-stream": "~0.1.7", 
    "gulp-copy": "0.0.2", 
    "gulp-jshint": "~1.11.0", 
    "jshint-stylish": "~2.0.1" 
    } 
} 

[14559:3392 - 0:2152] 11:04:01 [[email protected]:o +1] ~/work/solo/fsstatic2 (master) 
$ gulp jshintall 
[11:04:11] Using gulpfile ~/work/solo/fsstatic2/gulpfile.js 
[11:04:11] Starting 'jshintall'... 

/home/tony/work/solo/fsstatic2/src/components/todo_example/todo.js 
    line 26 col 23 Missing semicolon. 

    ⚠ 1 warning 

[11:04:11] 'jshintall' errored after 467 ms 
[11:04:11] Error in plugin 'gulp-jshint' 
Message: 
    JSHint failed for: /home/tony/work/solo/fsstatic2/src/components/todo_example/todo.js 

[14559:3392 - 0:2153] 11:04:11 [[email protected]:o +1] ~/work/solo/fsstatic2 (master) 
$ echo $? 
1 
+1

從gulp-jshint 2.0.0開始,這是正確的答案。 –