|
1 | 1 | import path from "node:path"; |
2 | 2 | import fs from "node:fs/promises"; |
3 | 3 | import BuildCacheStorage from "../build/cache/BuildCacheStorage.js"; |
| 4 | +import {CACHE_VERSION} from "../build/cache/CacheManager.js"; |
4 | 5 |
|
5 | 6 | // ======================================== |
6 | 7 | // SHARED UTILITIES |
@@ -99,89 +100,75 @@ async function cleanFrameworkCache(ui5DataDir, frameworkInfo) { |
99 | 100 |
|
100 | 101 | /** |
101 | 102 | * Check if build cache exists and get its info. |
| 103 | + * Only checks the current known cache version to avoid processing unknown future versions. |
102 | 104 | * |
103 | 105 | * @param {string} ui5DataDir Resolved absolute path to UI5 data directory |
104 | 106 | * @returns {Promise<{path: string, size: number, type: string}|null>} Build cache info or null |
105 | 107 | */ |
106 | 108 | async function getBuildCacheInfo(ui5DataDir) { |
107 | 109 | const buildCacheDir = path.join(ui5DataDir, "buildCache"); |
108 | | - try { |
109 | | - await fs.access(buildCacheDir); |
110 | | - const versionDirs = await fs.readdir(buildCacheDir, {withFileTypes: true}); |
| 110 | + const dbDir = path.join(buildCacheDir, CACHE_VERSION); |
111 | 111 |
|
112 | | - let hasAnyRecords = false; |
113 | | - for (const versionDir of versionDirs) { |
114 | | - if (!versionDir.isDirectory()) { |
115 | | - continue; |
116 | | - } |
| 112 | + try { |
| 113 | + await fs.access(dbDir); |
| 114 | + } catch { |
| 115 | + // Current version directory doesn't exist |
| 116 | + return null; |
| 117 | + } |
117 | 118 |
|
118 | | - const dbDir = path.join(buildCacheDir, versionDir.name); |
119 | | - try { |
120 | | - const storage = new BuildCacheStorage(dbDir); |
121 | | - if (storage.hasRecords()) { |
122 | | - hasAnyRecords = true; |
123 | | - storage.close(); |
124 | | - break; |
125 | | - } |
126 | | - storage.close(); |
127 | | - } catch { |
128 | | - // Skip if database can't be opened |
| 119 | + try { |
| 120 | + const storage = new BuildCacheStorage(dbDir); |
| 121 | + try { |
| 122 | + if (storage.hasRecords()) { |
| 123 | + const size = await getDirectorySize(buildCacheDir); |
| 124 | + return { |
| 125 | + path: `buildCache/${CACHE_VERSION} (database records)`, |
| 126 | + size, |
| 127 | + type: "database" |
| 128 | + }; |
129 | 129 | } |
130 | | - } |
131 | | - |
132 | | - if (hasAnyRecords) { |
133 | | - const size = await getDirectorySize(buildCacheDir); |
134 | | - return { |
135 | | - path: "buildCache/ (database records)", |
136 | | - size, |
137 | | - type: "database" |
138 | | - }; |
| 130 | + } finally { |
| 131 | + storage.close(); |
139 | 132 | } |
140 | 133 | } catch { |
141 | | - // Directory doesn't exist |
| 134 | + // Skip if database can't be opened |
142 | 135 | } |
143 | 136 | return null; |
144 | 137 | } |
145 | 138 |
|
146 | 139 | /** |
147 | | - * Clean build cache by clearing all records from SQLite databases. |
| 140 | + * Clean build cache by clearing all records from SQLite database. |
| 141 | + * Only cleans the current known cache version to avoid processing unknown future versions. |
148 | 142 | * |
149 | 143 | * @param {string} ui5DataDir Resolved absolute path to UI5 data directory |
150 | 144 | * @returns {Promise<Array<{path: string, type: string, size: number}>>} Removed entries |
151 | 145 | */ |
152 | 146 | async function cleanBuildCache(ui5DataDir) { |
153 | 147 | const buildCacheDir = path.join(ui5DataDir, "buildCache"); |
| 148 | + const dbDir = path.join(buildCacheDir, CACHE_VERSION); |
154 | 149 | const removed = []; |
155 | 150 |
|
156 | 151 | try { |
157 | | - await fs.access(buildCacheDir); |
| 152 | + await fs.access(dbDir); |
158 | 153 | } catch { |
| 154 | + // Current version directory doesn't exist |
159 | 155 | return removed; |
160 | 156 | } |
161 | 157 |
|
162 | | - let cacheVersionDirs; |
163 | 158 | try { |
164 | | - cacheVersionDirs = await fs.readdir(buildCacheDir, {withFileTypes: true}); |
165 | | - } catch { |
166 | | - return removed; |
167 | | - } |
168 | | - |
169 | | - for (const versionDir of cacheVersionDirs) { |
170 | | - if (!versionDir.isDirectory()) { |
171 | | - continue; |
172 | | - } |
173 | | - |
174 | | - const dbDir = path.join(buildCacheDir, versionDir.name); |
175 | | - |
176 | 159 | const storage = new BuildCacheStorage(dbDir); |
177 | | - const freedSize = storage.clearAllRecords(); |
178 | | - storage.close(); |
179 | | - |
180 | | - removed.push({ |
181 | | - path: `buildCache/${versionDir.name}`, |
182 | | - type: "buildCache", |
183 | | - size: freedSize, |
184 | | - }); |
| 160 | + try { |
| 161 | + const freedSize = storage.clearAllRecords(); |
| 162 | + removed.push({ |
| 163 | + path: `buildCache/${CACHE_VERSION}`, |
| 164 | + type: "buildCache", |
| 165 | + size: freedSize, |
| 166 | + }); |
| 167 | + } finally { |
| 168 | + storage.close(); |
| 169 | + } |
| 170 | + } catch { |
| 171 | + // Skip if database can't be cleared |
185 | 172 | } |
186 | 173 |
|
187 | 174 | return removed; |
|
0 commit comments