-
Notifications
You must be signed in to change notification settings - Fork 79
feat: Clean cache command #1394
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
d3xter666
wants to merge
20
commits into
main
Choose a base branch
from
feat-clean-cache
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 13 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
9bcacaf
feat: Clean cache command
d3xter666 d49b45d
refactor: Use single place for DB manipulation
d3xter666 c64092a
refactor: Simplify cache clean
d3xter666 30df6b5
refactor: Position correctly the CacheCleanup
d3xter666 7318ffe
refactor: Add confirmation dialog for the cache clean command
d3xter666 0320c6f
refactor: Rename cacheVersionDir
d3xter666 b0c9252
refactor: Restore location of CacheCleanup
d3xter666 dc168dc
fix: Clean only current cache version
d3xter666 becc3a8
refactor: Simplify CacheCleanup
d3xter666 c958891
test: Improve coverage
d3xter666 36fd376
refactor: CLI package orchestrates cache cleanup
d3xter666 c1eb38c
refactor: Add skip confirmation option
d3xter666 48aafe9
fix: Windows paths for tests
d3xter666 1c161bb
refactor: Use yesno package for CLI confirmation
d3xter666 2631451
refactor: Simplify cleanup meta structure
d3xter666 1ade6fd
fix: Add guard to not accidently create a new DB
d3xter666 8a53eb8
refactor: Reuse meta from installers in cache cleanup
d3xter666 fd604cd
refactor: Cleanup details
d3xter666 9c81ede
fix: Respect datadir config
d3xter666 8172c6f
docs: Update cache clean --help CLI information
d3xter666 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| import chalk from "chalk"; | ||
| import path from "node:path"; | ||
| import os from "node:os"; | ||
| import process from "node:process"; | ||
| import readline from "node:readline"; | ||
| import baseMiddleware from "../middlewares/base.js"; | ||
| import Configuration from "@ui5/project/config/Configuration"; | ||
| import * as frameworkCache from "@ui5/project/ui5Framework/cache"; | ||
| import CacheManager from "@ui5/project/build/cache/CacheManager"; | ||
|
|
||
| const cacheCommand = { | ||
| command: "cache", | ||
| describe: "Manage UI5 CLI cache", | ||
| middlewares: [baseMiddleware], | ||
| handler: handleCache | ||
| }; | ||
|
|
||
| cacheCommand.builder = function(cli) { | ||
| return cli | ||
| .demandCommand(1, "Command required. Available command is 'clean'") | ||
| .command("clean", "Remove all cached UI5 data", { | ||
| handler: handleCache, | ||
| builder: function(yargs) { | ||
| return yargs.option("interactive", { | ||
|
d3xter666 marked this conversation as resolved.
Outdated
|
||
| describe: "Show confirmation prompt before cleaning. Use --no-interactive to skip (e.g. for CI)", | ||
| default: true, | ||
| type: "boolean", | ||
| }); | ||
| }, | ||
| middlewares: [baseMiddleware], | ||
| }) | ||
| .example("$0 cache clean", | ||
| "Remove all cached UI5 data") | ||
| .example("$0 cache clean --no-interactive", | ||
| "Remove all cached UI5 data without confirmation (CI mode)"); | ||
| }; | ||
|
|
||
| /** | ||
| * Format a byte size as a human-readable string. | ||
| * | ||
| * @param {number} bytes Size in bytes | ||
| * @returns {string} Formatted size string | ||
| */ | ||
| function formatSize(bytes) { | ||
| if (bytes < 1024) { | ||
| return `${bytes} B`; | ||
| } else if (bytes < 1024 * 1024) { | ||
| return `${(bytes / 1024).toFixed(1)} KB`; | ||
| } else if (bytes < 1024 * 1024 * 1024) { | ||
| return `${(bytes / (1024 * 1024)).toFixed(1)} MB`; | ||
| } | ||
| return `${(bytes / (1024 * 1024 * 1024)).toFixed(1)} GB`; | ||
| } | ||
|
|
||
| /** | ||
| * Prompt user for confirmation. | ||
| * | ||
| * @param {string} question The question to ask | ||
| * @returns {Promise<boolean>} True if user confirmed | ||
| */ | ||
| async function confirm(question) { | ||
| const rl = readline.createInterface({ | ||
|
d3xter666 marked this conversation as resolved.
Outdated
|
||
| input: process.stdin, | ||
| output: process.stderr | ||
| }); | ||
|
|
||
| return new Promise((resolve) => { | ||
| rl.question(question, (answer) => { | ||
| rl.close(); | ||
| resolve(answer.toLowerCase() === "y" || answer.toLowerCase() === "yes"); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| async function handleCache(argv) { | ||
| const interactive = argv?.interactive !== false; | ||
|
|
||
| // Resolve UI5 data directory | ||
| let ui5DataDir = process.env.UI5_DATA_DIR; | ||
| if (!ui5DataDir) { | ||
| const config = await Configuration.fromFile(); | ||
| ui5DataDir = config.getUi5DataDir(); | ||
| } | ||
| if (ui5DataDir) { | ||
| ui5DataDir = path.resolve(process.cwd(), ui5DataDir); | ||
| } else { | ||
| ui5DataDir = path.join(os.homedir(), ".ui5"); | ||
| } | ||
|
|
||
| // Check what items exist before cleaning (orchestrate both domains) | ||
| const items = []; | ||
| const frameworkInfo = await frameworkCache.getCacheInfo(ui5DataDir); | ||
| if (frameworkInfo) { | ||
| items.push(frameworkInfo); | ||
| } | ||
| const buildInfo = await CacheManager.getCacheInfo(ui5DataDir); | ||
| if (buildInfo) { | ||
| items.push(buildInfo); | ||
| } | ||
|
|
||
| if (items.length === 0) { | ||
| process.stderr.write("Nothing to clean\n"); | ||
| return; | ||
| } | ||
|
|
||
| // Display items that will be removed | ||
| process.stderr.write(chalk.bold("\nThe following items from cache will be removed:\n")); | ||
| let totalSize = 0; | ||
| for (const item of items) { | ||
| totalSize += item.size; | ||
| const sizeStr = item.size > 0 ? ` (${formatSize(item.size)})` : ""; | ||
| process.stderr.write(` ${chalk.yellow("•")} ${item.path}${sizeStr}\n`); | ||
| } | ||
| process.stderr.write(chalk.bold(`\nTotal: ${formatSize(totalSize)}\n\n`)); | ||
|
|
||
| // Ask for confirmation (skip in non-interactive mode) | ||
| if (interactive) { | ||
| const confirmed = await confirm("Do you want to continue? (y/N) "); | ||
| if (!confirmed) { | ||
| process.stderr.write("Cancelled\n"); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| // Perform the actual cleanup (orchestrate both domains) | ||
| const removed = []; | ||
| const frameworkResult = await frameworkCache.cleanCache(ui5DataDir); | ||
| if (frameworkResult) { | ||
| removed.push(frameworkResult); | ||
| } | ||
| const buildResult = await CacheManager.cleanCache(ui5DataDir); | ||
| if (buildResult) { | ||
| removed.push(buildResult); | ||
| } | ||
|
|
||
| process.stderr.write("\n"); | ||
| for (const entry of removed) { | ||
| const sizeStr = entry.size > 0 ? ` (${formatSize(entry.size)})` : ""; | ||
| process.stderr.write(`${chalk.green("✓")} Removed ${chalk.bold(entry.path)}${sizeStr}\n`); | ||
| } | ||
|
|
||
| const totalRemoved = removed.reduce((sum, entry) => sum + entry.size, 0); | ||
| process.stderr.write( | ||
| `\n${chalk.green("Success:")} Cleaned ${removed.length} ${removed.length === 1 ? "entry" : "entries"}` + | ||
| (totalRemoved > 0 ? `, freed ${formatSize(totalRemoved)}` : "") + "\n" | ||
| ); | ||
| } | ||
|
|
||
| export default cacheCommand; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.