4

非常に大規模なサイトでコードのリファクタリングを行っています。変更されたすべてのファイルに lint を強制したいのですが、残りは無視します (それらの多くは最終的に削除されるため、整理するのは時間の無駄です)。

ファイルの変更日が作成された(*レポからフェッチされた)日付よりも新しいことを確認し、その場合はそれをリントするうなり声のあるタスクが必要です(ファイルのjsonリストをうなり声で更新するのも良いでしょう)リントされる)。

私は、grunt とそのプラグインを除いて、node をあまり使用していません。http://gruntjs.com/creating-tasksを出発点として使用しますが、このタスクの作成方法、特に非同期性に関する考慮事項を誰かがスケッチしてくれませんか。

4

2 に答える 2

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']);
};

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 に関連するものがあることに注意してください。

更新:これらすべてをよりエレガントな方法で処理する、より新しいプラグインがあります

于 2013-07-09T15:31:09.160 に答える