Skip to content

Instantly share code, notes, and snippets.

@tarto-dev
Last active July 3, 2020 14:09
Show Gist options
  • Save tarto-dev/cfddb50b61f4d7c9a6cb244d578342f2 to your computer and use it in GitHub Desktop.
Save tarto-dev/cfddb50b61f4d7c9a6cb244d578342f2 to your computer and use it in GitHub Desktop.

Required packages

npm install --global standard snazzy # Will install STANDARD (Linter) and SNAZZY (Linter results beautifier)

Quick-install :

Go in project root folder (the one who contains .git folder) then type

$ curl -o \
.git/hooks/pre-commit \
https://gist.githubusercontent.com/benftwc/cfddb50b61f4d7c9a6cb244d578342f2/raw/8560d9f61daf4def4cc31ccc942e77a5efb8bdc9/pre-commit.sh \
&& chmod u+x .git/hooks/pre-commit 
#!/bin/bash
# Redirect output to stderr.
exec 1>&2
# enable user input
exec < /dev/tty
OUTPUT=0
if ! command -v standard &> /dev/null
then
echo "standard package is missing"
echo "Plase run npm install standard or check local installation"
exit 1
fi
if ! command -v snazzy &> /dev/null
then
echo "snazzy package is missing"
echo "Please run npm install snazzy or check local installation"
exit 1
fi
consoleregexp='console.log'
# Javascript CONSOLE.LOG presence testing
echo -e "===== console regex =====\n"
if test $(git diff --cached | grep $consoleregexp | wc -l) != 0
then
echo -e "Found console.log occurences 😏 \n"
exec git diff --cached | grep -ne $consoleregexp
echo -e "\n"
OUTPUT=1
fi
function xargs-r() {
if IFS= read -r -d $'\n' path; then
{ echo "$path"; cat; } | xargs $@
fi
}
# StandardJS testing
echo -e "===== JS standards ====="
git diff --name-only --cached --relative | grep '\.jsx\?$' | sed 's/[^[:alnum:]]/\\&/g' | xargs-r -E '' -t standard | snazzy
if [[ $? -ne 0 ]]; then
echo 'JavaScript Standard Style errors were detected. Aborting commit.'
OUTPUT=1
fi
if [[ $OUTPUT ]]
then
echo -e "\n\n\e[1m\e[40;38;5;82m"
read -p "Edit the COMMIT_MESSAGE dispite the above errors? (y/n) " yn
echo -e "\e[0m"
echo $yn | grep ^[Yy]$
if [ $? -eq 0 ]
then
OUTPUT=0; #THE USER WANTS TO CONTINUE
else
OUTPUT=1; # THE USER DONT WANT TO CONTINUE SO ROLLBACK
fi
fi
exit $OUTPUT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment