|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Claude Code Hook: Format Lua Files |
| 4 | +# Triggers after Claude edits/writes Lua files and runs nix fmt |
| 5 | +# |
| 6 | +# Environment variables provided by Claude Code: |
| 7 | +# - CLAUDE_PROJECT_DIR: Path to the project directory |
| 8 | +# - CLAUDE_TOOL_NAME: Name of the tool that was executed |
| 9 | +# - CLAUDE_TOOL_ARGS: JSON string containing tool arguments |
| 10 | + |
| 11 | +set -euo pipefail |
| 12 | + |
| 13 | +# Colors for output |
| 14 | +RED='\033[0;31m' |
| 15 | +GREEN='\033[0;32m' |
| 16 | +YELLOW='\033[1;33m' |
| 17 | +NC='\033[0m' # No Color |
| 18 | + |
| 19 | +# Log function |
| 20 | +log() { |
| 21 | + echo -e "[$(date '+%H:%M:%S')] $1" >&2 |
| 22 | +} |
| 23 | + |
| 24 | +# Parse tool arguments to get the file path |
| 25 | +get_file_path() { |
| 26 | + # Read hook input from stdin |
| 27 | + local hook_input |
| 28 | + if [ -t 0 ]; then |
| 29 | + # No stdin input available |
| 30 | + log "DEBUG: No stdin input available" |
| 31 | + return |
| 32 | + fi |
| 33 | + |
| 34 | + hook_input=$(cat) |
| 35 | + log "DEBUG: Hook input = $hook_input" |
| 36 | + |
| 37 | + # Try to extract file_path from tool_input |
| 38 | + local file_path |
| 39 | + file_path=$(echo "$hook_input" | grep -o '"file_path"[[:space:]]*:[[:space:]]*"[^"]*"' | sed 's/.*"file_path"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/') |
| 40 | + |
| 41 | + if [ -n "$file_path" ]; then |
| 42 | + echo "$file_path" |
| 43 | + return |
| 44 | + fi |
| 45 | + |
| 46 | + # Try extracting any .lua file path from the input |
| 47 | + local lua_path |
| 48 | + lua_path=$(echo "$hook_input" | grep -o '"[^"]*\.lua"' | sed 's/"//g' | head -1) |
| 49 | + |
| 50 | + if [ -n "$lua_path" ]; then |
| 51 | + echo "$lua_path" |
| 52 | + return |
| 53 | + fi |
| 54 | + |
| 55 | + log "DEBUG: Could not extract file path from hook input" |
| 56 | +} |
| 57 | + |
| 58 | +# Check if file is a Lua file |
| 59 | +is_lua_file() { |
| 60 | + local file="$1" |
| 61 | + [[ $file =~ \.lua$ ]] |
| 62 | +} |
| 63 | + |
| 64 | +# Main logic |
| 65 | +main() { |
| 66 | + log "${YELLOW}Claude Code Hook: Lua Formatter${NC}" |
| 67 | + |
| 68 | + # Get the file path from tool arguments |
| 69 | + FILE_PATH=$(get_file_path) |
| 70 | + |
| 71 | + if [ -z "$FILE_PATH" ]; then |
| 72 | + log "${RED}Error: Could not determine file path from tool arguments${NC}" |
| 73 | + exit 1 |
| 74 | + fi |
| 75 | + |
| 76 | + log "Tool: ${CLAUDE_TOOL_NAME:-unknown}, File: $FILE_PATH" |
| 77 | + |
| 78 | + # Check if it's a Lua file |
| 79 | + if ! is_lua_file "$FILE_PATH"; then |
| 80 | + log "Skipping: Not a Lua file ($FILE_PATH)" |
| 81 | + exit 0 |
| 82 | + fi |
| 83 | + |
| 84 | + # Check if file exists |
| 85 | + if [ ! -f "$FILE_PATH" ]; then |
| 86 | + log "${RED}Error: File does not exist: $FILE_PATH${NC}" |
| 87 | + exit 1 |
| 88 | + fi |
| 89 | + |
| 90 | + log "${YELLOW}Formatting Lua file with nix fmt...${NC}" |
| 91 | + |
| 92 | + # Change to project directory |
| 93 | + cd "${CLAUDE_PROJECT_DIR}" |
| 94 | + |
| 95 | + # Run nix fmt on the file |
| 96 | + if nix fmt "$FILE_PATH" 2>/dev/null; then |
| 97 | + log "${GREEN}✓ Successfully formatted: $FILE_PATH${NC}" |
| 98 | + exit 0 |
| 99 | + else |
| 100 | + EXIT_CODE=$? |
| 101 | + log "${RED}✗ nix fmt failed with exit code $EXIT_CODE${NC}" |
| 102 | + log "${RED}This indicates the file has formatting issues that need manual attention${NC}" |
| 103 | + |
| 104 | + # Don't fail the hook - just warn about formatting issues |
| 105 | + # This allows Claude's operation to continue while alerting about format problems |
| 106 | + log "${YELLOW}Continuing with Claude's operation, but please fix formatting issues${NC}" |
| 107 | + exit 0 |
| 108 | + fi |
| 109 | +} |
| 110 | + |
| 111 | +# Run main function |
| 112 | +main "$@" |
0 commit comments