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:
- Intelligent file reference analysis
- Dry-run mode for safe analysis
- Automatic backup creation
- Detailed reporting
- Recent file protection (30-day safety)
- Configurable analysis scope
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:
move-core-models.sh- Migration script (no longer needed)move-remaining-models.sh- Migration script (no longer needed)move-state-models.sh- Migration script (no longer needed)move-utility-types.sh- Migration script (no longer needed)remove-core-models.sh- Migration script (no longer needed)update-core-model-imports.sh- Migration script (no longer needed)update-theme-imports.sh- Migration script (no longer needed)
What it keeps:
patch-ux-test-build.sh- Might still be needed for special buildsspace-images.sh- Utility for copying space imagestheme-assets.sh- Utility for copying theme assets
Safety Features:
- Creates backup in
tools/executors/backup/ - Generates README with removal details
- Timestamps all operations
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:
- Local branches older than 1 month
- Branches with '/' in their name (feature branches)
- Branches that are not published to remote
- Branches that are local-only
Safety Features:
- Shows list of branches to be deleted first
- Asks for confirmation before deletion
- Only removes local branches
- Preserves published branches
๐ ๏ธ 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
- Check for new unused files:
node tools/scripts/cleanup-unused-files.js --dry-run
Weekly
- Clean up old branches:
./tools/scripts/cleanup-branches.sh - Review unused files report
Monthly
- Full unused files cleanup:
node tools/scripts/cleanup-unused-files.js - Review and update cleanup configurations
Quarterly
- Deep cleanup and optimization
- Review all tools and scripts for necessity
- Update cleanup tool configurations
- Analyze dependencies and remove unused libraries
๐ก๏ธ Safety Features
Backup System
- Automatic Backups: All removed files are backed up automatically
- Timestamped Backups: Backups are organized by date
- Manifest Files: Each backup includes a manifest with file details
Dry Run Mode
- No Changes: Analyze without making any modifications
- Safe Testing: Test cleanup logic before actual removal
- Detailed Reporting: See exactly what would be removed
Recent File Protection
- 30-Day Protection: Files modified within 30 days are preserved
- Smart Analysis: Recently modified files are skipped automatically
- Development Safety: Protects files you're actively working on
Confirmation Prompts
- Branch Cleanup: Shows list and asks for confirmation
- File Removal: Provides detailed reports before removal
- Executor Cleanup: Creates backups before removal
๐ 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
- Reduced Build Time: Fewer files to process
- Smaller Bundle Size: Removed unused dependencies
- Faster Analysis: Cleaner codebase for static analysis
Maintainability
- Clear Structure: Easier to understand project organization
- Reduced Complexity: Fewer files to maintain
- Better Documentation: Up-to-date project structure docs
Developer Experience
- Faster Navigation: Less clutter in file explorer
- Clearer Dependencies: Removed obsolete migration scripts
- Better Onboarding: New developers see clean, current codebase
๐ Best Practices
- Always Use Dry Run First: Analyze before removing
- Review Reports: Check what would be removed
- Test After Cleanup: Ensure build system still works
- Keep Backups: Don't disable the backup system
- Document Changes: Update project documentation
- Regular Maintenance: Don't let unused files accumulate
๐ค Contributing
To improve cleanup tools:
- Add new file analysis patterns
- Improve unused file detection algorithms
- Add more safety checks
- Enhance reporting capabilities
- Add new cleanup categories
๐ License
These tools are part of the project and follow the same license terms.