[英]How to fail Grunt build if JSHint fails during watch task?
This seems like a basic question but I can't figure out how to do it. 这似乎是一个基本问题,但我不知道该怎么做。 This is how to do it in gulp .
这就是如何做到的 。
I want when I save a file with a jshint error to fail the Grunt build. 我想在保存带有jshint错误的文件时使Grunt构建失败。 The output states that jshint failed but Grunt still completes successfully.
输出表明jshint失败,但是Grunt仍然成功完成。
grunt.initConfig({
watch: {
js: {
files: ['/scripts/{,**}/*.js'],
tasks: ['newer:jshint:all']
}
}
})
I know there is grunt.fail
but how would I use it here? 我知道这里有
grunt.fail
但是我将在这里使用它吗?
The following gist will report a jshint
error via the CLI and fail to execute any subsequent build steps when saving the .js
file. 以下要点将通过CLI报告
jshint
错误,并且在保存.js
文件时无法执行任何后续的构建步骤。
You will need to adapt according to your requirements : 您将需要根据您的要求进行调整:
project
│
├──package.json
│
├───scripts
│ │
│ └───test.js
│
├─── Gruntfile.js
│
└───node_modules
│
└─── ...
{
"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"
}
}
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', [
]);
};
console.log('Hello World');
var test = function() {
return 'test';
};
cd
to the project
directory cd
到project
目录 $ npm install
$ npm install
$ grunt watch
$ grunt watch
test.js
, (eg add a new line to the end of the file), and save the change. test.js
进行简单的编辑(例如,在文件末尾添加新行),然后保存更改。 The CLI reports the error as follows: 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...
NOTE: 注意:
Any subsequent build tasks specified in the tasks
array of the watch.js
object, (eg concat
as per commented in the Gruntfile.js
), will not be invoked using this gist as the jshint
task fails (... and the concat
task has not been defined of course!). 中指定的任何后续的构建任务
tasks
数组的watch.js
对象(如concat
按在评论Gruntfile.js
),不会因为使用此要点来调用jshint
任务失败(...和concat
任务有尚未定义!)。
However, when the JavaScript file/s successfully pass the jshint
task, any subsequent build tasks that are defined in the tasks
array of the watch.js
object will be invoked. 但是,当JavaScript文件成功传递
jshint
任务时,将调用watch.js
对象的tasks
数组中定义的所有后续构建任务。
I hope this helps! 我希望这有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.