Running grunt-contrib-jshint only on recently modified files

We refactor code on a very large site. I would like to use linting for any files that change but ignore the rest (since many of them will be deleted, so it is a waste of time to put them in order).

I would like to have a grunt task that checks that the file modification date is later than its created date (given from the repo date) and draw it if it is (it would be nice to also have a json groovy list of files that need to draw).

I have not used node much except the grunt and its plugins. I am going to use http://gruntjs.com/creating-tasks as a starting point, but someone can outline for me how I could approach writing this task, in particular any considerations related to asynchronism.

+4
source share
2 answers

A couple of options:

1 - You can use the custom filter function to filter the list of files returned by your jshint template. Something like that:

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 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 determine when files change. You can then read the list of files from the event, as described in this comment from 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 does not exactly match, as it depends on how the watch will work as soon as you start modifying files, but is best suited for the general Grunt approach.

See also this question , but be careful, some of them relate to the old version of Grunt and coffeescript.

UPDATE: now there is a grunt-newer plugin that handles all this in a more elegant way

+9
source

Use grunt-newer for this. It is especially recommended that you configure Grunt tasks to work only with newer files.

Example:

 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
source

Source: https://habr.com/ru/post/1490509/


All Articles