-
Notifications
You must be signed in to change notification settings - Fork 81
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
Draft
d3xter666
wants to merge
62
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.
Draft
Changes from all commits
Commits
Show all changes
62 commits
Select commit
Hold shift + click to select a range
58c8dab
feat: Clean cache command
d3xter666 63ae0be
refactor: Use single place for DB manipulation
d3xter666 ae8fd6c
refactor: Simplify cache clean
d3xter666 822b97c
refactor: Position correctly the CacheCleanup
d3xter666 2b9db79
refactor: Add confirmation dialog for the cache clean command
d3xter666 4fc2366
refactor: Rename cacheVersionDir
d3xter666 ac3ccad
refactor: Restore location of CacheCleanup
d3xter666 2b86b03
fix: Clean only current cache version
d3xter666 eadc2dd
refactor: Simplify CacheCleanup
d3xter666 9eb2589
test: Improve coverage
d3xter666 1e9e164
refactor: CLI package orchestrates cache cleanup
d3xter666 aafb6aa
refactor: Add skip confirmation option
d3xter666 51d7e9e
fix: Windows paths for tests
d3xter666 d84abfb
refactor: Use yesno package for CLI confirmation
d3xter666 70abc65
refactor: Simplify cleanup meta structure
d3xter666 7a400c2
fix: Add guard to not accidently create a new DB
d3xter666 4b46ef3
refactor: Reuse meta from installers in cache cleanup
d3xter666 63bf4c0
refactor: Cleanup details
d3xter666 91940f5
fix: Respect datadir config
d3xter666 8f71c58
docs: Update cache clean --help CLI information
d3xter666 542124c
refactor: Cleanup confirmation revised
d3xter666 a9b1a4d
refactor: Revise actual cleanup UX
d3xter666 111c3eb
fix: Docs generation failure
d3xter666 7e72368
fix: Respect cleanup locking
d3xter666 9881fde
refactor: Redundant cleanup summary
d3xter666 04846b9
fix: Cache locking race condition
d3xter666 0ee608a
test: Fix race condition expectations
d3xter666 1dbf5fe
refactor: Cleanups
d3xter666 3799a0c
refactor: Naming of locks
d3xter666 b604bcd
docs: Update documentation to respect recent changes
d3xter666 aa58f34
revert: Redundant maven installer locks
d3xter666 f282d69
refactor: Use pacote's internals for its own cleanup
d3xter666 9e016ee
revert: NPM Install sync. It's now redundant
d3xter666 7cb8c8c
docs: No mention of "incremental build"
d3xter666 f019cd2
refactor: Avoid hardcoded values
d3xter666 076943b
feat: Add cleanup lock for "ui5 serve"
d3xter666 ef04e16
refactor: Move server lock into the server package and reuse in cli
d3xter666 127a7ae
refactor: Data dir is resolved centrally via util
d3xter666 472534c
fix: Remove redundant lock cleanups after server close
d3xter666 5965866
refactor: Revise locks destinations and messages
d3xter666 785f3b9
refactor: Lock full build, so that it does not run into timing problems
d3xter666 cec7a78
refactor: Avoid cache cleanup + build race conditions
d3xter666 8f19f33
refactor: Cleanups
d3xter666 19713f8
refactor: Use the common test/tmp dir
d3xter666 9508077
refactor: Move server lock to the ProjectGraph.serve
d3xter666 e77abba
refactor: Rename getDefaultUi5DataDir to resolveUi5DataDir
d3xter666 aca5195
refactor: Do not pass dir (cwd) as argument to resolveUi5DataDir
d3xter666 7b97f37
refactor: Cleanup of redundant lock watchers
d3xter666 8976268
refactor: Cleanup of ProjectGraph
d3xter666 85c6a6f
refactor: Acquire locks in the project builder & server
d3xter666 218365a
test: Fix stubs
d3xter666 637f369
test: Update lock util tests
d3xter666 3fc73eb
test: Server integration
d3xter666 605a62a
refactor: DB size optimization
d3xter666 beb06d0
refactor: Split cache command into logical chunks
d3xter666 cf9899b
refactor: Consolidate common logic for CacheManager
d3xter666 6748569
refactor: Consolidate repetative code
d3xter666 0033065
refactor: Lock cleanups
d3xter666 2b5b8c0
test: Cleanup of repetative cases
d3xter666 b6db030
refactor: Provide dataDir to the buildServer
d3xter666 fb3a5c7
refactor: Enhance locks
d3xter666 f1f529f
refactor: Cleanup locks for wrong places
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,260 @@ | ||
| import chalk from "chalk"; | ||
| import path from "node:path"; | ||
| import process from "node:process"; | ||
| import baseMiddleware from "../middlewares/base.js"; | ||
| import {resolveUi5DataDir} from "@ui5/project/utils/dataDir"; | ||
| import {getLockDir, hasActiveLocks} from "@ui5/project/utils/lock"; | ||
| import * as frameworkCache from "@ui5/project/ui5Framework/cache"; | ||
| import CacheManager from "@ui5/project/build/cache/CacheManager"; | ||
|
|
||
| const cacheCommand = { | ||
| command: "cache", | ||
| describe: "Manage the UI5 CLI cache (downloaded framework packages and build data)", | ||
| 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("yes", { | ||
| alias: "y", | ||
| describe: "Skip the confirmation prompt, e.g. for use in CI pipelines", | ||
| default: false, | ||
| type: "boolean", | ||
| }) | ||
| .example("$0 cache clean", | ||
| "Remove all cached UI5 data after confirming the prompt") | ||
| .example("$0 cache clean --yes", | ||
| "Remove all cached UI5 data without confirmation (e.g. in CI)") | ||
| .example("UI5_DATA_DIR=/custom/path $0 cache clean", | ||
| "Remove cached data from a non-default UI5 data directory") | ||
| .epilogue( | ||
| "The cache is stored in the UI5 data directory (default: ~/.ui5).\n" + | ||
| "Override the location with the UI5_DATA_DIR environment variable or\n" + | ||
| "the 'ui5DataDir' configuration option (see 'ui5 config --help').\n\n" + | ||
| "Two cache types are removed:\n" + | ||
| " UI5 Framework packages Downloaded UI5 library files " + | ||
| "(~/.ui5/framework/)\n" + | ||
| " Build cache (DB) build data " + | ||
| "(~/.ui5/buildCache/)" | ||
| ); | ||
| }, | ||
| middlewares: [baseMiddleware], | ||
| }); | ||
| }; | ||
|
|
||
| const LABEL_FRAMEWORK = "UI5 Framework packages"; | ||
| const LABEL_BUILD = "Build cache (DB)"; | ||
| // Pad labels to equal width for two-column alignment | ||
| const LABEL_WIDTH = Math.max(LABEL_FRAMEWORK.length, LABEL_BUILD.length); | ||
|
|
||
| /** | ||
| * 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`; | ||
| } | ||
|
|
||
| /** | ||
| * Format framework cache stats as a human-readable detail string. | ||
| * E.g. "1,189 versions of 155 libraries" or "1 version of 1 library". | ||
| * | ||
| * @param {number} libraryCount | ||
| * @param {number} versionCount | ||
| * @returns {string} | ||
| */ | ||
| function formatFrameworkStats(libraryCount, versionCount) { | ||
| const v = `${versionCount.toLocaleString("en-US")} ${versionCount === 1 ? "version" : "versions"}`; | ||
| const l = `${libraryCount.toLocaleString("en-US")} ${libraryCount === 1 ? "library" : "libraries"}`; | ||
| return `${v} of ${l}`; | ||
| } | ||
|
|
||
| /** | ||
| * Pad a label to the shared column width. | ||
| * | ||
| * @param {string} label | ||
| * @returns {string} | ||
| */ | ||
| function padLabel(label) { | ||
| return label.padEnd(LABEL_WIDTH); | ||
| } | ||
|
|
||
| /** | ||
| * Display information about the cached data that will be removed, | ||
| * including the absolute paths and details about the framework and build caches. | ||
| * | ||
| * @param {*} data | ||
| * @param {object} data.frameworkInfo | ||
| * @param {object} data.buildInfo | ||
| * @param {string} data.frameworkAbsPath | ||
| * @param {string} data.buildAbsPath | ||
| * @param {number} data.buildPreSize | ||
| */ | ||
| async function displayCacheInfo({ | ||
| frameworkInfo, | ||
| buildInfo, | ||
| frameworkAbsPath, | ||
| buildAbsPath, | ||
| buildPreSize, | ||
| }) { | ||
| // Display items that will be removed | ||
| process.stderr.write(chalk.bold("\nThe following cached data will be removed:\n\n")); | ||
| if (frameworkInfo) { | ||
| const detail = formatFrameworkStats(frameworkInfo.libraryCount, frameworkInfo.versionCount); | ||
| process.stderr.write( | ||
| ` ${chalk.yellow("•")} ${padLabel(LABEL_FRAMEWORK)} ${frameworkAbsPath} (${detail})\n` | ||
| ); | ||
| } | ||
| if (buildInfo) { | ||
| const detail = buildPreSize > 0 ? formatSize(buildPreSize) : ""; | ||
| process.stderr.write( | ||
| ` ${chalk.yellow("•")} ${padLabel(LABEL_BUILD)} ${buildAbsPath} (${detail})\n` | ||
| ); | ||
| } | ||
| process.stderr.write("\n"); | ||
| } | ||
|
|
||
| /** | ||
| * Display the result of the cache cleanup operation, | ||
| * including which caches were removed and their details. | ||
| * | ||
| * @param {object} data | ||
| * @param {object} data.frameworkResult | ||
| * @param {object} data.buildResult | ||
| * @param {string} data.frameworkAbsPath | ||
| * @param {string} data.buildAbsPath | ||
| * @param {number} data.buildPreSize | ||
| */ | ||
| async function displayCleanupResult({ | ||
| frameworkResult, | ||
| buildResult, | ||
| frameworkAbsPath, | ||
| buildAbsPath, | ||
| buildPreSize, | ||
| }) { | ||
| process.stderr.write("\n"); | ||
| if (frameworkResult) { | ||
| const detail = formatFrameworkStats( | ||
| frameworkResult.libraryCount, | ||
| frameworkResult.versionCount, | ||
| ); | ||
| process.stderr.write( | ||
| `${chalk.green("✓")} Removed ${chalk.bold(LABEL_FRAMEWORK)}` + | ||
| ` (${frameworkAbsPath} · ${detail})\n`, | ||
| ); | ||
| } | ||
| if (buildResult) { | ||
| // Use pre-clean size so the number matches what was shown before confirmation | ||
| const detail = buildPreSize > 0 ? formatSize(buildPreSize) : ""; | ||
| process.stderr.write( | ||
| `${chalk.green("✓")} Removed ${chalk.bold(LABEL_BUILD)}` + | ||
| ` (${buildAbsPath}${detail ? ` · ${detail}` : ""})\n`, | ||
| ); | ||
| } | ||
|
|
||
| // Success summary | ||
| const cleaned = []; | ||
| if (frameworkResult) { | ||
| cleaned.push(LABEL_FRAMEWORK); | ||
| } | ||
| if (buildResult) { | ||
| cleaned.push(LABEL_BUILD); | ||
| } | ||
| process.stderr.write( | ||
| `\n${chalk.green("Success:")} Cleaned ${cleaned.join(" and ")}\n`, | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Prompt the user for confirmation before proceeding with cache cleanup. | ||
| * | ||
| * @param {Yargs.Arguments} argv | ||
| * @returns {Promise<boolean>} Confirmation result | ||
| */ | ||
| async function getConfirmation(argv) { | ||
| if (argv.yes) { | ||
| return true; | ||
| } | ||
| const {default: yesno} = await import("yesno"); | ||
| return yesno({ | ||
| question: "Do you want to continue? (y/N)", | ||
| defaultValue: false | ||
| }); | ||
| } | ||
|
|
||
| async function handleCache(argv) { | ||
| // Resolve UI5 data directory — uses the same resolution chain as ui5 build/serve: | ||
| // UI5_DATA_DIR env var → ui5DataDir config (~/.ui5rc) → default ~/.ui5 | ||
| // Relative paths are resolved against process.cwd() (project root when invoked from the project). | ||
| const ui5DataDir = await resolveUi5DataDir(); | ||
|
|
||
| // Abort early if a lock is active — before prompting the user | ||
| if (await hasActiveLocks(getLockDir(ui5DataDir))) { | ||
| process.stderr.write( | ||
| `${chalk.red("Error:")} A UI5 server or build process is currently running. ` + | ||
| "Cannot clean the cache while it is in use. " + | ||
| "Please stop all running 'ui5 serve' or wait for 'ui5 build' processes to finish.\n" | ||
| ); | ||
| process.exitCode = 1; | ||
| return; | ||
| } | ||
|
|
||
| // Inform the user immediately — getPackageStats may take a moment on a large cache | ||
| process.stderr.write(`Checking cache at ${chalk.bold(ui5DataDir)} …\n`); | ||
|
|
||
| const frameworkInfo = await frameworkCache.getCacheInfo(ui5DataDir); | ||
| const buildInfo = await CacheManager.getCacheInfo(ui5DataDir); | ||
|
|
||
| // Compute absolute paths once — producers return relative sub-path segments | ||
| const frameworkAbsPath = frameworkInfo ? path.join(ui5DataDir, frameworkInfo.path) : null; | ||
| const buildAbsPath = buildInfo ? path.join(ui5DataDir, buildInfo.path) : null; | ||
| const buildPreSize = buildInfo?.size ?? 0; | ||
|
|
||
| if (!frameworkInfo && !buildInfo) { | ||
| process.stderr.write("Nothing to clean\n"); | ||
| return; | ||
| } | ||
|
|
||
| await displayCacheInfo({ | ||
| frameworkInfo, | ||
| buildInfo, | ||
| frameworkAbsPath, | ||
| buildAbsPath, | ||
| buildPreSize, | ||
| }); | ||
|
|
||
| const confirmed = await getConfirmation(argv); | ||
| if (!confirmed) { | ||
| process.stderr.write("Cancelled\n"); | ||
| return; | ||
| } | ||
|
|
||
| // Perform the actual cleanup (orchestrate both domains) | ||
| const frameworkResult = await frameworkCache.cleanCache(ui5DataDir); | ||
| const buildResult = await CacheManager.cleanCache(ui5DataDir); | ||
|
|
||
| await displayCleanupResult({ | ||
| frameworkResult, | ||
| buildResult, | ||
| frameworkAbsPath, | ||
| buildAbsPath, | ||
| buildPreSize, | ||
| }); | ||
| } | ||
|
|
||
| export default cacheCommand; | ||
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
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.