仅在最近修改的文件上运行grunt-contrib-jshint [英] Running grunt-contrib-jshint only on recently modified files

查看:109
本文介绍了仅在最近修改的文件上运行grunt-contrib-jshint的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在重构一个非常大的网站上的代码。我想对任何被更改的文件执行linting操作,但忽略其他文件(因为其中很多文件最终会被删除,因此浪费时间整理它们)。

我想要有一个咕task任务来检查文件的修改日期是否比它创建的日期(*从回购中获取)更新,如果是这种情况,请检查该日期是否更新(也可能会发生grunt更新json的文件列表)。



除了grunt及其插件之外,我还没有使用过更多的节点。我将以 http://gruntjs.com/creating-tasks 为起点,但有人可以为我画出我如何处理写作任务的方法,尤其是考虑如何处理不同步问题。

一对情侣选项:

<1> - 您可以使用自定义过滤器函数来过滤由您的jshint文件模式返回的文件列表。像这样:

  module.exports = function(grunt){
var fs = require('fs') ;

var myLibsPattern = ['./mylibs/**/*.js'];

//在Linux上,至少ctime在后续修改后不会被保留
//所以找到匹配过滤器模式的最早创建文件的日期/时间
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:允许3分钟退出回购
var filterSince =(new Date(earliestCreationTime)).getTime()+(3 * 60 * 1000);

grunt.initConfig({
选项:{
eqeqeq:true,
eqnull:true
},
jshint:{
sincecheckout:{
src:myLibsPattern,
//根据它是否比我们的repo创建时间更新来筛选
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 :{'b $ b files:['<%= jshint.all.src%>'],
tasks:['jshint'],
options:{nospawn:true}

},
jshint:{all:{src:['Gruntfile.js','lib / ** / *。js']}}
});
//在监视事件中,只将已更改的文件注入配置
grunt.event.on('watch',function(action,filepath){
grunt.config(['jshint ','all','src'],[filepath]);
});

这并不完全符合您的要求,因为它取决于只要开始修改文件,就会立即开始运行,但它可能更适合整体Grunt方法。 $ b

另请参阅这个问题,但要注意一些问题到旧版本的Grunt和coffeescript。



更新:现在有一个 grunt-newer 插件,它以更优雅的方式处理所有这些内容。


We're going through refactoring the code on a very large site. I would like to enforce linting on any files that get changed, but ignore the rest (as many of them will end up being removed so it's a waste of time to tidy them up).

I would like to have a grunt task that checks that a file's modified date is more recent than its created (*fetched from repo) date and lints it if this is the case (would be good also to have grunt update a json list of files to be linted).

I haven't used node much apart from grunt and its plugins. I'm going to use http://gruntjs.com/creating-tasks as a starting point, but could someone sketch out for me how I might approach writing this task, in particular any considerations to do with asynchronisity.

解决方案

A couple options:

1 - You can use a custom filter function to filter the list of files returned by your jshint file pattern. Something like this:

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 - use the grunt-contrib-watch plugin to detect when files are changing. Then you can read the list of files from the event, as described in this comment by 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]);
});

This doesn't exactly meet your requirements since it depends on having the watch running as soon as you start modifying files, but it might fit better with the overall Grunt approach.

See also this question but beware some of it relates to older version of Grunt and to coffeescript.

UPDATE: there's now a grunt-newer plugin which handles all this in a more elegant way

这篇关于仅在最近修改的文件上运行grunt-contrib-jshint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆