- Create script in
~/.local/bin
calledlintr
and paste the contents from below - Give the script execution permissions,
chmod +x ~/.local/bin/lintr
- Change to git repo where you want the hook to apply and run
echo "lintr" >> .git/hooks/pre-commit
- Done, now commit some files and test the script
Last active
October 6, 2020 19:25
-
-
Save danew/a844768a87ef55d4bdd4b0a5da6dd47f to your computer and use it in GitHub Desktop.
ESlint pre-commit hook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
FILE_LIST=$(git diff --staged --diff-filter=ACMTUXB --name-only | grep -E \\.[tj]sx?$) | |
if [ -z "$FILE_LIST" ]; then | |
echo "Nothing to lint" | |
exit 0 | |
fi | |
if ! npx eslint --fix --max-warnings 0 $FILE_LIST; then | |
echo "${bold}Lint Failed" | |
exit 1 | |
fi | |
git add $FILE_LIST | |
bold=$(tput bold) | |
normal=$(tput sgr0) | |
echo "${bold}Linted:${normal}" | |
echo $FILE_LIST | tr ' ' '\n' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment