簡體   English   中英

如果JSHint在監視任務期間失敗,如何使Grunt構建失敗?

[英]How to fail Grunt build if JSHint fails during watch task?

這似乎是一個基本問題,但我不知道該怎么做。 這就是如何做到的

我想在保存帶有jshint錯誤的文件時使Grunt構建失敗。 輸出表明jshint失敗,但是Grunt仍然成功完成。

grunt.initConfig({
    watch: {
      js: {
        files: ['/scripts/{,**}/*.js'],
        tasks: ['newer:jshint:all']
      }
    }
})

我知道這里有grunt.fail但是我將在這里使用它嗎?

以下要點將通過CLI報告jshint錯誤,並且在保存.js文件時無法執行任何后續的構建步驟。

您將需要根據您的要求進行調整:

目錄結構:

project
│
├──package.json
│
├───scripts
│   │
│   └───test.js
│
├─── Gruntfile.js
│
└───node_modules
    │
    └─── ...

package.json

{
  "name": "stack40031078",
  "version": "0.0.1",
  "description": "Answer to stack question 40031078",
  "author": "RobC",
  "license": "Apache-2.0",
  "devDependencies": {
    "grunt": "^1.0.1",
    "grunt-contrib-jshint": "^1.0.0",
    "grunt-contrib-watch": "^1.0.0",
    "grunt-newer": "^1.2.0"
  }
}

Gruntfile.js

module.exports = function (grunt) {

    grunt.initConfig({

        pkg: grunt.file.readJSON('package.json'),

        // VALIDATE JS
        jshint: {
            // Note we're using 'src:' instead of 'all:' below.
            files: {
                src: './scripts/{,**}/*.js'
            },
            options: {
                // Use your jshint config here or define them in
                // a separate .jshintrc file and set the flag to:
                //
                // jshintrc: true
                curly: true,
                eqeqeq: true,
                immed: true,
                latedef: true,
                newcap: true,
                noarg: true,
                sub: true,
                undef: true,
                boss: true,
                eqnull: true,
                browser: true,
                smarttabs: true,
                globals: {}
            }
        },

        // WATCH THE JS FILES
        watch: {
            js: {
                files: ['./scripts/{,**}/*.js'],
                // NOTE: we're not using 'newer:jshint:all' below, just 'newer:jshint'
                tasks: ['newer:jshint' /* <-- Add subsequent build tasks here. E.g. ,'concat' - A registered task can also be added. E.g. 'default' */]
            }
        }

    });

    grunt.loadNpmTasks('grunt-contrib-jshint');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-newer');

    grunt.registerTask('default', [

    ]);

};

test.js

console.log('Hello World');

var test = function() {
    return 'test';
};

測試演示程序

  1. cdproject目錄
  2. 運行$ npm install
  3. 運行$ grunt watch
  4. 打開並對test.js進行簡單的編輯(例如,在文件末尾添加新行),然后保存更改。

CLI報告錯誤,如下所示:

    Running "jshint:files" (jshint) task
       ./scripts/test.js
          1 |console.log('Hello Universe');
             ^ 'console' is not defined.

    >> 1 error in 1 file
    Warning: Task "jshint:files" failed. Use --force to continue.

    Aborted due to warnings.
    Completed in 0.965s at Fri Oct 14 2016 10:22:59 GMT+0100 (BST) - Waiting...

注意:

中指定的任何后續的構建任務tasks數組的watch.js對象(如concat按在評論Gruntfile.js ),不會因為使用此要點來調用jshint任務失敗(...和concat任務有尚未定義!)。

但是,當JavaScript文件成功傳遞jshint任務時,將調用watch.js對象的tasks數組中定義的所有后續構建任務。

我希望這有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM