use std::path::PathBuf;
use crate::dev_proxy::endpoint::IpcEndpoint;
use crate::dev_proxy::vite::ViteRoutes;
pub(crate) const IPC_ENV: &str = crate::config::VITE_IPC_ENV;
#[must_use]
pub(crate) fn endpoint_from_env() -> Option<IpcEndpoint> {
parse_endpoint(std::env::var(IPC_ENV).ok())
}
#[must_use]
pub(crate) fn parse_endpoint(raw: Option<String>) -> Option<IpcEndpoint> {
raw.filter(|s| !s.is_empty())
.map(PathBuf::from)
.map(IpcEndpoint::new)
}
pub(crate) const PREFIXES_ENV: &str = "ARCATURE_VITE_PREFIXES";
#[must_use]
pub(crate) fn prefixes_from_env() -> ViteRoutes {
parse_prefixes(std::env::var(PREFIXES_ENV).ok())
}
#[must_use]
pub(crate) fn parse_prefixes(raw: Option<String>) -> ViteRoutes {
let Some(raw) = raw.filter(|s| !s.trim().is_empty()) else {
return ViteRoutes::defaults();
};
let roots = ViteRoutes::new(raw.split(','));
if roots.asset_roots().is_empty() {
return ViteRoutes::defaults();
}
roots
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_none_yields_none() {
assert!(parse_endpoint(None).is_none());
}
#[test]
fn parse_empty_yields_none() {
assert!(parse_endpoint(Some(String::new())).is_none());
}
#[test]
fn parse_nonempty_yields_endpoint() {
let endpoint = parse_endpoint(Some(String::from("/tmp/arcature-vite-test.sock")))
.expect("non-empty value should yield an endpoint");
assert_eq!(
endpoint.path(),
std::path::Path::new("/tmp/arcature-vite-test.sock")
);
}
#[test]
fn unset_prefixes_yield_the_conventional_roots() {
let routes = parse_prefixes(None);
assert!(routes.matches_path("/resources/js/app.tsx"));
assert!(routes.matches_path("/src/app.tsx"));
}
#[test]
fn a_blank_prefix_value_yields_the_conventional_roots() {
let routes = parse_prefixes(Some(String::from(" ")));
assert!(routes.matches_path("/resources/js/app.tsx"));
}
#[test]
fn configured_prefixes_are_split_on_commas() {
let routes = parse_prefixes(Some(String::from("assets, public/vendor")));
assert!(routes.matches_path("/assets/app.tsx"));
assert!(routes.matches_path("/public/vendor/lib.js"));
assert!(!routes.matches_path("/resources/js/app.tsx"));
}
#[test]
fn a_prefix_value_that_normalises_away_falls_back_to_the_defaults() {
let routes = parse_prefixes(Some(String::from("/,,")));
assert!(routes.matches_path("/resources/js/app.tsx"));
}
#[test]
fn parse_windows_pipe_name() {
let endpoint = parse_endpoint(Some(String::from(r"\\.\pipe\arcature-vite-42")))
.expect("Windows pipe name should yield an endpoint");
assert_eq!(
endpoint.path(),
std::path::Path::new(r"\\.\pipe\arcature-vite-42")
);
}
}