diff --git a/Gruntfile.js b/Gruntfile.js index 5f7d56c9..74dc9e40 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -28,6 +28,13 @@ module.exports = function(grunt) { fix: true }, src: ['src/**/*.js', 'test/tests/**/*.js'] + }, + sourceNofix: { + options: { + configFile: './.eslintrc', + fix: false + }, + src: ['src/**/*.js', 'test/tests/**/*.js'] } }, webpack: { @@ -59,7 +66,10 @@ module.exports = function(grunt) { }, githooks: { all: { - 'pre-commit':'lint' //runs linting test before every git commit + options:{ + template:"templates/pre-commit-hook.js" + }, + 'pre-commit':'lint-nofix' //runs elint in -nofix mode before every git commit } } }); @@ -73,6 +83,7 @@ module.exports = function(grunt) { grunt.loadNpmTasks('grunt-githooks'); grunt.registerTask('lint', ['eslint:source']); + grunt.registerTask('lint-nofix', ['eslint:sourceNofix']); grunt.registerTask('default', ['webpack:prod', 'decomment']); grunt.registerTask('dev', ['eslint','connect','webpack:dev', 'decomment']); grunt.registerTask('serve', 'connect:server:keepalive'); diff --git a/templates/pre-commit-hook.js b/templates/pre-commit-hook.js new file mode 100644 index 00000000..cac3f608 --- /dev/null +++ b/templates/pre-commit-hook.js @@ -0,0 +1,32 @@ + // hooks/pre-commit.js + + const exec = require('child_process').exec; + // Executes shell commands synchronously + const sh = require('child_process').execSync; + + exec('git diff --cached --quiet', function (err, stdout, stderr) { + + // only run if there are staged changes + // i.e. what you would be committing if you ran "git commit" without "-a" option. + if (err) { + + // stash unstaged changes - only test what's being committed + sh('git stash --keep-index --quiet'); + + exec('grunt {{task}}', function (err, stdout, stderr) { + + console.log(stdout); + + // restore stashed changes + sh('git stash pop --quiet'); + + let exitCode = 0; + if (err) { + console.log(stderr); + exitCode = -1; + } + process.exit(exitCode); + }); + } + + }); \ No newline at end of file