Back to Hooks

File Size Guard

PreToolUse

Prevents writing excessively large files that could bloat the repository, with configurable size limits per file type

safetyfile-sizerepositoryguard

Hook Script

#!/bin/bash
# File Size Guard Hook
# Prevents writing files that exceed size limits

FILE_PATH="$1"
CONTENT="$2"

# Default max size in bytes (500KB)
MAX_SIZE=512000

# Get content size
CONTENT_SIZE=$(echo -n "$CONTENT" | wc -c)

# Check file extension for adjusted limits
case "$FILE_PATH" in
  *.min.js|*.min.css|*.map)
    MAX_SIZE=2048000  # 2MB for minified/map files
    ;;
  *.json)
    MAX_SIZE=1048576  # 1MB for JSON files
    ;;
  *.svg)
    MAX_SIZE=1048576  # 1MB for SVG files
    ;;
  *.lock|*-lock.json)
    MAX_SIZE=5242880  # 5MB for lock files
    ;;
esac

if [ "$CONTENT_SIZE" -gt "$MAX_SIZE" ]; then
  echo "BLOCKED: File size ($CONTENT_SIZE bytes) exceeds limit ($MAX_SIZE bytes)"
  echo "File: $FILE_PATH"
  echo "Consider splitting the file or using a different approach."
  exit 1
fi

exit 0

Settings Configuration

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Write",
        "command": "./hooks/file-size-guard.sh"
      }
    ]
  }
}

How to use

  1. Create a hooks directory in your project: mkdir hooks
  2. Save the hook script as hooks/file-size-guard.sh
  3. Make it executable: chmod +x hooks/file-size-guard.sh
  4. Add the configuration to your Claude Code settings
  5. Restart Claude Code to apply changes