Created
July 11, 2023 10:15
-
-
Save ikushlianski/72e41f07f43964b758c0e6e2d8ef9fe7 to your computer and use it in GitHub Desktop.
Manual pre-commit hook in a monorepo
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
#!/bin/bash | |
# avoid pre-commit hooks when fixing conflicts/merging | |
git rev-parse -q --verify MERGE_HEAD && exit 0 | |
# list all staged JS and TS files | |
js_ts_files=$(git diff --diff-filter=d --name-only --cached | grep -E '\.(js|jsx|ts|tsx)$') | |
############# | |
## LINTING ## | |
############# | |
for file in $js_ts_files; do | |
## BACKEND PROJECT ## | |
if [[ $file =~ ^"src" ]]; then | |
# changes have been made to `backend` project's `src`, check it | |
npx eslint --fix "$file" | |
linting_result=$? | |
git add "$file" | |
if [ $linting_result -ne 0 ]; then | |
exit 1 | |
else | |
echo "Linted ${file}" | |
fi | |
fi | |
if [[ $file =~ ^"stacks" ]]; then | |
# changes have been made to `backend` project's `stacks`, check it | |
npx eslint --fix "$file" | |
linting_result=$? | |
git add "$file" | |
if [ $linting_result -ne 0 ]; then | |
exit 1 | |
else | |
echo "Linted ${file}" | |
fi | |
fi | |
## FRONTEND PROJECT ## | |
if [[ $file =~ ^"frontend" ]]; then | |
# changes have been made to `frontend` project, check it | |
cd frontend || exit | |
# remove `frontend/` part from filename for ESLint to see the file | |
normalized="${file//frontend\//}" | |
npx eslint --fix "$normalized" | |
linting_result=$? | |
git add "$normalized" | |
if [ $linting_result -ne 0 ]; then | |
exit 1 | |
else | |
echo "Linted ${file}" | |
fi | |
cd .. | |
fi | |
done | |
############# | |
## TESTING ## | |
############# | |
set -e | |
should_run_backend_tests=false | |
should_run_frontend_tests=false | |
should_run_analog_cli_tests=false | |
for file in $js_ts_files; do | |
# check what the file path starts with | |
if [[ $file =~ ^"src" ]]; then # backend folder | |
should_run_backend_tests=true | |
fi | |
if [[ $file =~ ^"stacks" ]]; then # backend folder | |
should_run_backend_tests=true | |
fi | |
if [[ $file =~ ^"frontend" ]]; then | |
should_run_frontend_tests=true | |
fi | |
if [[ $file =~ ^"analog-cli" ]]; then | |
should_run_analog_cli_tests=true | |
fi | |
done | |
if [[ $should_run_backend_tests == true ]]; then | |
npm run test | |
fi | |
if [[ $should_run_frontend_tests == true ]]; then | |
cd frontend || exit | |
npm run test | |
cd .. | |
fi | |
if [[ $should_run_analog_cli_tests == true ]]; then | |
cd analog-cli || exit | |
npm run test | |
cd .. | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment