mirror of
https://github.com/luongnv89/claude-howto.git
synced 2026-04-30 21:57:48 +02:00
34259caf7c
The squash merge of PR #49 discarded two review fix commits. Re-applying: - security-scan.sh: wrap output in hookSpecificOutput (PostToolUse protocol); restore secret/token detection; fix JSON newline escaping; escape file path - validate-prompt.sh: try "user_prompt" field first, fall back to "prompt" - log-bash.sh: document sed truncation limitation for quoted commands - format-code.sh: fix misleading comment about updatedInput output
51 lines
1.3 KiB
Bash
51 lines
1.3 KiB
Bash
#!/bin/bash
|
|
# Auto-format code before writing
|
|
# Hook: PreToolUse:Write
|
|
#
|
|
# Reads the target file path from stdin JSON and runs the appropriate formatter
|
|
# in-place on the existing file (if it already exists on disk). For new files,
|
|
# formatting runs after Claude writes the file via a subsequent PostToolUse hook.
|
|
#
|
|
# 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" ]; then
|
|
exit 0
|
|
fi
|
|
|
|
# Detect file type and format accordingly
|
|
case "$FILE_PATH" in
|
|
*.js|*.jsx|*.ts|*.tsx)
|
|
if command -v prettier &> /dev/null; then
|
|
prettier --write "$FILE_PATH" 2>/dev/null
|
|
fi
|
|
;;
|
|
*.py)
|
|
if command -v black &> /dev/null; then
|
|
black "$FILE_PATH" 2>/dev/null
|
|
fi
|
|
;;
|
|
*.go)
|
|
if command -v gofmt &> /dev/null; then
|
|
gofmt -w "$FILE_PATH" 2>/dev/null
|
|
fi
|
|
;;
|
|
*.rs)
|
|
if command -v rustfmt &> /dev/null; then
|
|
rustfmt "$FILE_PATH" 2>/dev/null
|
|
fi
|
|
;;
|
|
*.java)
|
|
if command -v google-java-format &> /dev/null; then
|
|
google-java-format -i "$FILE_PATH" 2>/dev/null
|
|
fi
|
|
;;
|
|
esac
|
|
|
|
exit 0
|