Shell
#!/bin/bash
# Post-Write Hook: Verifies code quality after file creation
FILE_PATH="$1"
FILE_EXT="${FILE_PATH##*.}"
# Skip verification for non-code files
case "$FILE_EXT" in
md|txt|json|yaml|yml|env|gitignore)
exit 0
;;
esac
# Detect project type and run appropriate checks
if [ -f "package.json" ]; then
echo "π Running verification checks..."
# TypeScript/JavaScript checks
if [[ "$FILE_EXT" == "ts" || "$FILE_EXT" == "tsx" || "$FILE_EXT" == "js" || "$FILE_EXT" == "jsx" ]]; then
# Type check if TypeScript
if command -v tsc &> /dev/null && [ -f "tsconfig.json" ]; then
echo "π Type checking..."
if ! npx tsc --noEmit 2>&1 | head -20; then
echo "β Type errors detected. Claude will continue fixing..."
exit 1
fi
fi
# Lint check
if [ -f ".eslintrc.json" ] || [ -f ".eslintrc.js" ] || [ -f "eslint.config.js" ]; then
echo "π§Ή Linting..."
if ! npx eslint "$FILE_PATH" --max-warnings 0 2>&1 | head -10; then
echo "β οΈ Lint issues detected. Claude will fix..."
exit 1
fi
fi
# Run tests if they exist
if [ -d "tests" ] || [ -d "test" ] || [ -d "__tests__" ]; then
echo "π§ͺ Running tests..."
if ! npm test -- --passWithNoTests 2>&1 | tail -20; then
echo "β Tests failed. Claude will fix..."
exit 1
fi
fi
fi
fi
# Python checks
if [[ "$FILE_EXT" == "py" ]]; then
if command -v python3 &> /dev/null; then
echo "π Checking Python syntax..."
if ! python3 -m py_compile "$FILE_PATH" 2>&1; then
echo "β Python syntax errors. Claude will fix..."
exit 1
fi
fi
fi
echo "β
All checks passed!"
exit 0