Created
January 7, 2017 00:28
-
-
Save marvinhagemeister/a427268a397fe668dfcac456a8e277e3 to your computer and use it in GitHub Desktop.
Git pre commit lint only staged files
This file contains 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
#!/usr/bin/env bash | |
BLUE='\033[1;34m' | |
BRIGHT_RED='\033[1;31m' | |
DARK_GRAY='\033[0;37m' | |
RESET='\033[m' | |
INFO="${BLUE}[INFO]${RESET}" | |
function task() { | |
if [ "$3" != "" ] && [ $1 ]; then | |
printf "$2 ..." | |
$1 $3 & | |
pid=$! | |
wait $pid | |
if [ $? == 0 ]; then | |
echo -ne "\033[0K\r$2 ... done!\n" | |
else | |
printf "${BRIGHT_RED} Please fix the above errors before committing.\n" | |
exit 1 | |
fi | |
fi | |
} | |
printf "\n" | |
# CSS + SCSS linting | |
css_files=$(git diff --cached --name-only | grep -i '\.s\?css$') | |
CSS_MESSAGE="${INFO} Linting stylesheets ${DARK_GRAY}[*.css, *.scss]${RESET}" | |
task ./node_modules/.bin/stylelint "$CSS_MESSAGE" "$css_files" | |
# JavaScript | |
js_files=$(git diff --cached --name-only | grep -i '\.jsx\?$') | |
JS_MESSAGE="${INFO} Linting scripts ${DARK_GRAY}[*.js, *.jsx]${RESET}" | |
task ./node_modules/.bin/eslint "$JS_MESSAGE" "$js_files" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment