#!/bin/bash # Auto-format files after writing # Runs appropriate formatter based on file extension set -e # Read the file path from stdin INPUT=$(cat) FILE_PATH=$(echo "$INPUT" | jq -r '.file_path // .filePath // empty') if [ -z "$FILE_PATH" ] || [ ! -f "$FILE_PATH" ]; then exit 0 fi # Get file extension EXT="${FILE_PATH##*.}" case "$EXT" in py) if command -v ruff &> /dev/null; then ruff format "$FILE_PATH" 2>/dev/null || true ruff check --fix "$FILE_PATH" 2>/dev/null || true fi ;; ts|tsx|js|jsx) # Try to find and use project's ESLint config DIR=$(dirname "$FILE_PATH") while [ "$DIR" != "/" ]; do if [ -f "$DIR/package.json" ]; then cd "$DIR" if [ -f "node_modules/.bin/eslint" ]; then ./node_modules/.bin/eslint --fix "$FILE_PATH" 2>/dev/null || true fi break fi DIR=$(dirname "$DIR") done ;; rs) if command -v rustfmt &> /dev/null; then rustfmt "$FILE_PATH" 2>/dev/null || true fi ;; tf) if command -v terraform &> /dev/null; then terraform fmt "$FILE_PATH" 2>/dev/null || true fi ;; yaml|yml) # Skip YAML formatting to preserve structure ;; esac exit 0