Skip to content

Commit 0ab0fec

Browse files
authored
Merge pull request #27 from ModusCreateOrg/improve-pre-commit-and-pre-push
Git Hooks and Code Quality Tools Improvement
2 parents 6030210 + 38b0315 commit 0ab0fec

File tree

3 files changed

+80
-8
lines changed

3 files changed

+80
-8
lines changed

.husky/pre-commit

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1-
sh ../lizard.sh
1+
#!/bin/sh
2+
project_root=$(pwd)
3+
cd $project_root
4+
sh ./lizard.sh
25

3-
cd frontend && npm run lint
6+
# Only run frontend lint if the directory exists
7+
if [ -d "frontend" ]; then
8+
cd frontend && npm run lint
9+
fi
10+
11+
cd $project_root
12+
13+
# Add backend linting
14+
if [ -d "backend" ]; then
15+
cd backend && npm run lint
16+
fi
17+
18+
exit 0

.husky/pre-push

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,53 @@
11
#!/usr/bin/env sh
2-
. "$(dirname -- "$0")/_/husky.sh"
32

4-
cd frontend && npm run lint && npm run test:ci
3+
# Get the range of commits being pushed
4+
# For the remote branch that doesn't exist yet, use the empty tree object
5+
RANGE="$(git rev-parse --verify HEAD)"
6+
7+
echo "Checking commit range: $RANGE"
8+
9+
# Check if any frontend files were modified in the commits being pushed
10+
FRONTEND_CHANGES=$(git diff --name-only $RANGE | grep "^frontend/" || true)
11+
12+
# Only run frontend tests if frontend files were modified
13+
if [ -n "$FRONTEND_CHANGES" ]; then
14+
echo "Frontend changes detected. Running frontend tests..."
15+
16+
# Navigate to frontend directory
17+
cd frontend || exit 1
18+
19+
. "$(dirname -- "$0")/_/husky.sh"
20+
21+
cd frontend && npm run lint && npm run test:ci
22+
23+
# Capture the exit code
24+
TEST_EXIT_CODE=$?
25+
26+
# Return to the original directory
27+
cd ..
28+
29+
# If tests failed, prevent the push
30+
if [ $TEST_EXIT_CODE -ne 0 ]; then
31+
echo "Frontend tests failed. Push aborted."
32+
exit 1
33+
fi
34+
35+
echo "Frontend tests passed."
36+
else
37+
echo "No frontend changes detected. Skipping frontend tests."
38+
fi
39+
40+
# Check if any backend files were modified in the commits being pushed
41+
BACKEND_CHANGES=$(git diff --name-only $RANGE | grep "^backend/" || true)
42+
43+
if [ -n "$BACKEND_CHANGES" ]; then
44+
echo "Backend changes detected. Running backend lint..."
45+
46+
. "$(dirname -- "$0")/_/husky.sh"
47+
cd backend && npm run lint
48+
else
49+
echo "No backend changes detected. Skipping backend tests."
50+
fi
51+
52+
# Continue with the push
53+
exit 0

lizard.sh

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
#!/bin/bash
22

33
# Check if lizard is installed
4-
if ! command -v lizard &> /dev/null; then
4+
if command -v lizard &> /dev/null; then
5+
echo "✅ lizard found in PATH"
6+
elif [ -x ~/.local/bin/lizard ]; then
7+
echo "✅ lizard found in user bin, adding to PATH"
8+
export PATH="$HOME/.local/bin:$PATH"
9+
elif [ -x /usr/local/bin/lizard ]; then
10+
echo "✅ lizard found in system bin, adding to PATH"
11+
export PATH="/usr/local/bin:$PATH"
12+
else
513
echo "❌ lizard is not installed."
614
echo "To install lizard globally, run:"
715
echo " pip install lizard"
@@ -16,7 +24,7 @@ NLOC_LIMIT=250
1624
FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.tsx$|\.ts$')
1725

1826
# Check if there are any files to analyze
19-
if [[ -z "$FILES" ]]; then
27+
if [ -z "$FILES" ]; then
2028
echo "No Typescript files to check"
2129
exit 0 # No files to check, allow commit
2230
fi
@@ -32,13 +40,13 @@ for FILE in $FILES; do
3240
OUTPUT=$(lizard -C $COMPLEXITY_LIMIT -Tnloc=$NLOC_LIMIT -w --warning-msvs "$FILE")
3341
ERROR_CODE=$?
3442

35-
if [[ "$ERROR_CODE" -ne 0 ]]; then
43+
if [ "$ERROR_CODE" -ne 0 ]; then
3644
ERROR_LIST+="$OUTPUT\n"
3745
fi
3846
done
3947

4048
# Check if there were any errors
41-
if [[ -n "$ERROR_LIST" ]]; then
49+
if [ -n "$ERROR_LIST" ]; then
4250
echo "❌ Commit aborted due to the errors in the following files:"
4351
echo "$ERROR_LIST"
4452
echo "Limits are: Code Complexity (CCN)=$COMPLEXITY_LIMIT and Number of Lines (NLOC)=$NLOC_LIMIT"

0 commit comments

Comments
 (0)