首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >仅在最近修改的文件上运行grunt-contrib-jshint

仅在最近修改的文件上运行grunt-contrib-jshint
EN

Stack Overflow用户
提问于 2013-07-09 23:05:52
回答 2查看 3.3K关注 0票数 4

我们在一个非常大的站点上进行代码重构。我希望对任何更改过的文件强制执行linting,但忽略其他文件(因为它们中的许多文件最终都会被删除,所以清理它们是浪费时间)。

我想有一个grunt任务,检查一个文件的修改日期是否比它的创建日期(*从repo中获取)更新,如果是这样的话就链接它(如果grunt更新要链接的文件的json列表也会很好)。

除了grunt和它的插件,我没有用过太多的node。我将使用http://gruntjs.com/creating-tasks作为起点,但是否有人可以为我勾勒出如何编写此任务,特别是与异步有关的任何考虑因素。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-09 23:31:09

以下是几个选项:

1-可以使用custom filter function过滤由jshint文件模式返回的文件列表。如下所示:

代码语言:javascript
复制
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 plugin检测文件何时发生更改。然后,您可以读取事件中的文件列表,如Kyle Robinson Young (“shama”)的this comment中所述:

代码语言:javascript
复制
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 question,但要注意其中一些与旧版本的Grunt和coffeescript有关。

更新:现在有了一个grunt-newer插件,它能以更优雅的方式处理这一切

票数 10
EN

Stack Overflow用户

发布于 2014-03-10 17:29:41

为此,请使用grunt-newer。它特别用于配置Grunt任务,使其仅使用较新的文件运行。

示例:

代码语言:javascript
复制
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']);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17551720

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档