|
| 1 | +import path from "node:path"; |
| 2 | +import fs from "node:fs/promises"; |
| 3 | +import {DatabaseSync} from "node:sqlite"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Get the size of a directory tree recursively. |
| 7 | + * |
| 8 | + * @param {string} dirPath Absolute path to directory |
| 9 | + * @returns {Promise<number>} Total size in bytes |
| 10 | + */ |
| 11 | +async function getDirectorySize(dirPath) { |
| 12 | + let total = 0; |
| 13 | + let entries; |
| 14 | + try { |
| 15 | + entries = await fs.readdir(dirPath, {withFileTypes: true}); |
| 16 | + } catch { |
| 17 | + return 0; |
| 18 | + } |
| 19 | + for (const entry of entries) { |
| 20 | + const entryPath = path.join(dirPath, entry.name); |
| 21 | + if (entry.isDirectory()) { |
| 22 | + total += await getDirectorySize(entryPath); |
| 23 | + } else { |
| 24 | + try { |
| 25 | + const stat = await fs.stat(entryPath); |
| 26 | + total += stat.size; |
| 27 | + } catch { |
| 28 | + // Skip inaccessible files |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + return total; |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Clean a single directory by removing it entirely. |
| 37 | + * |
| 38 | + * @param {string} dirPath Absolute path to directory |
| 39 | + * @param {string} displayPath Path to display in results |
| 40 | + * @param {string} type Type of cache entry |
| 41 | + * @returns {Promise<Array<{path: string, type: string, size: number}>>} Removed entries |
| 42 | + */ |
| 43 | +async function cleanDirectory(dirPath, displayPath, type) { |
| 44 | + const removed = []; |
| 45 | + try { |
| 46 | + await fs.access(dirPath); |
| 47 | + } catch { |
| 48 | + return removed; |
| 49 | + } |
| 50 | + |
| 51 | + const size = await getDirectorySize(dirPath); |
| 52 | + try { |
| 53 | + await fs.rm(dirPath, {recursive: true, force: true}); |
| 54 | + removed.push({path: displayPath, type, size}); |
| 55 | + } catch { |
| 56 | + // Skip on failure |
| 57 | + } |
| 58 | + return removed; |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Clean build cache directory by clearing all records from the SQLite database. |
| 63 | + * |
| 64 | + * @param {string} buildCacheDir Path to buildCache/ |
| 65 | + * @returns {Promise<Array<{path: string, type: string, size: number}>>} Removed entries |
| 66 | + */ |
| 67 | +async function cleanBuildCache(buildCacheDir) { |
| 68 | + const removed = []; |
| 69 | + try { |
| 70 | + await fs.access(buildCacheDir); |
| 71 | + } catch { |
| 72 | + return removed; |
| 73 | + } |
| 74 | + |
| 75 | + let versionDirs; |
| 76 | + try { |
| 77 | + versionDirs = await fs.readdir(buildCacheDir, {withFileTypes: true}); |
| 78 | + } catch { |
| 79 | + return removed; |
| 80 | + } |
| 81 | + |
| 82 | + const tables = ["content", "index_cache", "stage_metadata", "task_metadata", "result_metadata"]; |
| 83 | + |
| 84 | + for (const versionDir of versionDirs) { |
| 85 | + if (!versionDir.isDirectory()) { |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
| 89 | + const dbPath = path.join(buildCacheDir, versionDir.name, "cache.db"); |
| 90 | + try { |
| 91 | + await fs.access(dbPath); |
| 92 | + } catch { |
| 93 | + continue; |
| 94 | + } |
| 95 | + |
| 96 | + const statBefore = await fs.stat(dbPath); |
| 97 | + const sizeBefore = statBefore.size; |
| 98 | + |
| 99 | + const db = new DatabaseSync(dbPath); |
| 100 | + db.exec("BEGIN"); |
| 101 | + for (const table of tables) { |
| 102 | + db.exec(`DELETE FROM ${table}`); |
| 103 | + } |
| 104 | + db.exec("COMMIT"); |
| 105 | + db.exec("VACUUM"); |
| 106 | + db.close(); |
| 107 | + |
| 108 | + const statAfter = await fs.stat(dbPath); |
| 109 | + const freedSize = sizeBefore - statAfter.size; |
| 110 | + |
| 111 | + removed.push({ |
| 112 | + path: `buildCache/${versionDir.name}`, |
| 113 | + type: "buildCache", |
| 114 | + size: freedSize, |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + return removed; |
| 119 | +} |
| 120 | + |
| 121 | +/** |
| 122 | + * Scans the UI5 data directory and removes all cache entries. |
| 123 | + * |
| 124 | + * @param {object} options |
| 125 | + * @param {string} options.ui5DataDir Resolved absolute path to UI5 data directory |
| 126 | + * @returns {Promise<{entries: Array<{path: string, type: string, size: number}>, |
| 127 | + * totalSize: number, totalCount: number}>} |
| 128 | + */ |
| 129 | +export async function cleanCache({ui5DataDir}) { |
| 130 | + const allRemoved = []; |
| 131 | + |
| 132 | + // Clean framework packages |
| 133 | + allRemoved.push(...await cleanDirectory( |
| 134 | + path.join(ui5DataDir, "framework", "packages"), |
| 135 | + "framework/packages", |
| 136 | + "framework" |
| 137 | + )); |
| 138 | + |
| 139 | + // Clean cacache |
| 140 | + allRemoved.push(...await cleanDirectory( |
| 141 | + path.join(ui5DataDir, "framework", "cacache"), |
| 142 | + "framework/cacache", |
| 143 | + "cacache" |
| 144 | + )); |
| 145 | + |
| 146 | + // Clean build cache (special: clears DB records, not files) |
| 147 | + allRemoved.push(...await cleanBuildCache(path.join(ui5DataDir, "buildCache"))); |
| 148 | + |
| 149 | + // Clean misc dirs |
| 150 | + const miscDirs = [ |
| 151 | + ["framework/staging", "staging"], |
| 152 | + ["framework/locks", "locks"], |
| 153 | + ["server", "server"], |
| 154 | + ]; |
| 155 | + for (const [rel, type] of miscDirs) { |
| 156 | + allRemoved.push(...await cleanDirectory(path.join(ui5DataDir, rel), rel, type)); |
| 157 | + } |
| 158 | + |
| 159 | + const totalSize = allRemoved.reduce((sum, entry) => sum + entry.size, 0); |
| 160 | + return { |
| 161 | + entries: allRemoved, |
| 162 | + totalSize, |
| 163 | + totalCount: allRemoved.length, |
| 164 | + }; |
| 165 | +} |
0 commit comments