From 1bd76be9053a63523da19b1942ff5f47880924bf Mon Sep 17 00:00:00 2001 From: MK Date: Tue, 27 Jan 2026 16:56:14 +0800 Subject: [PATCH 1/2] refactor: consolidate cache directory logic via vite_shared crate - Add `get_cache_dir()` to vite_shared for centralized cache path management - Create `cache.rs` module in vite_js_runtime with `get_cache_dir()` helper - Replace repeated `vite_shared::get_cache_dir()?.join("js_runtime")` pattern --- Cargo.lock | 12 +++++++-- Cargo.toml | 1 + crates/vite_install/Cargo.toml | 2 +- crates/vite_install/src/config.rs | 15 ++--------- crates/vite_js_runtime/Cargo.toml | 2 +- crates/vite_js_runtime/src/cache.rs | 12 +++++++++ crates/vite_js_runtime/src/lib.rs | 1 + crates/vite_js_runtime/src/providers/node.rs | 14 ++-------- crates/vite_js_runtime/src/runtime.rs | 18 +++---------- crates/vite_shared/Cargo.toml | 15 +++++++++++ crates/vite_shared/src/cache.rs | 28 ++++++++++++++++++++ crates/vite_shared/src/lib.rs | 5 ++++ 12 files changed, 82 insertions(+), 43 deletions(-) create mode 100644 crates/vite_js_runtime/src/cache.rs create mode 100644 crates/vite_shared/Cargo.toml create mode 100644 crates/vite_shared/src/cache.rs create mode 100644 crates/vite_shared/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 163cdcb8b0..fea225963e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7001,7 +7001,6 @@ version = "0.0.0" dependencies = [ "backon", "crossterm", - "directories", "flate2", "futures-util", "hex", @@ -7023,6 +7022,7 @@ dependencies = [ "vite_error", "vite_glob", "vite_path", + "vite_shared", "vite_str", "vite_workspace", ] @@ -7033,7 +7033,6 @@ version = "0.0.0" dependencies = [ "async-trait", "backon", - "directories", "flate2", "futures-util", "hex", @@ -7048,6 +7047,7 @@ dependencies = [ "tokio", "tracing", "vite_path", + "vite_shared", "vite_str", "zip", ] @@ -7078,6 +7078,14 @@ dependencies = [ "vite_str", ] +[[package]] +name = "vite_shared" +version = "0.0.0" +dependencies = [ + "directories", + "vite_path", +] + [[package]] name = "vite_shell" version = "0.0.0" diff --git a/Cargo.toml b/Cargo.toml index 3528196f41..c0c41e65a8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -158,6 +158,7 @@ vite_js_runtime = { path = "crates/vite_js_runtime" } vite_glob = { git = "ssh://git@github.com/voidzero-dev/vite-task.git", rev = "d1824f23d28fdac7024c80c25f8e84b24b4f7704" } vite_install = { path = "crates/vite_install" } vite_migration = { path = "crates/vite_migration" } +vite_shared = { path = "crates/vite_shared" } vite_path = { git = "ssh://git@github.com/voidzero-dev/vite-task.git", rev = "d1824f23d28fdac7024c80c25f8e84b24b4f7704" } vite_str = { git = "ssh://git@github.com/voidzero-dev/vite-task.git", rev = "d1824f23d28fdac7024c80c25f8e84b24b4f7704" } vite_task = { git = "ssh://git@github.com/voidzero-dev/vite-task.git", rev = "d1824f23d28fdac7024c80c25f8e84b24b4f7704" } diff --git a/crates/vite_install/Cargo.toml b/crates/vite_install/Cargo.toml index 572966d103..ab712dce8f 100644 --- a/crates/vite_install/Cargo.toml +++ b/crates/vite_install/Cargo.toml @@ -10,7 +10,6 @@ rust-version.workspace = true [dependencies] backon = { workspace = true } crossterm = { workspace = true } -directories = { workspace = true } flate2 = { workspace = true } futures-util = { workspace = true } hex = { workspace = true } @@ -30,6 +29,7 @@ vite_command = { workspace = true } vite_error = { workspace = true } vite_glob = { workspace = true } vite_path = { workspace = true } +vite_shared = { workspace = true } vite_str = { workspace = true } vite_workspace = { workspace = true } diff --git a/crates/vite_install/src/config.rs b/crates/vite_install/src/config.rs index 0ad115e87a..d4189854b5 100644 --- a/crates/vite_install/src/config.rs +++ b/crates/vite_install/src/config.rs @@ -1,8 +1,7 @@ use std::{env, sync::LazyLock}; -use directories::BaseDirs; use vite_error::Error; -use vite_path::{AbsolutePathBuf, current_dir}; +use vite_path::AbsolutePathBuf; pub static NPM_REGISTRY: LazyLock = LazyLock::new(|| { env::var("npm_config_registry") @@ -26,11 +25,7 @@ pub fn get_npm_package_version_url(name: &str, version_or_tag: &str) -> String { /// It will use the cache directory of the operating system if available, /// otherwise it will use the current directory. pub fn get_cache_dir() -> Result { - let cache_dir = match BaseDirs::new() { - Some(dirs) => AbsolutePathBuf::new(dirs.cache_dir().to_path_buf()).unwrap(), - None => current_dir()?.join(".cache"), - }; - Ok(cache_dir.join("vite")) + Ok(vite_shared::get_cache_dir()?) } #[cfg(test)] @@ -53,10 +48,4 @@ mod tests { "https://registry.npmjs.org/@vitejs/release-scripts/-/release-scripts-1.6.0.tgz" ); } - - #[test] - fn test_get_cache_dir() { - let cache_dir = get_cache_dir().unwrap(); - assert!(cache_dir.ends_with("vite")); - } } diff --git a/crates/vite_js_runtime/Cargo.toml b/crates/vite_js_runtime/Cargo.toml index 8c502af052..1177b3064b 100644 --- a/crates/vite_js_runtime/Cargo.toml +++ b/crates/vite_js_runtime/Cargo.toml @@ -10,7 +10,6 @@ rust-version.workspace = true [dependencies] async-trait = { workspace = true } backon = { workspace = true } -directories = { workspace = true } flate2 = { workspace = true } futures-util = { workspace = true } hex = { workspace = true } @@ -24,6 +23,7 @@ thiserror = { workspace = true } tokio = { workspace = true, features = ["full"] } tracing = { workspace = true } vite_path = { workspace = true } +vite_shared = { workspace = true } vite_str = { workspace = true } zip = { workspace = true } diff --git a/crates/vite_js_runtime/src/cache.rs b/crates/vite_js_runtime/src/cache.rs new file mode 100644 index 0000000000..170fa4bd67 --- /dev/null +++ b/crates/vite_js_runtime/src/cache.rs @@ -0,0 +1,12 @@ +//! Cache directory utilities for JavaScript runtimes. + +use vite_path::AbsolutePathBuf; + +use crate::Error; + +/// Get the cache directory for JavaScript runtimes. +/// +/// Returns `$CACHE_DIR/vite/js_runtime`. +pub(crate) fn get_cache_dir() -> Result { + Ok(vite_shared::get_cache_dir()?.join("js_runtime")) +} diff --git a/crates/vite_js_runtime/src/lib.rs b/crates/vite_js_runtime/src/lib.rs index 94d654bae9..6836b42f97 100644 --- a/crates/vite_js_runtime/src/lib.rs +++ b/crates/vite_js_runtime/src/lib.rs @@ -33,6 +33,7 @@ //! 2. Add the runtime type to `JsRuntimeType` enum //! 3. Add a match arm in `download_runtime()` to use the new provider +mod cache; mod dev_engines; mod download; mod error; diff --git a/crates/vite_js_runtime/src/providers/node.rs b/crates/vite_js_runtime/src/providers/node.rs index 685ba5a7a1..3184954ff3 100644 --- a/crates/vite_js_runtime/src/providers/node.rs +++ b/crates/vite_js_runtime/src/providers/node.rs @@ -6,10 +6,9 @@ use std::{ }; use async_trait::async_trait; -use directories::BaseDirs; use node_semver::{Range, Version}; use serde::{Deserialize, Serialize}; -use vite_path::{AbsolutePath, AbsolutePathBuf, current_dir}; +use vite_path::{AbsolutePath, AbsolutePathBuf}; use vite_str::Str; use crate::{ @@ -157,7 +156,7 @@ impl NodeProvider { /// /// Returns an error if the download fails or the JSON is invalid. pub async fn fetch_version_index(&self) -> Result, Error> { - let cache_dir = get_cache_dir()?; + let cache_dir = crate::cache::get_cache_dir()?; let cache_path = cache_dir.join("node/index_cache.json"); // Try to load from cache @@ -351,15 +350,6 @@ fn calculate_expires_at(max_age: Option) -> u64 { SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() + ttl } -/// Get the cache directory for JavaScript runtimes. -fn get_cache_dir() -> Result { - let cache_dir = match BaseDirs::new() { - Some(dirs) => AbsolutePathBuf::new(dirs.cache_dir().to_path_buf()).unwrap(), - None => current_dir()?.join(".cache"), - }; - Ok(cache_dir.join("vite/js_runtime")) -} - /// Get the Node.js distribution base URL /// /// Returns the value of `VITE_NODE_DIST_MIRROR` environment variable if set, diff --git a/crates/vite_js_runtime/src/runtime.rs b/crates/vite_js_runtime/src/runtime.rs index 6690942997..c3152a1861 100644 --- a/crates/vite_js_runtime/src/runtime.rs +++ b/crates/vite_js_runtime/src/runtime.rs @@ -1,6 +1,5 @@ -use directories::BaseDirs; use tempfile::TempDir; -use vite_path::{AbsolutePath, AbsolutePathBuf, current_dir}; +use vite_path::{AbsolutePath, AbsolutePathBuf}; use vite_str::Str; use crate::{ @@ -108,7 +107,7 @@ pub async fn download_runtime_with_provider( version: &str, ) -> Result { let platform = Platform::current(); - let cache_dir = get_cache_dir()?; + let cache_dir = crate::cache::get_cache_dir()?; // Get paths from provider let platform_str = provider.platform_string(platform); @@ -212,7 +211,7 @@ pub async fn download_runtime_for_project(project_path: &AbsolutePath) -> Result let package_json_path = project_path.join("package.json"); let dev_engines = read_dev_engines(&package_json_path).await?; let provider = NodeProvider::new(); - let cache_dir = get_cache_dir()?; + let cache_dir = crate::cache::get_cache_dir()?; // Find the "node" runtime configuration (supports both single object and array) let node_runtime = dev_engines @@ -288,15 +287,6 @@ async fn read_dev_engines( Ok(pkg.dev_engines) } -/// Get the cache directory for JavaScript runtimes -fn get_cache_dir() -> Result { - let cache_dir = match BaseDirs::new() { - Some(dirs) => AbsolutePathBuf::new(dirs.cache_dir().to_path_buf()).unwrap(), - None => current_dir()?.join(".cache"), - }; - Ok(cache_dir.join("vite/js_runtime")) -} - #[cfg(test)] mod tests { use tempfile::TempDir; @@ -576,7 +566,7 @@ mod tests { let version = "20.17.0"; // Clear any existing cache for this version - let cache_dir = get_cache_dir().unwrap(); + let cache_dir = crate::cache::get_cache_dir().unwrap(); let install_dir = cache_dir.join(vite_str::format!("node/{version}")); if tokio::fs::try_exists(&install_dir).await.unwrap_or(false) { tokio::fs::remove_dir_all(&install_dir).await.unwrap(); diff --git a/crates/vite_shared/Cargo.toml b/crates/vite_shared/Cargo.toml new file mode 100644 index 0000000000..d5b3e8f615 --- /dev/null +++ b/crates/vite_shared/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "vite_shared" +version = "0.0.0" +authors.workspace = true +edition.workspace = true +license.workspace = true +publish = false +rust-version.workspace = true + +[dependencies] +directories = { workspace = true } +vite_path = { workspace = true } + +[lints] +workspace = true diff --git a/crates/vite_shared/src/cache.rs b/crates/vite_shared/src/cache.rs new file mode 100644 index 0000000000..ac398d608a --- /dev/null +++ b/crates/vite_shared/src/cache.rs @@ -0,0 +1,28 @@ +use directories::BaseDirs; +use vite_path::{AbsolutePathBuf, current_dir}; + +/// Get the vite cache directory. +/// +/// Uses the OS-specific cache directory (e.g., `~/.cache` on Linux, +/// `~/Library/Caches` on macOS), or falls back to `.cache` in the +/// current working directory. +/// +/// Returns the path to `$CACHE_DIR/vite`. +pub fn get_cache_dir() -> std::io::Result { + let cache_dir = match BaseDirs::new() { + Some(dirs) => AbsolutePathBuf::new(dirs.cache_dir().to_path_buf()).unwrap(), + None => current_dir()?.join(".cache"), + }; + Ok(cache_dir.join("vite")) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_get_cache_dir() { + let cache_dir = get_cache_dir().unwrap(); + assert!(cache_dir.ends_with("vite")); + } +} diff --git a/crates/vite_shared/src/lib.rs b/crates/vite_shared/src/lib.rs new file mode 100644 index 0000000000..71943286b6 --- /dev/null +++ b/crates/vite_shared/src/lib.rs @@ -0,0 +1,5 @@ +//! Shared utilities for vite-plus crates + +mod cache; + +pub use cache::get_cache_dir; From c6a1f1ed04021cf8aeee2bf82e85f9a1501e0c60 Mon Sep 17 00:00:00 2001 From: MK Date: Thu, 29 Jan 2026 11:20:46 +0800 Subject: [PATCH 2/2] refactor(vite_shared): rename cache directory from vite to vite-plus Add platform-specific path documentation for Linux, macOS, and Windows. --- crates/vite_shared/src/cache.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/crates/vite_shared/src/cache.rs b/crates/vite_shared/src/cache.rs index ac398d608a..afa0fd1f04 100644 --- a/crates/vite_shared/src/cache.rs +++ b/crates/vite_shared/src/cache.rs @@ -1,19 +1,23 @@ use directories::BaseDirs; use vite_path::{AbsolutePathBuf, current_dir}; -/// Get the vite cache directory. +/// Get the vite-plus cache directory. /// -/// Uses the OS-specific cache directory (e.g., `~/.cache` on Linux, -/// `~/Library/Caches` on macOS), or falls back to `.cache` in the -/// current working directory. +/// Uses the OS-specific cache directory, or falls back to `.cache` in the +/// current working directory if the home directory cannot be determined. /// -/// Returns the path to `$CACHE_DIR/vite`. +/// # Platform-specific paths +/// +/// - **Linux**: `~/.cache/vite-plus` +/// - **macOS**: `~/Library/Caches/vite-plus` +/// - **Windows**: `C:\Users\\AppData\Local\vite-plus` +/// - **Fallback**: `$CWD/.cache/vite-plus` pub fn get_cache_dir() -> std::io::Result { let cache_dir = match BaseDirs::new() { Some(dirs) => AbsolutePathBuf::new(dirs.cache_dir().to_path_buf()).unwrap(), None => current_dir()?.join(".cache"), }; - Ok(cache_dir.join("vite")) + Ok(cache_dir.join("vite-plus")) } #[cfg(test)] @@ -23,6 +27,6 @@ mod tests { #[test] fn test_get_cache_dir() { let cache_dir = get_cache_dir().unwrap(); - assert!(cache_dir.ends_with("vite")); + assert!(cache_dir.ends_with("vite-plus")); } }