Skip to content

Commit 6b12746

Browse files
committed
refactor: Restore location of CacheCleanup
1 parent 366b7ce commit 6b12746

5 files changed

Lines changed: 142 additions & 77 deletions

File tree

packages/cli/lib/cli/commands/cache.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import process from "node:process";
55
import readline from "node:readline";
66
import baseMiddleware from "../middlewares/base.js";
77
import Configuration from "@ui5/project/config/Configuration";
8-
import {cleanCache, getCacheInfo} from "@ui5/project/build/cache/CacheCleanup";
8+
import {cleanCache, getCacheInfo} from "@ui5/project/cache/CacheCleanup";
99

1010
const cacheCommand = {
1111
command: "cache",

packages/cli/test/lib/cli/commands/cache.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ test.beforeEach(async (t) => {
3636

3737
t.context.cache = await esmock.p("../../../../lib/cli/commands/cache.js", {
3838
"@ui5/project/config/Configuration": t.context.Configuration,
39-
"@ui5/project/build/cache/CacheCleanup": {
39+
"@ui5/project/cache/CacheCleanup": {
4040
cleanCache: t.context.cleanCacheStub,
4141
getCacheInfo: t.context.getCacheInfoStub,
4242
},

packages/project/lib/build/cache/CacheCleanup.js renamed to packages/project/lib/cache/CacheCleanup.js

Lines changed: 137 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import path from "node:path";
22
import fs from "node:fs/promises";
3-
import BuildCacheStorage from "./BuildCacheStorage.js";
3+
import BuildCacheStorage from "../build/cache/BuildCacheStorage.js";
4+
5+
// ========================================
6+
// SHARED UTILITIES
7+
// ========================================
48

59
/**
610
* Get the size of a directory tree recursively.
711
*
812
* @param {string} dirPath Absolute path to directory
913
* @returns {Promise<number>} Total size in bytes
1014
*/
11-
export async function getDirectorySize(dirPath) {
15+
async function getDirectorySize(dirPath) {
1216
let total = 0;
1317
let entries;
1418
try {
@@ -32,14 +36,123 @@ export async function getDirectorySize(dirPath) {
3236
return total;
3337
}
3438

39+
// ========================================
40+
// FRAMEWORK CACHE (ui5Framework namespace)
41+
// Manages: framework/packages, framework/cacache,
42+
// framework/staging, framework/locks, etc.
43+
// ========================================
44+
45+
/**
46+
* Check if framework cache exists and get its info.
47+
*
48+
* @param {string} ui5DataDir Resolved absolute path to UI5 data directory
49+
* @returns {Promise<{path: string, size: number, type: string}|null>} Framework cache info or null
50+
*/
51+
async function getFrameworkCacheInfo(ui5DataDir) {
52+
const frameworkDir = path.join(ui5DataDir, "framework");
53+
try {
54+
await fs.access(frameworkDir);
55+
const size = await getDirectorySize(frameworkDir);
56+
if (size > 0) {
57+
return {
58+
path: "framework/",
59+
size,
60+
type: "directory"
61+
};
62+
}
63+
} catch {
64+
// Directory doesn't exist
65+
}
66+
return null;
67+
}
68+
69+
/**
70+
* Clean framework cache directory.
71+
*
72+
* @param {string} ui5DataDir Resolved absolute path to UI5 data directory
73+
* @param {{path: string, size: number, type: string}} frameworkInfo Framework cache info
74+
* @returns {Promise<{path: string, type: string, size: number}|null>} Removal result or null
75+
*/
76+
async function cleanFrameworkCache(ui5DataDir, frameworkInfo) {
77+
if (!frameworkInfo) {
78+
return null;
79+
}
80+
81+
const frameworkDir = path.join(ui5DataDir, "framework");
82+
try {
83+
await fs.rm(frameworkDir, {recursive: true, force: true});
84+
return {
85+
path: "framework",
86+
type: "framework",
87+
size: frameworkInfo.size
88+
};
89+
} catch {
90+
// Framework directory couldn't be removed
91+
}
92+
return null;
93+
}
94+
95+
// ========================================
96+
// BUILD CACHE (build/cache namespace)
97+
// Manages: buildCache/v*/ SQLite databases
98+
// ========================================
99+
35100
/**
36-
* Clean build cache directory by clearing all records from the SQLite database.
101+
* Check if build cache exists and get its info.
37102
*
38-
* @param {string} buildCacheDir Path to buildCache/
103+
* @param {string} ui5DataDir Resolved absolute path to UI5 data directory
104+
* @returns {Promise<{path: string, size: number, type: string}|null>} Build cache info or null
105+
*/
106+
async function getBuildCacheInfo(ui5DataDir) {
107+
const buildCacheDir = path.join(ui5DataDir, "buildCache");
108+
try {
109+
await fs.access(buildCacheDir);
110+
const versionDirs = await fs.readdir(buildCacheDir, {withFileTypes: true});
111+
112+
let hasAnyRecords = false;
113+
for (const versionDir of versionDirs) {
114+
if (!versionDir.isDirectory()) {
115+
continue;
116+
}
117+
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
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+
};
139+
}
140+
} catch {
141+
// Directory doesn't exist
142+
}
143+
return null;
144+
}
145+
146+
/**
147+
* Clean build cache by clearing all records from SQLite databases.
148+
*
149+
* @param {string} ui5DataDir Resolved absolute path to UI5 data directory
39150
* @returns {Promise<Array<{path: string, type: string, size: number}>>} Removed entries
40151
*/
41-
async function cleanBuildCache(buildCacheDir) {
152+
async function cleanBuildCache(ui5DataDir) {
153+
const buildCacheDir = path.join(ui5DataDir, "buildCache");
42154
const removed = [];
155+
43156
try {
44157
await fs.access(buildCacheDir);
45158
} catch {
@@ -53,7 +166,6 @@ async function cleanBuildCache(buildCacheDir) {
53166
return removed;
54167
}
55168

56-
57169
for (const versionDir of cacheVersionDirs) {
58170
if (!versionDir.isDirectory()) {
59171
continue;
@@ -75,6 +187,10 @@ async function cleanBuildCache(buildCacheDir) {
75187
return removed;
76188
}
77189

190+
// ========================================
191+
// PUBLIC API - Orchestrates both caches
192+
// ========================================
193+
78194
/**
79195
* Check what cache items exist and their sizes without removing them.
80196
*
@@ -85,58 +201,16 @@ async function cleanBuildCache(buildCacheDir) {
85201
export async function getCacheInfo({ui5DataDir}) {
86202
const items = [];
87203

88-
// Check framework directory
89-
const frameworkDir = path.join(ui5DataDir, "framework");
90-
try {
91-
await fs.access(frameworkDir);
92-
const size = await getDirectorySize(frameworkDir);
93-
if (size > 0) {
94-
items.push({
95-
path: "framework/",
96-
size,
97-
type: "directory"
98-
});
99-
}
100-
} catch {
101-
// Directory doesn't exist, skip
204+
// Check framework cache
205+
const frameworkInfo = await getFrameworkCacheInfo(ui5DataDir);
206+
if (frameworkInfo) {
207+
items.push(frameworkInfo);
102208
}
103209

104-
// Check buildCache directory
105-
const buildCacheDir = path.join(ui5DataDir, "buildCache");
106-
try {
107-
await fs.access(buildCacheDir);
108-
const versionDirs = await fs.readdir(buildCacheDir, {withFileTypes: true});
109-
110-
let hasAnyRecords = false;
111-
for (const versionDir of versionDirs) {
112-
if (!versionDir.isDirectory()) {
113-
continue;
114-
}
115-
116-
const dbDir = path.join(buildCacheDir, versionDir.name);
117-
try {
118-
const storage = new BuildCacheStorage(dbDir);
119-
if (storage.hasRecords()) {
120-
hasAnyRecords = true;
121-
storage.close();
122-
break;
123-
}
124-
storage.close();
125-
} catch {
126-
// Skip if database can't be opened
127-
}
128-
}
129-
130-
if (hasAnyRecords) {
131-
const size = await getDirectorySize(buildCacheDir);
132-
items.push({
133-
path: "buildCache/ (database records)",
134-
size,
135-
type: "database"
136-
});
137-
}
138-
} catch {
139-
// Directory doesn't exist, skip
210+
// Check build cache
211+
const buildInfo = await getBuildCacheInfo(ui5DataDir);
212+
if (buildInfo) {
213+
items.push(buildInfo);
140214
}
141215

142216
return items;
@@ -157,29 +231,20 @@ export async function getCacheInfo({ui5DataDir}) {
157231
export async function cleanCache({ui5DataDir}) {
158232
const allRemoved = [];
159233

160-
// Get info about what exists (reuses getCacheInfo to avoid duplication)
234+
// Get info about what exists
161235
const items = await getCacheInfo({ui5DataDir});
162236

163-
// Remove framework if it exists
237+
// Clean framework cache
164238
const frameworkItem = items.find((item) => item.path === "framework/");
165-
if (frameworkItem) {
166-
const frameworkDir = path.join(ui5DataDir, "framework");
167-
try {
168-
await fs.rm(frameworkDir, {recursive: true, force: true});
169-
allRemoved.push({
170-
path: "framework",
171-
type: "framework",
172-
size: frameworkItem.size
173-
});
174-
} catch {
175-
// Framework directory couldn't be removed
176-
}
239+
const frameworkResult = await cleanFrameworkCache(ui5DataDir, frameworkItem);
240+
if (frameworkResult) {
241+
allRemoved.push(frameworkResult);
177242
}
178243

179-
// Clean build cache if it exists
244+
// Clean build cache
180245
const buildCacheItem = items.find((item) => item.type === "database");
181246
if (buildCacheItem) {
182-
allRemoved.push(...await cleanBuildCache(path.join(ui5DataDir, "buildCache")));
247+
allRemoved.push(...await cleanBuildCache(ui5DataDir));
183248
}
184249

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

packages/project/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"./graph/ProjectGraph": "./lib/graph/ProjectGraph.js",
3232
"./graph/projectGraphBuilder": "./lib/graph/projectGraphBuilder.js",
3333
"./graph": "./lib/graph/graph.js",
34-
"./build/cache/CacheCleanup": "./lib/build/cache/CacheCleanup.js",
34+
"./cache/CacheCleanup": "./lib/cache/CacheCleanup.js",
3535
"./package.json": "./package.json"
3636
},
3737
"engines": {

packages/project/test/lib/build/cache/CacheCleanup.js renamed to packages/project/test/lib/cache/CacheCleanup.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import test from "ava";
22
import path from "node:path";
33
import fs from "node:fs/promises";
44
import {rimraf} from "rimraf";
5-
import {cleanCache} from "../../../../lib/build/cache/CacheCleanup.js";
5+
import {cleanCache} from "../../../lib/cache/CacheCleanup.js";
66

7-
const TEST_DIR = path.join(import.meta.dirname, "..", "..", "..", "tmp", "CacheCleanup");
7+
const TEST_DIR = path.join(import.meta.dirname, "..", "..", "tmp", "CacheCleanup");
88

99
test.after.always(async () => {
1010
await rimraf(TEST_DIR).catch(() => {});

0 commit comments

Comments
 (0)