|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Cross-platform script to delete stale files listed in .stale_files |
| 5 | + * Works on Windows, macOS, and Linux |
| 6 | + * |
| 7 | + * This script is designed to run during npm preinstall lifecycle |
| 8 | + * to clean up obsolete files before dependencies are installed. |
| 9 | + */ |
| 10 | + |
| 11 | +const fs = require('fs'); |
| 12 | +const path = require('path'); |
| 13 | + |
| 14 | +// Configuration |
| 15 | +const STALE_FILES_NAME = 'stale_files'; |
| 16 | + |
| 17 | +// Determine the root directory (project root) |
| 18 | +const rootDir = path.resolve(__dirname, '..'); |
| 19 | +const staleFilesPath = path.join(rootDir, STALE_FILES_NAME); |
| 20 | + |
| 21 | +/** |
| 22 | + * Validates if a path is within the project root directory |
| 23 | + * @param {string} targetPath - Absolute path to validate |
| 24 | + * @param {string} rootPath - Root directory path |
| 25 | + * @returns {boolean} - True if path is within root, false otherwise |
| 26 | + */ |
| 27 | +function isPathWithinRoot(targetPath, rootPath) { |
| 28 | + const relative = path.relative(rootPath, targetPath); |
| 29 | + return !relative.startsWith('..') && !path.isAbsolute(relative); |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Safely delete a file or directory |
| 34 | + * @param {string} filePath - Absolute path to the file/directory |
| 35 | + * @param {string} rootPath - Root directory path for validation |
| 36 | + * @returns {boolean} - True if deleted successfully, false otherwise |
| 37 | + */ |
| 38 | +function deleteFileOrDirectory(filePath, rootPath) { |
| 39 | + try { |
| 40 | + // Validate path is within project root (security check) |
| 41 | + if (!isPathWithinRoot(filePath, rootPath)) { |
| 42 | + console.log(`⊘ Skipped (outside project): ${filePath}`); |
| 43 | + return false; |
| 44 | + } |
| 45 | + |
| 46 | + // Check if file/directory exists |
| 47 | + if (!fs.existsSync(filePath)) { |
| 48 | + console.log(`⊘ Not found (skipped): ${filePath}`); |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + const stats = fs.statSync(filePath); |
| 53 | + |
| 54 | + if (stats.isDirectory()) { |
| 55 | + // Remove directory recursively |
| 56 | + fs.rmSync(filePath, { recursive: true, force: true }); |
| 57 | + console.log(`✓ Deleted directory: ${filePath}`); |
| 58 | + } else { |
| 59 | + // Remove file |
| 60 | + fs.unlinkSync(filePath); |
| 61 | + console.log(`✓ Deleted file: ${filePath}`); |
| 62 | + } |
| 63 | + return true; |
| 64 | + } catch (error) { |
| 65 | + // Catch any errors but continue without throwing |
| 66 | + console.log(`⊘ Could not delete (skipped): ${filePath} - ${error.message}`); |
| 67 | + return false; |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * Parse and validate file paths from .stale_files content |
| 73 | + * @param {string} content - Raw content from .stale_files |
| 74 | + * @returns {string[]} - Array of valid file paths |
| 75 | + */ |
| 76 | +function parseStaleFiles(content) { |
| 77 | + return content |
| 78 | + .split(/\r?\n/) // Handle both Unix and Windows line endings |
| 79 | + .map(line => line.trim()) |
| 80 | + .filter(line => line.length > 0); // Filter out empty lines |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Main function to process and delete stale files |
| 85 | + * @returns {void} |
| 86 | + */ |
| 87 | +function deleteStaleFiles() { |
| 88 | + console.log('🧹 Starting stale files cleanup...\n'); |
| 89 | + |
| 90 | + // Verify we're running in a valid project root |
| 91 | + if (!fs.existsSync(rootDir)) { |
| 92 | + console.warn(`⚠ Warning: Project root directory not found: ${rootDir}`); |
| 93 | + console.log('Skipping stale files cleanup.\n'); |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + // Check if .stale_files exists |
| 98 | + if (!fs.existsSync(staleFilesPath)) { |
| 99 | + console.log(`⚠ No ${STALE_FILES_NAME} found. Nothing to clean up.`); |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + let content; |
| 104 | + try { |
| 105 | + // Read the .stale_files content |
| 106 | + content = fs.readFileSync(staleFilesPath, 'utf8'); |
| 107 | + } catch (error) { |
| 108 | + console.warn(`⚠ Warning: Could not read ${STALE_FILES_NAME}:`, error.message); |
| 109 | + console.log('Skipping stale files cleanup.\n'); |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + const files = parseStaleFiles(content); |
| 114 | + |
| 115 | + if (files.length === 0) { |
| 116 | + console.log(`⚠ No files listed in ${STALE_FILES_NAME}. Nothing to clean up.`); |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + console.log(`Found ${files.length} file(s)/directory(ies) to process:\n`); |
| 121 | + |
| 122 | + let deletedCount = 0; |
| 123 | + let skippedCount = 0; |
| 124 | + |
| 125 | + // Process each file |
| 126 | + files.forEach(file => { |
| 127 | + const absolutePath = path.join(rootDir, file); |
| 128 | + const deleted = deleteFileOrDirectory(absolutePath, rootDir); |
| 129 | + |
| 130 | + if (deleted) { |
| 131 | + deletedCount++; |
| 132 | + } else { |
| 133 | + skippedCount++; |
| 134 | + } |
| 135 | + }); |
| 136 | + |
| 137 | + // Summary |
| 138 | + console.log('\n=================================================='); |
| 139 | + console.log('📊 Cleanup Summary:'); |
| 140 | + console.log(` ✓ Deleted: ${deletedCount}`); |
| 141 | + console.log(` ⊘ Skipped: ${skippedCount}`); |
| 142 | + console.log('==================================================\n'); |
| 143 | +} |
| 144 | + |
| 145 | +// Main execution |
| 146 | +if (require.main === module) { |
| 147 | + deleteStaleFiles(); |
| 148 | +} |
0 commit comments