Files
AI_template/.claude/hooks/post-edit-format.sh

35 lines
811 B
Bash
Executable File

#!/bin/bash
# Auto-format files after Edit/Write using Prettier
# Event: PostToolUse | Matcher: Edit|Write
# Silently skips if Prettier is not installed or file doesn't exist
INPUT=$(cat)
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // empty')
if [ -z "$FILE_PATH" ] || [ ! -f "$FILE_PATH" ]; then
exit 0
fi
EXTENSIONS="ts tsx js jsx json css scss html md yaml yml"
FILE_EXT="${FILE_PATH##*.}"
SHOULD_FORMAT=false
for ext in $EXTENSIONS; do
if [ "$FILE_EXT" = "$ext" ]; then
SHOULD_FORMAT=true
break
fi
done
if [ "$SHOULD_FORMAT" = false ]; then
exit 0
fi
if command -v npx &>/dev/null && [ -f "node_modules/.bin/prettier" ]; then
npx prettier --write "$FILE_PATH" 2>/dev/null
elif command -v prettier &>/dev/null; then
prettier --write "$FILE_PATH" 2>/dev/null
fi
exit 0