I recently started a project on github. I've managed to setup automatic testing after each commit using Travis. But now I would like to setup a pre-commit hook with jshint too. So if jshint reports errors, the commit should fail. But is this possible, and if so, how to do this ?
Open a terminal window by using option + T in GitKraken Client. Once the terminal windows is open, change directory to . git/hooks . Then use the command chmod +x pre-commit to make the pre-commit file executable.
pre-commit hooks are a mechanism of the version control system git. They let you execute code right before the commit. Confusingly, there is also a Python package called pre-commit which allows you to create and use pre-commit hooks with a way simpler interface.
There's an easier way of doing pre-commit checks (e.g. JSHint) in your Node.js workflow:
Install jshint from NPM:
npm install jshint
Next create a .jshintrc file in your project if you don't already have one. e.g: https://github.com/nelsonic/learn-jshint/blob/master/.jshintrc
Now install pre-commit module (and save it as a dev dependency):
npm install pre-commit --save-dev
Next you will need to define the task (script) that will be run for JSHint in your package.json
e.g:
{ "scripts": { "jshint": "jshint -c .jshintrc --exclude-path .gitignore ." } }
then you register the scripts you want to be run pre-commit (also in package.json) e.g:
"pre-commit": [ "jshint", "coverage", "etc" ]
This allows you to have more than just one check in your pre-commit workflow. (We have checks to ensure team members code complies with JSHint, Code Style and Test Coverage is 100%)
For a more detailed tutorial you can share with your team see: https://github.com/nelsonic/learn-pre-commit
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With