Skip to content

Instantly share code, notes, and snippets.

@danew
Last active October 6, 2020 19:25
Show Gist options
  • Save danew/a844768a87ef55d4bdd4b0a5da6dd47f to your computer and use it in GitHub Desktop.
Save danew/a844768a87ef55d4bdd4b0a5da6dd47f to your computer and use it in GitHub Desktop.
ESlint pre-commit hook

Instructions

  1. Create script in ~/.local/bin called lintr and paste the contents from below
  2. Give the script execution permissions, chmod +x ~/.local/bin/lintr
  3. Change to git repo where you want the hook to apply and run echo "lintr" >> .git/hooks/pre-commit
  4. Done, now commit some files and test the script
#!/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