use std::fs;
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use serde_json::Value;
use crate::errors::ErrorContext;
use crate::utils::normalize_path;
pub(super) fn watch_registry_key(path: &Path) -> PathBuf {
fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf())
}
pub(super) fn is_test_runtime() -> bool {
std::env::var_os("VITEST").is_some()
|| std::env::var("NODE_ENV")
.map(|value| value == "test")
.unwrap_or(false)
}
pub(super) fn now_ms() -> i64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or(Duration::ZERO)
.as_millis() as i64
}
pub(super) fn relative_posix(root: &Path, path: &Path) -> String {
path.strip_prefix(root)
.map(path_to_posix)
.unwrap_or_else(|_| path_to_posix(path))
}
fn path_to_posix(path: &Path) -> String {
normalize_path(&path.to_string_lossy())
}
pub(super) fn path_string(path: &Path) -> String {
path.to_string_lossy().into_owned()
}
pub(super) fn context(entries: impl IntoIterator<Item = (&'static str, Value)>) -> ErrorContext {
entries
.into_iter()
.map(|(key, value)| (key.to_owned(), value))
.collect()
}