Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimized pre-commit-hook action. #574

Merged
merged 3 commits into from
Jan 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ module.exports = function(grunt) {
fix: true
},
src: ['src/**/*.js', 'test/tests/**/*.js']
},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good approach.
btw
There are some common config fields in both source and sourceNofix, I wonder if we don't have to write them twice?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

though same but was unable to come up with such thing ;)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@montoyamoraga @therewasaguy do you think we can do smth better here ?

sourceNofix: {
options: {
configFile: './.eslintrc',
fix: false
},
src: ['src/**/*.js', 'test/tests/**/*.js']
}
},
webpack: {
Expand Down Expand Up @@ -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
}
}
});
Expand All @@ -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');
Expand Down
32 changes: 32 additions & 0 deletions templates/pre-commit-hook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// hooks/pre-commit.js
frappelatte28 marked this conversation as resolved.
Show resolved Hide resolved

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) {
endurance21 marked this conversation as resolved.
Show resolved Hide resolved

// 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);
});
}

});