我是gulp的新手,因此我将一个简单的脚本放在一起1)了解gulp如何工作以及2)改进我的开发工作流程。
我遇到的问题是,如果我将runSequence任务列表设置为jscs然后lint的顺序,则lint任务不会被执行。但是,如果我将它们反转,并首先运行lint,然后运行jscs,则两个任务都会成功执行。在做了更多测试之后,我确定我使用的jscs导致了问题,或者jscs存在一个问题,导致无法以这种方式执行。
/* File: gulpfile.js */
/* grab the packages we will be using */
var gulp = require('gulp');
var gutil = require('gulp-util');
var jscs = require('gulp-jscs');
var jsHint = require('gulp-jshint');
var jsHintStylish = require('jshint-stylish');
var runSequence = require('run-sequence');
var console = require('better-console');
// configure the default task and add the watch task to it
gulp.task('default', ['watch']);
// configure the jscs task
gulp.task('jscs', function() {
return gulp.src('src/app/**/*.js')
.pipe(jscs('.jscsrc'));
});
// configure the lint task
gulp.task('lint', function() {
return gulp.src("src/app/**/*.js")
.pipe(jsHint('.jshintrc'))
.pipe(jsHint.reporter(jsHintStylish));
});
// configure the watch task - set up which files to watch and what tasks to use when those files change
gulp.task('watch',function() {
// Clear console
console.clear();
// setup a watch against the files that need to be checked on save
gulp.watch('src/app/**/*.js', function(){
// Clear console so only current issues are shown
console.clear();
// Execute tasks to check code for issues
runSequence('lint','jscs',function(){
gutil.log("Code Check Complete.");
});
});
});
{
"name": "my project name",
"version": "0.1.0",
"author": {
"name": "my name",
"email": "myemail@domain.com"
},
"devDependencies": {
"better-console": "^0.2.4",
"gulp": "^3.8.11",
"gulp-jscs": "^1.6.0",
"gulp-jshint": "^1.11.0",
"gulp-util": "^3.0.4",
"jshint-stylish": "^1.0.2",
"run-sequence": "^1.1.0"
}
}
(function() {
'use strict;
var x = ;
})();
'lint','jscs',function(){...}
时的示例输出:[15:10:49] Starting 'lint'...
c:\Users\anatha\My Projects\Tracks\Tracks.Client\src\app\app.js
line 3 col 17 Unclosed string.
line 4 col 1 Unclosed string.
line 5 col 14 Unclosed string.
line 6 col 1 Unclosed string.
line 7 col 6 Unclosed string.
line 8 col 1 Unclosed string.
line 3 col 5 Unclosed string.
line 1 col 13 Missing semicolon.
line 8 col 1 Missing "use strict" statement.
line 1 col 13 Unmatched '{'.
line 1 col 1 Unmatched '('.
line 8 col 1 Expected an assignment or function call and instead saw an expression.
line 8 col 1 Missing semicolon.
× 4 errors
‼ 9 warnings
[15:10:49] Finished 'lint' after 104 ms
[15:10:49] Starting 'jscs'...
[15:10:49] 'jscs' errored after 157 ms
[15:10:49] Error in plugin 'gulp-jscs'
Message:
Unexpected token ILLEGAL at app.js :
1 |(function() {
2 |
3 | 'use strict;
-----------------------^
4 |
5 | var x = ;
[15:10:49] Code Check Complete.
'jscs','lint',function(){...}
时的示例输出(请注意,永远不会执行lint语句):[15:09:48] Starting 'jscs'...
[15:09:48] 'jscs' errored after 178 ms
[15:09:48] Error in plugin 'gulp-jscs'
Message:
Unexpected token ILLEGAL at app.js :
1 |(function() {
2 |
3 | 'use strict;
-----------------------^
4 |
5 | var x = ;
[15:09:48] Code Check Complete.
答案 0 :(得分:0)
在花了几个小时研究问题的原因之后,我把它缩小到JSCS在确定它检查的代码不遵守用户设置的规范时抛出错误的事实。抛出此错误时,它会禁止执行运行序列中的后续任务;然而,奇怪的是,永远不会生成未处理的错误事件来提醒用户该问题。
这与JSHINT的运作方式不同。当JSHINT确定代码中存在错误时,它只是输出代码违规,但不会抛出导致后续任务永远不会被触发的错误。
我更新了我的脚本以解决此问题,方法是捕获未处理的错误,并对该错误执行最少量的处理,以便完成运行序列中指定的其余任务。
gulpfile.js
文件:/* File: gulpfile.js */
/* grab the packages we will be using */
var gulp = require('gulp');
var gUtil = require('gulp-util');
var jscs = require('gulp-jscs');
var jsHint = require('gulp-jshint');
var jsHintStylish = require('jshint-stylish');
var runSequence = require('run-sequence');
var console = require('better-console');
// default error handler
var handleJscsError = function(err) {
console.log("Error: " + err.toString());
this.emit('end');
}
// configure the default task and add the watch task to it
gulp.task('default', ['watch']);
// configure the jscs task
gulp.task('jscs', function() {
return gulp.src('src/app/**/*.js')
.pipe(jscs('.jscsrc'))
.on('error',handleJscsError);
});
// configure the lint task
gulp.task('lint', function() {
return gulp.src("src/app/**/*.js")
.pipe(jsHint('.jshintrc'))
.pipe(jsHint.reporter(jsHintStylish));
});
// configure the watch task - set up which files to watch and what tasks to use when those files change
gulp.task('watch', function() {
// Clear console
console.clear();
// setup a watch against the files that need to be checked on save
return gulp.watch('src/app/**/*.js', function(){
// Clear console so only current issues are shown
console.clear();
// Execute tasks to check code for issues
runSequence('jscs','lint',function(){
console.log("Tasks Completed.")
});
});
});
我尝试通过创建自定义JSCS记者来解决此问题。虽然我确实得到了一些功能,但它真的感觉像是对我而言是一个坚实的解决方案。并且要补充一点,JSCS不提供像JSHINT这样的自定义报告确实使得这更加丑陋,因为知道一旦support is added for custom reporting,我就不得不重构代码。