Skip to content

Commit 7473bd4

Browse files
committed
refactor: Simplify CacheCleanup
1 parent 1bd0769 commit 7473bd4

1 file changed

Lines changed: 45 additions & 144 deletions

File tree

packages/project/lib/cache/CacheCleanup.js

Lines changed: 45 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ import fs from "node:fs/promises";
33
import BuildCacheStorage from "../build/cache/BuildCacheStorage.js";
44
import {CACHE_VERSION} from "../build/cache/CacheManager.js";
55

6-
// ========================================
7-
// SHARED UTILITIES
8-
// ========================================
9-
106
/**
117
* Get the size of a directory tree recursively.
128
*
@@ -37,167 +33,52 @@ async function getDirectorySize(dirPath) {
3733
return total;
3834
}
3935

40-
// ========================================
41-
// FRAMEWORK CACHE (ui5Framework namespace)
42-
// Manages: framework/packages, framework/cacache,
43-
// framework/staging, framework/locks, etc.
44-
// ========================================
45-
4636
/**
47-
* Check if framework cache exists and get its info.
37+
* Check what cache items exist and their sizes without removing them.
4838
*
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
39+
* @param {object} options
40+
* @param {string} options.ui5DataDir Resolved absolute path to UI5 data directory
41+
* @returns {Promise<Array<{path: string, size: number, type: string}>>} List of cache items
5142
*/
52-
async function getFrameworkCacheInfo(ui5DataDir) {
43+
export async function getCacheInfo({ui5DataDir}) {
44+
const items = [];
45+
46+
// Check framework cache
5347
const frameworkDir = path.join(ui5DataDir, "framework");
5448
try {
5549
await fs.access(frameworkDir);
5650
const size = await getDirectorySize(frameworkDir);
5751
if (size > 0) {
58-
return {
52+
items.push({
5953
path: "framework/",
6054
size,
6155
type: "directory"
62-
};
56+
});
6357
}
6458
} catch {
6559
// Directory doesn't exist
6660
}
67-
return null;
68-
}
6961

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-
// ========================================
100-
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) {
62+
// Check build cache (only current version)
10963
const buildCacheDir = path.join(ui5DataDir, "buildCache");
11064
const dbDir = path.join(buildCacheDir, CACHE_VERSION);
111-
11265
try {
11366
await fs.access(dbDir);
114-
} catch {
115-
// Current version directory doesn't exist
116-
return null;
117-
}
118-
119-
try {
12067
const storage = new BuildCacheStorage(dbDir);
12168
try {
12269
if (storage.hasRecords()) {
12370
const size = await getDirectorySize(buildCacheDir);
124-
return {
71+
items.push({
12572
path: `buildCache/${CACHE_VERSION} (database records)`,
12673
size,
12774
type: "database"
128-
};
75+
});
12976
}
13077
} finally {
13178
storage.close();
13279
}
13380
} 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);
81+
// Skip if database can't be opened or doesn't exist
20182
}
20283

20384
return items;
@@ -218,20 +99,40 @@ export async function getCacheInfo({ui5DataDir}) {
21899
export async function cleanCache({ui5DataDir}) {
219100
const allRemoved = [];
220101

221-
// Get info about what exists
222-
const items = await getCacheInfo({ui5DataDir});
223-
224102
// 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);
103+
const frameworkDir = path.join(ui5DataDir, "framework");
104+
try {
105+
const size = await getDirectorySize(frameworkDir);
106+
if (size > 0) {
107+
await fs.rm(frameworkDir, {recursive: true, force: true});
108+
allRemoved.push({
109+
path: "framework",
110+
type: "framework",
111+
size
112+
});
113+
}
114+
} catch {
115+
// Directory doesn't exist or couldn't be removed
229116
}
230117

231-
// Clean build cache
232-
const buildCacheItem = items.find((item) => item.type === "database");
233-
if (buildCacheItem) {
234-
allRemoved.push(...await cleanBuildCache(ui5DataDir));
118+
// Clean build cache (only current version)
119+
const buildCacheDir = path.join(ui5DataDir, "buildCache");
120+
const dbDir = path.join(buildCacheDir, CACHE_VERSION);
121+
try {
122+
await fs.access(dbDir);
123+
const storage = new BuildCacheStorage(dbDir);
124+
try {
125+
const freedSize = storage.clearAllRecords();
126+
allRemoved.push({
127+
path: `buildCache/${CACHE_VERSION}`,
128+
type: "buildCache",
129+
size: freedSize
130+
});
131+
} finally {
132+
storage.close();
133+
}
134+
} catch {
135+
// Database doesn't exist or couldn't be cleared
235136
}
236137

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

0 commit comments

Comments
 (0)