Skip to content

Instantly share code, notes, and snippets.

@sjungling
Last active September 3, 2017 15:45
Show Gist options
  • Save sjungling/5601127 to your computer and use it in GitHub Desktop.
Save sjungling/5601127 to your computer and use it in GitHub Desktop.
New Build Preflight Check
#!/bin/sh
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
NORMAL=$(tput sgr0)
function check_master() {
printf "Checking to see if this is the master branch… "
# CONSTANT(S)
BRANCH_PATTERN="^# On branch ([^${IFS}]*)"
# Get status w/current branch
git_status=$(git status 2> /dev/null)
# Get current branch name
if [[ ${git_status} =~ ${BRANCH_PATTERN} ]]; then
branch_name=${BASH_REMATCH[1]}
fi
if [[ "$branch_name" == "master" ]]; then
printf "${RED}✘ `whoami`, no! You should not commit to the master branch!${NORMAL}\n"
exit 1
fi
printf "${GREEN}✔ Yep, you're not on the master branch.${NORMAL}\n"
}
function check_javascript(){
if has_jshint && has_jshintconfig; then
has_javascript_modifications=$(git status -sb |grep -E "^(A|M).*\.js"|awk '{split($0,a,/\ +/);print a[2]}')
if [[ -n "$has_javascript_modifications" ]]; then
echo "Checking to ensure your Javascript changes are awesome…"
for f in $has_javascript_modifications
do
echo "Scanning: $f"
jshint $f
if [[ $? != 0 ]]; then
printf "${RED}✘ Please address the errors with '$f' before continuing with the commit ${NORMAL}\n"
exit 1
fi
done
printf "${GREEN}✔ Congrats! Your Javascript would make Crockford proud!${NORMAL}\n"
fi
else
printf "${RED}✘ Since you're missing part of the jsHint set-up, we'll skip that for now${NORMAL}\n"
fi
}
function has_jshint() {
printf "Checking for jshint… "
if [[ -z `which jshint` ]]; then
echo """
Whoa there partner, looks like you're missing a jsHint from your toolbelt.
To install jshint, you'll need to install: 'node', 'npm', and then you can run:
npm -g install jshint
After that, anytime you make changes to a Javascript file, we'll check to make
sure that everything looks good.
"""
return 1
fi
printf "${GREEN}✔ Found jsHint${NORMAL}\n"
return 0
}
function has_jshintconfig(){
printf "Checking for jshint config… "
if [[ ! -a '.jshintrc' ]]; then
printf "${RED}✘ Looks like you're missing the .jshintrc config file.${NORMAL}\n"
return 1
fi
printf "${GREEN}✔ Found jsHint config${NORMAL}\n"
return 0
}
function check_sass(){
if [[ -a `which sass-convert` ]]; then
has_scss_modifications=$(git status -sb |grep -E "^(A|M).*\.(scss|sass)"|awk '{split($0,a,/\ +/);print a[2]}')
if [[ -n "$has_scss_modifications" ]]; then
echo "Checking to ensure your Sass changes are totally rad…"
for f in $has_scss_modifications
do
echo "Sass cleaning: $f"
sass-convert -F scss -T scss -i $f
git add $f
if [[ $? != 0 ]]; then
printf "${RED}✘ Hrmmm… Looks like we found an error with '$f'\nPlease address the error(s) before continuing with the commit ${NORMAL}\n\n"
exit 1
fi
done
printf "${GREEN}✔ Congrats! You've written some sassy Sass!${NORMAL}\n\n"
fi
fi
}
# Run it
check_master
check_javascript
check_sass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment