Skip to content

Instantly share code, notes, and snippets.

@etc-tiago
Created February 17, 2021 11:22
Show Gist options
  • Save etc-tiago/8eeafe9509e35eed7aef0a0558be94fa to your computer and use it in GitHub Desktop.
Save etc-tiago/8eeafe9509e35eed7aef0a0558be94fa to your computer and use it in GitHub Desktop.
Precommit ESLint
#!/bin/sh
RED="\033[1;31m"
GREEN="\033[1;32m"
NC="\033[0m"
linter_exit_code=1
staged_js_files=$(git diff --cached --diff-filter=d --name-only | grep .js$)
./node_modules/.bin/eslint $staged_js_files --quiet --fix
linter_exit_code=$?
git add -f $staged_js_files
if [ $linter_exit_code -ne 0 ]
then
echo "${RED} ❌ Linter errors have occurred${NC}"
exit 1
else
echo "${GREEN} ✔ Eslint did not find any errors${NC}"
exit 0
fi
###############
#### The name of file should be .githooks-pre-commit, but the gist not allow /
###############
{
"scripts": {
"postinstall": "git config core.hooksPath ./.githooks"
},
"devDependencies": {
"eslint": "^7.15.0",
"eslint-config-prettier": "^7.0.0",
"eslint-plugin-prettier": "^3.2.0",
"prettier": "^2.2.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment