Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A pre-commit hook that performs checks based on what is being committed.
#! /bin/bash
# Before we allow a commit, let's first enable preflight.
# To disable this check, set NO_PREFLIGHT=1
# The commit will fail if the preflight checks do.
set -e;
should_check() {
APP=$1
FOUND="-1"
while read file; do
if [[ "$file" = *"$APP"* ]]; then
FOUND="0";
break
fi
done <<< $(git diff --cached --staged --name-only)
echo "$FOUND"
}
## Begin Main ##
if [ -z "$NO_PREFLIGHT" ]; then
cat << EOF
-----------------------------------------------------
Running preflight checks... (this could take a while)
You might want to go get a coffee or talk a walk. ☕️
-----------------------------------------------------
EOF
if [[ $(should_check "api") -eq 0 ]]; then
echo "[$(date)] Linting the API..."
python flake8
echo "[$(date)] Running the API tests..."
pytest
else
echo "[$(date)] Skipping API. Nothing was modified."
fi
if [[ $(should_check "client") -eq 0 ]]; then
echo "[$(date)] Linting the client..."
yarn run lint
echo "[$(date)] Running the client tests..."
yarn run test
else
echo "[$(date)] Skipping client. Nothing was modified."
fi
cat << EOF
-----------------------------------------------------
Done with preflight checks! 🎉🎉🎉
Committing...
-----------------------------------------------------
EOF
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.