Skip to content

Instantly share code, notes, and snippets.

@reedom
Created September 25, 2025 09:55
Show Gist options
  • Select an option

  • Save reedom/22274aa5aed6ec4aa4ef299a57aa60f3 to your computer and use it in GitHub Desktop.

Select an option

Save reedom/22274aa5aed6ec4aa4ef299a57aa60f3 to your computer and use it in GitHub Desktop.
Fix EOF newlines for existing files
#!/bin/bash
# Fix EOF newlines for existing files
# Usage: ./scripts/fix-eof-newlines.sh [directory]
set -e
# Default to current directory if no argument provided
TARGET_DIR="${1:-.}"
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo "πŸ” Checking files for missing EOF newlines in: $TARGET_DIR"
# Counter for statistics
total_checked=0
total_fixed=0
total_skipped=0
# Find all files, excluding common binary/generated files and directories
files_to_check=$(find "$TARGET_DIR" -type f \
! -path "*/node_modules/*" \
! -path "*/.git/*" \
! -path "*/vendor/*" \
! -path "*/dist/*" \
! -path "*/.next/*" \
! -path "*/__pycache__/*" \
! -path "*/target/*" \
! -name "*.png" \
! -name "*.jpg" \
! -name "*.jpeg" \
! -name "*.gif" \
! -name "*.ico" \
! -name "*.pdf" \
! -name "*.zip" \
! -name "*.tar.gz" \
! -name "*.exe" \
! -name "*.dll" \
! -name "*.so" \
! -name "*.woff" \
! -name "*.woff2" \
! -name "*.ttf" \
! -name "*.eot" \
! -name "*.min.js" \
! -name "*.min.css" \
! -name "package-lock.json" \
! -name "yarn.lock" \
! -name "Pipfile.lock" \
! -name "poetry.lock")
while IFS= read -r file; do
# Skip empty result
if [ -z "$file" ]; then
continue
fi
# Skip if file doesn't exist or is empty
if [ ! -f "$file" ] || [ ! -s "$file" ]; then
continue
fi
# Skip large files (>5MB)
file_size=$(stat -c%s "$file" 2>/dev/null || stat -f%z "$file" 2>/dev/null || echo 0)
if [ "$file_size" -gt 5242880 ]; then
echo -e "${YELLOW}⏭️ Skipping large file: $file${NC}"
((total_skipped++))
continue
fi
# Skip binary files using file command
if file "$file" | grep -q "binary\|executable\|archive"; then
echo -e "${YELLOW}⏭️ Skipping binary file: $file${NC}"
((total_skipped++))
continue
fi
((total_checked++))
# Check if file ends with newline
if [ "$(tail -c1 "$file" | wc -l)" -eq 0 ]; then
echo -e "${RED}πŸ”§ Fixing: $file${NC}"
# Add newline to the end of file
echo "" >> "$file"
((total_fixed++))
else
echo -e "${GREEN}βœ… OK: $file${NC}"
fi
done <<< "$files_to_check"
echo ""
echo "πŸ“Š Summary:"
echo " Files checked: $total_checked"
echo " Files fixed: $total_fixed"
echo " Files skipped: $total_skipped"
if [ "$total_fixed" -gt 0 ]; then
echo ""
echo -e "${GREEN}πŸŽ‰ Fixed $total_fixed files with missing EOF newlines!${NC}"
echo "πŸ’‘ You can now commit these changes."
else
echo ""
echo -e "${GREEN}✨ All files already have proper EOF newlines!${NC}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment