我是新手,所以我把一个简单的脚本放在一起,1)学习gulp的工作原理,2)改进我的开发工作流程。
我遇到的问题是,如果我将runSequence任务列表设置为jscs顺序,然后设置为lint,则不会执行lint任务。但是,如果我反转它们,并首先运行lint,然后运行jscs,这两个任务都会成功执行。在做了更多的测试之后,我确信我使用jscs的方式会导致问题,或者jscs有一个问题阻止了它以这种方式执行。
我检查过的东西:
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');
// 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.");
});
});
});package.json:
{
"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"
}
}正在监视的文件夹中的JavaScript文件。注意我放置的错误,这样jscs和lint都会报告问题:
(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.发布于 2015-05-24 18:07:04
在花了几个小时研究这个问题的原因之后,我缩小了范围,因为当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那样提供自定义报告,这使得这一点更加丑陋,知道一旦为自定义报告添加了支持。,我将不得不重构代码无论如何。
https://stackoverflow.com/questions/30418131
复制相似问题