#!/bin/bash# Auto-format code after writing# Hook: PostToolUse:Write## Reads the target file path from stdin JSON and runs the appropriate formatter# in-place on the file after Claude writes it.## Compatible with: macOS, Linux, Windows (Git Bash)# Read JSON input from stdin (Claude Code hook protocol)
INPUT=$(cat)
# Extract file_path using sed (compatible with all platforms)
FILE_PATH=$(echo"$INPUT" | sed -n 's/.*"file_path"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -1)
if [ -z "$FILE_PATH" ] || [ ! -f "$FILE_PATH" ]; thenexit 0
fi# Detect file type and format accordinglycase"$FILE_PATH"in
*.js|*.jsx|*.ts|*.tsx)
ifcommand -v prettier &> /dev/null; then
prettier --write "$FILE_PATH" 2>/dev/null
fi
;;
*.py)
ifcommand -v black &> /dev/null; then
black "$FILE_PATH" 2>/dev/null
fi
;;
*.go)
ifcommand -v gofmt &> /dev/null; then
gofmt -w "$FILE_PATH" 2>/dev/null
fi
;;
*.rs)
ifcommand -v rustfmt &> /dev/null; then
rustfmt "$FILE_PATH" 2>/dev/null
fi
;;
*.java)
ifcommand -v google-java-format &> /dev/null; then
google-java-format -i "$FILE_PATH" 2>/dev/null
fi
;;
esacexit 0