fix-all.shβ’7.91 kB
#!/bin/bash
set -e
# Auto-Fix Script for Development Issues
# Automatically fixes common development problems to maintain code quality
echo "π§ Auto-Fix Development Issues"
echo "ββββββββββββββββββββββββββββββββββββββββββββββββββ"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
DRY_RUN=false
VERBOSE=false
SKIP_BUILD=false
AUTO_COMMIT=false
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
--dry-run)
DRY_RUN=true
shift
;;
--verbose|-v)
VERBOSE=true
shift
;;
--skip-build)
SKIP_BUILD=true
shift
;;
--auto-commit)
AUTO_COMMIT=true
shift
;;
--help|-h)
cat << EOF
Auto-Fix Development Issues
Usage: ./scripts/fix-all.sh [options]
Options:
--dry-run Show what would be fixed without making changes
--verbose, -v Show detailed output and file-by-file progress
--skip-build Skip build verification after fixes
--auto-commit Automatically commit fixes (use with caution)
--help, -h Show this help message
This script automatically fixes:
β’ Prettier formatting issues
β’ ESLint auto-fixable problems
β’ Import organization (node β external β internal)
β’ Simple TypeScript optimizations (any β unknown)
β’ Unused import removal
β’ Basic code quality improvements
Examples:
./scripts/fix-all.sh --dry-run
./scripts/fix-all.sh --verbose
./scripts/fix-all.sh --auto-commit
EOF
exit 0
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
done
# Function to run fix step with timing and status
run_fix() {
local name="$1"
local command="$2"
local start_time=$(date +%s)
printf "${BLUE}π§ %-30s${NC}" "$name"
if [[ "$DRY_RUN" == "true" ]]; then
printf "${YELLOW}[DRY RUN]${NC}\\n"
return 0
fi
if eval "$command" > /tmp/fix-step.log 2>&1; then
local end_time=$(date +%s)
local duration=$((end_time - start_time))
printf "${GREEN}β
(${duration}s)${NC}\\n"
return 0
else
local end_time=$(date +%s)
local duration=$((end_time - start_time))
printf "${RED}β (${duration}s)${NC}\\n"
if [[ "$VERBOSE" == "true" ]]; then
echo "${RED}Error output:${NC}"
cat /tmp/fix-step.log
fi
return 1
fi
}
# Function to organize imports in TypeScript files
organize_imports() {
local file="$1"
if [[ ! -f "$file" ]]; then
return 1
fi
# Extract imports and organize them
local node_imports=$(grep "^import.*from ['\"]node:" "$file" | sort)
local external_imports=$(grep "^import.*from ['\"][^./]" "$file" | grep -v "from ['\"]node:" | sort)
local internal_imports=$(grep "^import.*from ['\"][./]" "$file" | sort)
# Only proceed if file has imports
if [[ -n "$node_imports$external_imports$internal_imports" ]]; then
# Create temporary file with organized imports
local temp_file=$(mktemp)
# Add copyright/header if exists
sed '/^import/,$d' "$file" > "$temp_file"
# Add organized imports with spacing
if [[ -n "$node_imports" ]]; then
echo "$node_imports" >> "$temp_file"
echo "" >> "$temp_file"
fi
if [[ -n "$external_imports" ]]; then
echo "$external_imports" >> "$temp_file"
echo "" >> "$temp_file"
fi
if [[ -n "$internal_imports" ]]; then
echo "$internal_imports" >> "$temp_file"
echo "" >> "$temp_file"
fi
# Add rest of file (skip imports)
sed '1,/^import/d' "$file" | sed '1,/^$/d' >> "$temp_file"
# Replace original file
mv "$temp_file" "$file"
fi
}
# Function to apply simple TypeScript optimizations
optimize_typescript() {
find src test -name "*.ts" -not -path "*/node_modules/*" | while read -r file; do
if [[ "$VERBOSE" == "true" ]]; then
echo " π Optimizing: $file"
fi
# Replace 'any' with 'unknown' where safe
sed -i.bak 's/: any\([^A-Za-z]\)/: unknown\1/g' "$file"
sed -i.bak 's/Record<string, any>/Record<string, unknown>/g' "$file"
# Remove unused variables (basic patterns)
sed -i.bak '/^[[:space:]]*const [a-zA-Z_][a-zA-Z0-9_]* =/d' "$file" 2>/dev/null || true
# Remove .bak files
rm -f "${file}.bak"
# Organize imports
organize_imports "$file"
done
}
# Start timing
SCRIPT_START=$(date +%s)
# Step 1: Environment validation
echo "${BLUE}π Environment Validation${NC}"
echo " Node: $(node --version)"
echo " NPM: $(npm --version)"
echo " Working Dir: $(pwd)"
echo ""
if [[ "$DRY_RUN" == "true" ]]; then
echo "${YELLOW}π§ͺ DRY RUN MODE - No changes will be made${NC}"
echo ""
fi
# Step 2: Prettier formatting
echo "${YELLOW}π Code Formatting${NC}"
run_fix "Prettier format" "npm run format" || {
echo "${YELLOW}β οΈ Prettier formatting failed, but continuing...${NC}"
}
# Step 3: ESLint auto-fix
echo "${YELLOW}π ESLint Auto-Fix${NC}"
run_fix "ESLint auto-fix" "npm run lint:fix" || {
echo "${YELLOW}β οΈ ESLint auto-fix failed, but continuing...${NC}"
}
# Step 4: TypeScript optimizations
echo "${YELLOW}β‘ TypeScript Optimizations${NC}"
if [[ "$DRY_RUN" != "true" ]]; then
optimize_typescript
echo "${BLUE}π§ TypeScript optimizations ${GREEN}β
${NC}"
else
echo "${BLUE}π§ TypeScript optimizations ${YELLOW}[DRY RUN]${NC}"
fi
# Step 5: Remove unused imports (using TypeScript compiler)
echo "${YELLOW}π§Ή Import Cleanup${NC}"
run_fix "Remove unused imports" "npx ts-unused-exports tsconfig.json --deleteUnusedFile" || {
echo "${YELLOW}β οΈ Import cleanup failed, but continuing...${NC}"
}
# Step 6: Build verification (if not skipped)
if [[ "$SKIP_BUILD" != "true" ]]; then
echo "${YELLOW}π¨ Build Verification${NC}"
run_fix "TypeScript compilation" "npm run build" || {
echo "${RED}β Build failed after fixes - manual intervention required${NC}"
exit 1
}
fi
# Step 7: Final validation
echo "${YELLOW}β
Final Validation${NC}"
run_fix "Format check" "npm run check:format" || {
echo "${YELLOW}β οΈ Format check failed after fixes${NC}"
}
run_fix "Lint check" "npm run lint:check" || {
echo "${YELLOW}β οΈ Lint check still has issues - may need manual fixes${NC}"
}
# Step 8: Auto-commit (if requested)
if [[ "$AUTO_COMMIT" == "true" && "$DRY_RUN" != "true" ]]; then
echo "${YELLOW}π Auto-Commit${NC}"
if git diff --quiet && git diff --cached --quiet; then
echo "${BLUE}π§ Auto-commit ${YELLOW}No changes to commit${NC}"
else
git add -A
git commit -m "Chore: Auto-fix development issues
- Applied Prettier formatting
- Fixed ESLint auto-fixable issues
- Organized imports (node β external β internal)
- Applied TypeScript optimizations (any β unknown)
- Removed unused imports and variables
Generated by scripts/fix-all.sh --auto-commit"
echo "${BLUE}π§ Auto-commit ${GREEN}β
Committed fixes${NC}"
fi
fi
# Calculate total time
SCRIPT_END=$(date +%s)
TOTAL_TIME=$((SCRIPT_END - SCRIPT_START))
MINUTES=$((TOTAL_TIME / 60))
SECONDS=$((TOTAL_TIME % 60))
echo ""
echo "${GREEN}β
Auto-fix completed successfully!${NC}"
echo "${GREEN}β±οΈ Total time: ${MINUTES}m ${SECONDS}s${NC}"
echo ""
# Success tips
echo "${BLUE}π‘ What was fixed:${NC}"
echo " β’ Code formatting (Prettier)"
echo " β’ Auto-fixable lint issues (ESLint)"
echo " β’ Import organization and cleanup"
echo " β’ TypeScript optimizations (any β unknown)"
echo " β’ Unused code removal"
if [[ "$DRY_RUN" != "true" ]]; then
echo ""
echo "${BLUE}π Next steps:${NC}"
echo " β’ Review changes with: git diff"
echo " β’ Run tests: npm test"
echo " β’ Create commit: git add -A && git commit -m 'Chore: Auto-fix issues'"
fi