whe*_*hys 4 javascript node.js jshint gruntjs
我们正在重构一个非常大的网站上的代码.我想对任何改变的文件强制执行linting,但忽略其余的文件(因为其中许多文件最终会被删除,所以浪费时间来整理它们).
我想有一个咕噜咕噜的任务,检查文件的修改日期是否比它创建的更新(*从repo获取)日期并且如果是这种情况则将其删除(也可以让grunt更新文件的json列表)被打印).
除了grunt及其插件,我还没有使用节点.我将使用http://gruntjs.com/creating-tasks作为起点,但是有人可以为我绘制一个如何编写此任务的方法,特别是与异步性有关的任何注意事项.
exp*_*nit 10
几个选项:
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']);
};
Run Code Online (Sandbox Code Playgroud)
2 - 使用grunt-contrib-watch插件检测文件何时发生变化.然后你就可以从事件中读取文件的列表,如在此评论被凯尔·罗宾逊年轻("莎玛"):
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]);
});
Run Code Online (Sandbox Code Playgroud)
这并不完全符合您的要求,因为它取决于您开始修改文件时手表的运行,但它可能更适合整体Grunt方法.
另请参阅此问题,但要注意其中一些与旧版本的Grunt和coffeescript有关.
更新:现在有一个咕噜咕噜的新插件,它以更优雅的方式处理所有这些
| 归档时间: |
|
| 查看次数: |
3259 次 |
| 最近记录: |