Skip to content
Shell
1.7 KB
Raw Blame
#!/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