Cleanup and Maintenance Tools

Tools for maintaining a clean and optimized monorepo

Cleanup and Maintenance Tools

๐Ÿงน Overview

These tools help maintain a clean, organized, and optimized monorepo by removing unused files, cleaning up deprecated scripts, and managing branches.

๐Ÿ“ Available Tools

1. cleanup-unused-files.js - Unused Files Detection

Purpose: Detect and optionally remove unused files across the project.

Usage:

# Analyze project for unused files (safe mode)
node tools/scripts/cleanup-unused-files.js --dry-run

# Verbose analysis with detailed output
node tools/scripts/cleanup-unused-files.js --dry-run --verbose

# Remove unused files (creates backup automatically)
node tools/scripts/cleanup-unused-files.js

# Remove without backup (not recommended)
node tools/scripts/cleanup-unused-files.js --no-backup

Features:

Configuration:

const config = {
  // Directories to analyze
  focusDirectories: [
    'tools/scripts',
    'tools/executors',
    'apps/*/src',
    'libs/*/src',
    'shared'
  ],
  
  // File extensions to analyze
  analyzeExtensions: ['.ts', '.js', '.mjs', '.scss', '.css', '.html'],
  
  // Patterns to exclude from analysis
  excludePatterns: [
    'node_modules/**',
    'dist/**',
    '.nx/**',
    '**/*.spec.ts',
    '**/*.test.ts'
  ]
};

2. cleanup-executors.js - Executor Cleanup

Purpose: Remove deprecated migration scripts from the executors directory.

Usage:

# Clean up deprecated executors
node tools/executors/cleanup-executors.js

What it removes:

What it keeps:

Safety Features:

3. cleanup-branches.sh - Branch Cleanup

Purpose: Clean up old local branches that are no longer needed.

Usage:

# Clean up old branches
./tools/scripts/cleanup-branches.sh

What it removes:

Safety Features:

๐Ÿ› ๏ธ Maintenance Commands

Unused Files Analysis

# Safe analysis without changes
node tools/scripts/cleanup-unused-files.js --dry-run

# Detailed analysis
node tools/scripts/cleanup-unused-files.js --dry-run --verbose

# Remove unused files with backup
node tools/scripts/cleanup-unused-files.js

Executor Cleanup

# Clean deprecated executors
cd tools/executors
node cleanup-executors.js

Branch Cleanup

# Clean old branches
./tools/scripts/cleanup-branches.sh

๐Ÿ“Š Cleanup Results and Reports

Unused Files Report

{
  "timestamp": "2025-01-15T10:30:00.000Z",
  "config": {
    "focusDirectories": ["tools/scripts", "libs/*/src"],
    "analyzeExtensions": [".ts", ".js", ".scss"],
    "excludePatterns": ["node_modules/**", "dist/**"]
  },
  "results": {
    "totalFiles": 37,
    "analyzedFiles": 22,
    "unusedFiles": 0,
    "recentFiles": 5,
    "errors": 0
  }
}

Backup Manifest

{
  "date": "2025-01-15T10:30:00.000Z",
  "files": ["file1.js", "file2.ts"],
  "totalFiles": 2,
  "reason": "Unused file cleanup"
}

๐Ÿ”ง Configuration and Customization

Unused Files Configuration

// Modify tools/scripts/cleanup-unused-files.js
const config = {
  focusDirectories: [
    'tools/scripts',      // Scripts directory
    'tools/executors',    // Executors directory
    'apps/*/src',         // All app source directories
    'libs/*/src',         // All library source directories
    'shared'              // Shared directory
  ],
  
  analyzeExtensions: [
    '.ts', '.js', '.mjs', 
    '.scss', '.css', '.html'
  ],
  
  excludePatterns: [
    'node_modules/**',
    'dist/**',
    '.nx/**',
    '**/*.spec.ts',
    '**/*.test.ts',
    '**/*.mock.ts'
  ],
  
  // Files modified within 30 days are protected
  recentFileProtectionDays: 30
};

Branch Cleanup Configuration

# Modify cleanup-branches.sh
# Change age threshold (currently 1 month)
AGE_THRESHOLD="1 month"

# Change branch name pattern (currently requires '/')
BRANCH_PATTERN="*/*"

๐Ÿš€ Quick Start Examples

Monthly Cleanup Routine

# 1. Analyze unused files
node tools/scripts/cleanup-unused-files.js --dry-run

# 2. Remove unused files if safe
node tools/scripts/cleanup-unused-files.js

# 3. Clean old branches
./tools/scripts/cleanup-branches.sh

# 4. Clean deprecated executors (one-time)
cd tools/executors && node cleanup-executors.js

Pre-Release Cleanup

# Clean everything before release
node tools/scripts/cleanup-unused-files.js --dry-run --verbose
node tools/scripts/cleanup-unused-files.js
./tools/scripts/cleanup-branches.sh

Development Cleanup

# Quick cleanup during development
node tools/scripts/cleanup-unused-files.js --dry-run
./tools/scripts/cleanup-branches.sh

๐Ÿ”„ Maintenance Schedule

Daily

Weekly

Monthly

Quarterly

๐Ÿ›ก๏ธ Safety Features

Backup System

Dry Run Mode

Recent File Protection

Confirmation Prompts

๐Ÿ› Troubleshooting

Unused Files Script Issues

# Check if script is executable
chmod +x tools/scripts/cleanup-unused-files.js

# Run with verbose output
node tools/scripts/cleanup-unused-files.js --dry-run --verbose

# Check backup directory
ls -la tools/scripts/backup/

Branch Cleanup Issues

# Check script permissions
chmod +x tools/scripts/cleanup-branches.sh

# Run with bash explicitly
bash tools/scripts/cleanup-branches.sh

# Check git status first
git status

Executor Cleanup Issues

# Check if backup was created
ls -la tools/executors/backup/

# Restore from backup if needed
cp tools/executors/backup/script.sh tools/executors/

๐Ÿ“ˆ Impact and Benefits

Performance Improvements

Maintainability

Developer Experience

๐Ÿ“ Best Practices

  1. Always Use Dry Run First: Analyze before removing
  2. Review Reports: Check what would be removed
  3. Test After Cleanup: Ensure build system still works
  4. Keep Backups: Don't disable the backup system
  5. Document Changes: Update project documentation
  6. Regular Maintenance: Don't let unused files accumulate

๐Ÿค Contributing

To improve cleanup tools:

  1. Add new file analysis patterns
  2. Improve unused file detection algorithms
  3. Add more safety checks
  4. Enhance reporting capabilities
  5. Add new cleanup categories

๐Ÿ“„ License

These tools are part of the project and follow the same license terms.