简体   繁体   English

使用带有运行序列的gulp时任务未执行的问题

[英]Issue with task not executing when using gulp with run-sequence

I am new to gulp and so I put a simple script together to 1) learn how gulp works and 2) improve my development workflow. 我是gulp的新手,因此我将一个简单的脚本组合在一起:1)了解gulp的工作原理,以及2)改善开发工作流程。

The issue I am having is that if I set the runSequence task list to be in the order of jscs and then lint, the lint task does not get executed. 我遇到的问题是,如果我将runSequence任务列表设置为jscs然后是lint的顺序,则不会执行lint任务。 However, if I reverse them, and run lint first, then jscs, both tasks execute successfully. 但是,如果我反转它们,并先运行lint,然后运行jscs,则两个任务都将成功执行。 After doing more tests, I'm certain that there is something about the way I am using jscs that is causing the problem, or there is an issue with jscs that prevents this from executing in this way. 经过更多测试之后,我确定是我使用jscs的方式引起了问题,或者jscs出现了问题,阻止了这种方式的执行。

Things I have checked: 我检查过的事情:

  1. Dependencies have been installed globally. 依赖项已全局安装。
  2. Dependencies have been setup in my package.json. 在我的package.json中已经设置了依赖项。
  3. All of the needed require statements at the top of my gulpfile.js are established. gulpfile.js顶部的所有必需的require语句都已建立。
  4. Checked that the .jshintrc file that I reference for jshint does in fact exist 检查我为jshint引用的.jshintrc文件是否确实存在
  5. Checked that the .jscsrc file that I reference for jscs does in fact exist 检查我为jscs引用的.jscsrc文件是否确实存在

gulpfile.js: 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: 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 file in the folder that is being watched. 正在监视的文件夹中的JavaScript文件。 Notice the errors that I placed so that both jscs and lint will report issues: 注意我放置的错误,以便jscs和lint都将报告问题:

(function() {

    'use strict;

    var x = ;

})();

Sample output when the run sequence task ordering is 'lint','jscs',function(){...} : 运行序列任务排序为'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.

Sample output when the run sequence task ordering is 'jscs','lint',function(){...} (Notice that the lint statement is never executed): 运行序列任务排序为'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.

After spending a number of hours researching the cause of the issue, I narrowed it down to the fact that JSCS throws an error when it determines that the code it has checked doesn't abide by the specs set by the user. 在花了几个小时研究了问题的原因之后,我将其范围缩小到以下事实:JSCS在确定它检查的代码不遵守用户设置的规范时抛出错误。 When this error is thrown, it inhibits subsequent tasks in the run-sequence from being executed; 引发此错误时,它会禁止运行顺序中的后续任务; however, what is strange is that an unhandled error event is never generated to alert the user of the issue. 但是,奇怪的是,从未生成未处理的错误事件来警告用户该问题。

This is different than the way JSHINT operates. 这与JSHINT的操作方式不同。 When JSHINT determines there is an error in the code, it simply outputs the code violations, but doesn't throw an error that causes subsequent tasks to never be triggered. 当JSHINT确定代码中存在错误时,它仅输出违反代码的行为,而不会引发导致后续任务永远不会被触发的错误。

I updated my script to work around this issue by catching the unhandled error, and performing the minimal amount of processing for that error that was needed to allow the remaining tasks specified in the run-sequence to be completed. 我更新了脚本,以解决此问题,方法是捕获未处理的错误,并对该错误执行最少的处理,以完成运行序列中指定的其余任务。

Updated gulpfile.js file: 更新了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.")
        });

    });

});

Another thing to note: 另一件事要注意:

I tried working around the issue by creating a custom JSCS reporter. 我尝试通过创建自定义JSCS报告程序来解决此问题。 And while I did get this somewhat functional, it really felt like a kludge to me rather than a solid solution. 尽管我确实做到了这一点,但对我而言,这确实是一种麻烦,而不是一个可靠的解决方案。 And to add that JSCS doesn't provide custom reporting like JSHINT does made this even uglier knowing that once support is added for custom reporting , I'd have to refactor the code anyway. 而且要补充一点,JSCS不像JSHINT那样提供自定义报告,这使我更加难看,因为一旦添加了对自定义报告的支持 ,我仍然必须重构代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Gulp和run-sequence错误:任务未配置为gulp上的任务 - Gulp and run-sequence error : Task is not configured as a task on gulp Gulp运行序列“'taskname'是无效的任务字符串”错误 - Gulp run-sequence “'taskname' is a not valid task string” error gulp.watch通过运行序列任务后仅运行一次 - gulp.watch only runs once when passed a run-sequence task Gulp:插件“运行序列(默认)MSBuild 失败,代码为 3221225477!”。 Gulp 任务 Visual Studio 2017 - Gulp: Error in plugin 'run-sequence(default) MSBuild failed with code 3221225477!. Gulp task Visual Studio 2017 Gulp (pump, run-sequence) “任务完成回调调用了太多次” - Gulp (pump, run-sequence) "task completion callback called too many times" “ gulp默认任务”和“运行序列npm模块”之间有什么区别? - what's the differnece between the 'gulp default task' and 'run-sequence npm modules'? Gulp插件错误:插件'run-sequence'出错:任务'ngconstant:prod'发生错误 - Gulp plugin error : Error in plugin 'run-sequence' : An error occured in task 'ngconstant:prod' 运行序列同步任务永远不会完成 - run-sequence synchronous task never completes run-sequence不按顺序运行gulp任务 - run-sequence doesn't run gulp tasks in order 运行顺序中的第二个任务不会运行 - Second task in run-sequence doesn't run
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM