#!/bin/sh . "$(dirname "$0")/_/husky.sh" # Get the list of staged JS/CSS files STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(js|jsx|ts|tsx|css|scss)$') # If there are no staged JS/CSS files, exit if [ -z "$STAGED_FILES" ]; then echo "No JS/CSS files staged for commit." exit 0 fi # Function to display errors display_errors() { local errors="$1" echo "Errors detected:" echo "---------------------------------------" echo "$errors" echo "---------------------------------------" } # Run Prettier echo "Running Prettier..." for FILE in $STAGED_FILES; do npx prettier --write "$FILE" done # Run ESLint for JS/TS files only echo "Running ESLint..." ESLINT_ERRORS=0 for FILE in $STAGED_FILES; do if echo "$FILE" | grep -E '\.(js|jsx|ts|tsx)$' > /dev/null; then ESLINT_OUTPUT=$(npx eslint "$FILE" 2>&1) if [ $? -ne 0 ]; then display_errors "$ESLINT_OUTPUT" ESLINT_ERRORS=1 fi fi done if [ $ESLINT_ERRORS -ne 0 ]; then echo "ESLint errors detected. Please fix them before committing." exit 1 fi # Add the fixed files back to the staging area for FILE in $STAGED_FILES; do git add "$FILE" done echo "JS/CSS pre-commit checks completed."