|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | + |
| 4 | +/** |
| 5 | + * Cross-platform script to delete temporary and cache directories. Run this if Platform.Bible is |
| 6 | + * holding on to outdated resources, such as localization strings, project data, or WebView ids. |
| 7 | + * |
| 8 | + * Warning: The `--core` flag deletes: |
| 9 | + * |
| 10 | + * - The `Electron` cache folder, which affect any other Electron apps |
| 11 | + * - `paranext-core/dev-appdata/`, which includes `installed-extensions/` |
| 12 | + */ |
| 13 | + |
| 14 | +// Define directory lists |
| 15 | + |
| 16 | +/* eslint-disable no-nested-ternary */ |
| 17 | +let electronParent = |
| 18 | + process.platform === 'win32' |
| 19 | + ? process.env.APPDATA |
| 20 | + : process.platform === 'linux' |
| 21 | + ? process.env.XDG_CONFIG_HOME |
| 22 | + : ''; |
| 23 | +if (!electronParent && process.env.HOME) { |
| 24 | + electronParent = |
| 25 | + process.platform === 'linux' |
| 26 | + ? path.join(process.env.HOME, '.config') |
| 27 | + : process.platform === 'darwin' |
| 28 | + ? path.join(process.env.HOME, 'Library', 'Application Support') |
| 29 | + : ''; |
| 30 | +} |
| 31 | +/* eslint-enable no-nested-ternary */ |
| 32 | + |
| 33 | +const CORE_DIRS = [ |
| 34 | + electronParent ? path.join(electronParent, 'Electron') : '', |
| 35 | + path.join(__dirname, '..', '..', 'paranext-core', 'dev-appdata'), |
| 36 | +]; |
| 37 | + |
| 38 | +const EXT_DIRS = [ |
| 39 | + path.join(__dirname, '..', 'coverage'), |
| 40 | + path.join(__dirname, '..', 'dist'), |
| 41 | + path.join(__dirname, '..', 'src', 'temp-build'), |
| 42 | +]; |
| 43 | + |
| 44 | +/** |
| 45 | + * Delete a directory if it exists |
| 46 | + * |
| 47 | + * @param {string} dirPath - The directory path to delete |
| 48 | + */ |
| 49 | +function deleteDirectory(dirPath) { |
| 50 | + if (!dirPath) { |
| 51 | + console.log('⊘ Skipping empty path'); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + try { |
| 56 | + if (fs.existsSync(dirPath)) { |
| 57 | + fs.rmSync(dirPath, { recursive: true, force: true }); |
| 58 | + console.log(`✓ Deleted ${dirPath}`); |
| 59 | + } else { |
| 60 | + console.log(`⊘ ${dirPath} does not exist`); |
| 61 | + } |
| 62 | + } catch (error) { |
| 63 | + console.error(`✗ Failed to delete ${dirPath}:`, error.message); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// Delete directories based on command-line flags |
| 68 | + |
| 69 | +try { |
| 70 | + const hasCoreFlag = process.argv.includes('--core'); |
| 71 | + const hasExtFlag = process.argv.includes('--ext'); |
| 72 | + |
| 73 | + if (!hasCoreFlag && !hasExtFlag) { |
| 74 | + console.error('Usage: node delete-temp-dirs.cjs [--core] [--ext]'); |
| 75 | + console.error(' --core Delete AppData and core cache directories (Electron, dev-appdata)'); |
| 76 | + console.error(' --ext Delete extension directories (coverage, dist, src/temp-build)'); |
| 77 | + process.exit(1); |
| 78 | + } |
| 79 | + |
| 80 | + if (hasCoreFlag) { |
| 81 | + console.log('Deleting core cache directories...'); |
| 82 | + CORE_DIRS.forEach(deleteDirectory); |
| 83 | + } |
| 84 | + |
| 85 | + if (hasExtFlag) { |
| 86 | + console.log('Deleting extension directories...'); |
| 87 | + EXT_DIRS.forEach(deleteDirectory); |
| 88 | + } |
| 89 | + |
| 90 | + console.log('Complete!'); |
| 91 | + process.exit(0); |
| 92 | +} catch (error) { |
| 93 | + console.error('Error during cleanup:', error); |
| 94 | + process.exit(1); |
| 95 | +} |
0 commit comments