Skip to content

Commit 34fd55f

Browse files
committed
refactor: Simplify CacheCleanup
1 parent 1bd0769 commit 34fd55f

1 file changed

Lines changed: 54 additions & 134 deletions

File tree

packages/project/lib/cache/CacheCleanup.js

Lines changed: 54 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -43,161 +43,61 @@ async function getDirectorySize(dirPath) {
4343
// framework/staging, framework/locks, etc.
4444
// ========================================
4545

46+
// ========================================
47+
// BUILD CACHE (build/cache namespace)
48+
// Manages: buildCache/v*/ SQLite databases
49+
// ========================================
50+
51+
// ========================================
52+
// PUBLIC API - Orchestrates both caches
53+
// ========================================
54+
4655
/**
47-
* Check if framework cache exists and get its info.
56+
* Check what cache items exist and their sizes without removing them.
4857
*
49-
* @param {string} ui5DataDir Resolved absolute path to UI5 data directory
50-
* @returns {Promise<{path: string, size: number, type: string}|null>} Framework cache info or null
58+
* @param {object} options
59+
* @param {string} options.ui5DataDir Resolved absolute path to UI5 data directory
60+
* @returns {Promise<Array<{path: string, size: number, type: string}>>} List of cache items
5161
*/
52-
async function getFrameworkCacheInfo(ui5DataDir) {
62+
export async function getCacheInfo({ui5DataDir}) {
63+
const items = [];
64+
65+
// Check framework cache
5366
const frameworkDir = path.join(ui5DataDir, "framework");
5467
try {
5568
await fs.access(frameworkDir);
5669
const size = await getDirectorySize(frameworkDir);
5770
if (size > 0) {
58-
return {
71+
items.push({
5972
path: "framework/",
6073
size,
6174
type: "directory"
62-
};
75+
});
6376
}
6477
} catch {
6578
// Directory doesn't exist
6679
}
67-
return null;
68-
}
69-
70-
/**
71-
* Clean framework cache directory.
72-
*
73-
* @param {string} ui5DataDir Resolved absolute path to UI5 data directory
74-
* @param {{path: string, size: number, type: string}} frameworkInfo Framework cache info
75-
* @returns {Promise<{path: string, type: string, size: number}|null>} Removal result or null
76-
*/
77-
async function cleanFrameworkCache(ui5DataDir, frameworkInfo) {
78-
if (!frameworkInfo) {
79-
return null;
80-
}
81-
82-
const frameworkDir = path.join(ui5DataDir, "framework");
83-
try {
84-
await fs.rm(frameworkDir, {recursive: true, force: true});
85-
return {
86-
path: "framework",
87-
type: "framework",
88-
size: frameworkInfo.size
89-
};
90-
} catch {
91-
// Framework directory couldn't be removed
92-
}
93-
return null;
94-
}
95-
96-
// ========================================
97-
// BUILD CACHE (build/cache namespace)
98-
// Manages: buildCache/v*/ SQLite databases
99-
// ========================================
10080

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.
104-
*
105-
* @param {string} ui5DataDir Resolved absolute path to UI5 data directory
106-
* @returns {Promise<{path: string, size: number, type: string}|null>} Build cache info or null
107-
*/
108-
async function getBuildCacheInfo(ui5DataDir) {
81+
// Check build cache (only current version)
10982
const buildCacheDir = path.join(ui5DataDir, "buildCache");
11083
const dbDir = path.join(buildCacheDir, CACHE_VERSION);
111-
11284
try {
11385
await fs.access(dbDir);
114-
} catch {
115-
// Current version directory doesn't exist
116-
return null;
117-
}
118-
119-
try {
12086
const storage = new BuildCacheStorage(dbDir);
12187
try {
12288
if (storage.hasRecords()) {
12389
const size = await getDirectorySize(buildCacheDir);
124-
return {
90+
items.push({
12591
path: `buildCache/${CACHE_VERSION} (database records)`,
12692
size,
12793
type: "database"
128-
};
94+
});
12995
}
13096
} finally {
13197
storage.close();
13298
}
13399
} catch {
134-
// Skip if database can't be opened
135-
}
136-
return null;
137-
}
138-
139-
/**
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.
142-
*
143-
* @param {string} ui5DataDir Resolved absolute path to UI5 data directory
144-
* @returns {Promise<Array<{path: string, type: string, size: number}>>} Removed entries
145-
*/
146-
async function cleanBuildCache(ui5DataDir) {
147-
const buildCacheDir = path.join(ui5DataDir, "buildCache");
148-
const dbDir = path.join(buildCacheDir, CACHE_VERSION);
149-
const removed = [];
150-
151-
try {
152-
await fs.access(dbDir);
153-
} catch {
154-
// Current version directory doesn't exist
155-
return removed;
156-
}
157-
158-
try {
159-
const storage = new BuildCacheStorage(dbDir);
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
172-
}
173-
174-
return removed;
175-
}
176-
177-
// ========================================
178-
// PUBLIC API - Orchestrates both caches
179-
// ========================================
180-
181-
/**
182-
* Check what cache items exist and their sizes without removing them.
183-
*
184-
* @param {object} options
185-
* @param {string} options.ui5DataDir Resolved absolute path to UI5 data directory
186-
* @returns {Promise<Array<{path: string, size: number, type: string}>>} List of cache items
187-
*/
188-
export async function getCacheInfo({ui5DataDir}) {
189-
const items = [];
190-
191-
// Check framework cache
192-
const frameworkInfo = await getFrameworkCacheInfo(ui5DataDir);
193-
if (frameworkInfo) {
194-
items.push(frameworkInfo);
195-
}
196-
197-
// Check build cache
198-
const buildInfo = await getBuildCacheInfo(ui5DataDir);
199-
if (buildInfo) {
200-
items.push(buildInfo);
100+
// Skip if database can't be opened or doesn't exist
201101
}
202102

203103
return items;
@@ -218,20 +118,40 @@ export async function getCacheInfo({ui5DataDir}) {
218118
export async function cleanCache({ui5DataDir}) {
219119
const allRemoved = [];
220120

221-
// Get info about what exists
222-
const items = await getCacheInfo({ui5DataDir});
223-
224121
// Clean framework cache
225-
const frameworkItem = items.find((item) => item.path === "framework/");
226-
const frameworkResult = await cleanFrameworkCache(ui5DataDir, frameworkItem);
227-
if (frameworkResult) {
228-
allRemoved.push(frameworkResult);
122+
const frameworkDir = path.join(ui5DataDir, "framework");
123+
try {
124+
const size = await getDirectorySize(frameworkDir);
125+
if (size > 0) {
126+
await fs.rm(frameworkDir, {recursive: true, force: true});
127+
allRemoved.push({
128+
path: "framework",
129+
type: "framework",
130+
size
131+
});
132+
}
133+
} catch {
134+
// Directory doesn't exist or couldn't be removed
229135
}
230136

231-
// Clean build cache
232-
const buildCacheItem = items.find((item) => item.type === "database");
233-
if (buildCacheItem) {
234-
allRemoved.push(...await cleanBuildCache(ui5DataDir));
137+
// Clean build cache (only current version)
138+
const buildCacheDir = path.join(ui5DataDir, "buildCache");
139+
const dbDir = path.join(buildCacheDir, CACHE_VERSION);
140+
try {
141+
await fs.access(dbDir);
142+
const storage = new BuildCacheStorage(dbDir);
143+
try {
144+
const freedSize = storage.clearAllRecords();
145+
allRemoved.push({
146+
path: `buildCache/${CACHE_VERSION}`,
147+
type: "buildCache",
148+
size: freedSize
149+
});
150+
} finally {
151+
storage.close();
152+
}
153+
} catch {
154+
// Database doesn't exist or couldn't be cleared
235155
}
236156

237157
const totalSize = allRemoved.reduce((sum, entry) => sum + entry.size, 0);

0 commit comments

Comments
 (0)