Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
2 changes: 1 addition & 1 deletion crates/vite_install/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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 }

Expand Down
15 changes: 2 additions & 13 deletions crates/vite_install/src/config.rs
Original file line number Diff line number Diff line change
@@ -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<String> = LazyLock::new(|| {
env::var("npm_config_registry")
Expand All @@ -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<AbsolutePathBuf, Error> {
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)]
Expand All @@ -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"));
}
}
2 changes: 1 addition & 1 deletion crates/vite_js_runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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 }

Expand Down
12 changes: 12 additions & 0 deletions crates/vite_js_runtime/src/cache.rs
Original file line number Diff line number Diff line change
@@ -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<AbsolutePathBuf, Error> {
Ok(vite_shared::get_cache_dir()?.join("js_runtime"))
}
1 change: 1 addition & 0 deletions crates/vite_js_runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 2 additions & 12 deletions crates/vite_js_runtime/src/providers/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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<Vec<NodeVersionEntry>, 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
Expand Down Expand Up @@ -351,15 +350,6 @@ fn calculate_expires_at(max_age: Option<u64>) -> u64 {
SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() + ttl
}

/// Get the cache directory for JavaScript runtimes.
fn get_cache_dir() -> Result<AbsolutePathBuf, Error> {
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,
Expand Down
18 changes: 4 additions & 14 deletions crates/vite_js_runtime/src/runtime.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -108,7 +107,7 @@ pub async fn download_runtime_with_provider<P: JsRuntimeProvider>(
version: &str,
) -> Result<JsRuntime, Error> {
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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<AbsolutePathBuf, Error> {
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;
Expand Down Expand Up @@ -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();
Expand Down
15 changes: 15 additions & 0 deletions crates/vite_shared/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions crates/vite_shared/src/cache.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use directories::BaseDirs;
use vite_path::{AbsolutePathBuf, current_dir};

/// Get the vite-plus cache directory.
///
/// Uses the OS-specific cache directory, or falls back to `.cache` in the
/// current working directory if the home directory cannot be determined.
///
/// # Platform-specific paths
///
/// - **Linux**: `~/.cache/vite-plus`
/// - **macOS**: `~/Library/Caches/vite-plus`
/// - **Windows**: `C:\Users\<User>\AppData\Local\vite-plus`
/// - **Fallback**: `$CWD/.cache/vite-plus`
pub fn get_cache_dir() -> std::io::Result<AbsolutePathBuf> {
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-plus"))
}

#[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-plus"));
}
}
5 changes: 5 additions & 0 deletions crates/vite_shared/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//! Shared utilities for vite-plus crates

mod cache;

pub use cache::get_cache_dir;