summaryrefslogtreecommitdiff
path: root/mcp-servers/token-gated-mcp-server/.claude/hooks/format-typescript.sh
blob: 6e50d6b7f1670452be225dabab21d5bdea038c4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash

# Hook script to format TypeScript files after editing
# Used in PostToolUse hooks for Edit/Write operations

# Parse the input JSON
file_path=$(echo "$CLAUDE_HOOK_DATA" | jq -r '.tool_input.file_path // empty')

# Exit if no file path
if [ -z "$file_path" ]; then
  exit 0
fi

# Only process TypeScript/JavaScript files
if [[ "$file_path" =~ \.(ts|tsx|js|jsx)$ ]]; then
  # Check if prettier is available
  if command -v npx &> /dev/null && [ -f "package.json" ]; then
    # Check if prettier is installed
    if npm list prettier --depth=0 &>/dev/null || npm list -g prettier --depth=0 &>/dev/null; then
      echo "🎨 Formatting $file_path with Prettier..."
      npx prettier --write "$file_path" 2>/dev/null
      
      if [ $? -eq 0 ]; then
        echo "✅ Formatted successfully"
      else
        echo "⚠️  Prettier formatting failed (non-critical)"
      fi
    fi
  fi
  
  # Additional validation for server files
  if [[ "$file_path" =~ (server|index)\.(ts|js)$ ]]; then
    # Check for token protection
    if grep -q 'radius.protect' "$file_path" 2>/dev/null; then
      echo "✅ Token protection detected in $file_path"
      
      # Count protected tools
      tool_count=$(grep -c 'radius.protect' "$file_path" 2>/dev/null)
      echo "   Found $tool_count protected tool(s)"
    fi
    
    # Check for proper FastMCP setup
    if grep -q 'FastMCP' "$file_path" 2>/dev/null; then
      echo "✅ FastMCP server configured"
    fi
    
    # Warn about missing error handling
    if ! grep -q 'try\|catch\|throw' "$file_path" 2>/dev/null; then
      echo "⚠️  Consider adding error handling to $file_path"
    fi
  fi
fi

exit 0