仅对最近修改的文件运行grunt contrib jshint
Running grunt-contrib-jshint only on recently modified files
我们正在一个非常大的站点上重构代码。我想对任何更改的文件强制执行linting,但忽略其余文件(因为其中许多文件最终会被删除,所以整理它们是浪费时间)。
我想要一个grunt任务,检查文件的修改日期是否比其创建日期(*从repo获取)更近,如果是这种情况,则对其进行linted(也可以让grunt更新要linted的文件的json列表)。
除了grunt和它的插件之外,我并没有使用太多node。我要用http://gruntjs.com/creating-tasks作为一个起点,但有人能为我勾勒出如何编写这项任务,特别是与异步有关的任何考虑因素吗。
几个选项:
1-您可以使用自定义过滤器函数来过滤jshint文件模式返回的文件列表。类似这样的东西:
module.exports = function(grunt) {
var fs = require('fs');
var myLibsPattern = ['./mylibs/**/*.js'];
// on linux, at least, ctime is not retained after subsequent modifications,
// so find the date/time of the earliest-created file matching the filter pattern
var creationTimes = grunt.file.expand( myLibsPattern ).map(function(f) { return new Date(fs.lstatSync(f).ctime).getTime() });
var earliestCreationTime = Math.min.apply(Math, creationTimes);
// hack: allow for 3 minutes to check out from repo
var filterSince = (new Date(earliestCreationTime)).getTime() + (3 * 60 * 1000);
grunt.initConfig({
options: {
eqeqeq: true,
eqnull: true
},
jshint: {
sincecheckout: {
src: myLibsPattern,
// filter based on whether it's newer than our repo creation time
filter: function(filepath) {
return (fs.lstatSync(filepath).mtime > filterSince);
},
},
},
});
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.registerTask('default', ['jshint']);
};
2-使用grunt contrib watch插件来检测文件何时发生更改。然后你可以阅读事件中的文件列表,如Kyle Robinson Young("shama")的评论所述:
grunt.initConfig({
watch: {
all: {
files: ['<%= jshint.all.src %>'],
tasks: ['jshint'],
options: { nospawn: true }
}
},
jshint: { all: { src: ['Gruntfile.js', 'lib/**/*.js'] } }
});
// On watch events, inject only the changed files into the config
grunt.event.on('watch', function(action, filepath) {
grunt.config(['jshint', 'all', 'src'], [filepath]);
});
这并不完全符合您的要求,因为这取决于您一开始修改文件就让手表运行,但它可能更适合Grunt的整体方法。
也可以看到这个问题,但要注意,其中一些问题与旧版本的Grunt和coffeescript有关。
更新:现在有一个更新的插件,它以更优雅的方式处理所有这些。
为此使用更新的grunt。它特别用于将Grunt任务配置为仅使用较新的文件运行。
示例:
grunt.initConfig({
jshint: {
options: {
jshintrc: '.jshintrc'
},
all: {
src: 'src/**/*.js'
}
}
});
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-newer');
grunt.registerTask('lint', ['newer:jshint:all']);
相关文章:
- 检查grunt是否在详细模式下运行
- 运行 grunt.file.copy 返回错误代码:ENOENT
- Node.js/Grunt - 无法运行 Grunt 的手表 - 为什么
- 在另一个任务之前异步运行 grunt 任务
- 如何在我的 Yeoman 生成器完成安装后运行 Grunt 任务
- 在不阻塞终端的情况下运行Grunt Watch
- 运行grunt contrib imagemin时出错
- 当运行grunt时,我得到SyntaxError:意外的令牌(
- 仅对最近修改的文件运行grunt contrib jshint
- 直接从节点运行Grunt任务
- Angular主存储库运行grunt测试失败;的任务
- 运行Grunt Express Server导致错误:enent: no such file or directory,
- 在Windows上运行Grunt时出错:致命错误:无法找到本地Grunt
- 使用QUnit运行Grunt时出现JSHint错误
- 运行Grunt任务返回错误
- 当我运行Grunt时,我得到警告说'default'未找到任务
- 运行“Grunt服务”时出错.使用generator-angular-fullstack种子
- 在VM共享目录上运行“grunt”
- QUnit测试在Travis CI上失败(在phantomjs上运行grunt.js)
- 运行grunt只是打开一个记事本文件,而不是实际运行任务