Customizable checks on pre-commit (staged) contents
npm install git-precommit-checks!version
!travis build
!node version
!dependencies
!coverage
!vulnerabilities
!MIT license

Documentation also available in French: 
Because we love git hooks and npm, we want to share and automate code/content quality.
git-precommit-checks has to be loaded manually or using any wrapper around git hooks.
As you can read below we highly recommend Husky.

npm install --save-dev git-precommit-checks
Configuration is loaded from the project root/top level directory using _git-precommit-checks.config.js_ or _git-precommit-checks.json_, or from _package.json_ so you can customize it according to your needs.
Here is an example using _git-precommit-checks.json_ configuration file:
``jsconsole.log
{
"rules": [
{
"filter": "\\.js$",
"nonBlocking": "true",
"message": "You’ve got leftover ",`
"regex": "console\\.log"
},
{
"message": "You’ve got leftover conflict markers",
"regex": "/^[<>|=]{4,}/m"
},
{
"message": "You have unfinished devs",
"nonBlocking": "true",
"regex": "(?:FIXME|TODO)"
}
]
}
Same thing using JS configuration file:
`js`
module.exports = {
display: {
notifications: true,
offendingContent: true,
rulesSummary: false,
shortStats: true,
verbose: false,
},
rules: [
{
message: 'You’ve got leftover conflict markers',
regex: /^[<>|=]{4,}/m,
},
{
filter: /^(?!README(_fr)?\.md)$/,
message: 'You have unfinished devs',
nonBlocking: true,
regex: /(?:FIXME|TODO)/,
},
],
}
When using _package.json_ file, you must add a dedicated git-precommit-checks key:
`jsconsole.log
"git-precommit-checks": {
"rules": [
{
"filter": "\\.js$",
"nonBlocking": "true",
"message": "You’ve got leftover ",`
"regex": "console\\.log"
},
{
"message": "You’ve got leftover conflict markers",
"regex": "/^[<>|=]{4,}/m"
},
{
"message": "You have unfinished devs",
"nonBlocking": "true",
"regex": "(?:FIXME|TODO)"
}
]
}
Each "pre-commit" entry is a checking rule: the pattern describes a regular expression that will be searched upon staged content. The associated message is displayed when the pattern is found.
Each rule will stop the commit when the associated pattern is found unless you set the nonBlocking key to true. Non blocking rules will print warning messages.
Only message and regex keys are mandatory.
You can also filter on files patterns using the filter key.
For instance, you'll get a warning about your package.json the first time you set the FIXME/TODO rule and every time you update that line. If you want to prevent such a warning you can extend that rule like this:
`js`
{
"filter": "^package\\.json$",
"message": "You have unfinished devs",
"nonBlocking": "true",
"regex": "(?:FIXME|TODO)"
}
⚠️ _There is no default checks configured after install, so please be aware that nothing will happend without adding your own rules!_
You can add an optional display entry in your config to enable some options:
`js`
"git-precommit-checks": {
"display": {
"notifications": true,
"offendingContent": true,
"rulesSummary": true,
"shortStats": true,
"verbose": true
},
…
- notifications: print error/warning summary using system notification.offendingContent
- : print offending contents right after associated file path and line number.rulesSummary
- : print rules as a table before parsing staged files.shortStats
- : print short stats (ie. 1 error, 1 warning.).verbose
- : print every performed action, files parsed, short summary/stats (errors and warnings number).
After installing locally or globally your module, add the following code (or equivalent) to your project pre-commit hook .git/hooks/pre-commit:
`bash
#!/bin/sh
scriptName="git-precommit-checks"
scriptPath="$(npm bin)/$scriptName"
if [ -f $scriptPath ]; then
$scriptPath
else
echo "Can't find $scriptName module"
echo "You can reinstall it using 'npm install $scriptName --save-dev' or delete this hook"
fi
`
Husky is a great tool to manage git hooks from your package.json.
You can use it and call git-precommit-checks on pre-commit:
`js``
"husky": {
"hooks": {
"pre-commit": "git-precommit-checks"
}
}
Any contribution is welcomed. Here is our contribution guideline