use crate::time::day_start_before;
pub const NPM_BEFORE: &str = "npm_config_before";
pub const UV_EXCLUDE_NEWER: &str = "UV_EXCLUDE_NEWER";
pub struct Found<'a> {
pub inherited: &'a dyn Fn(&str) -> bool,
pub project_has: &'a dyn Fn(&str) -> bool,
pub project_file: &'a dyn Fn(&str) -> Option<String>,
}
pub fn env(days: u16, now_ms: i64, found: &Found<'_>) -> Vec<(&'static str, String)> {
let Found {
inherited,
project_has,
project_file,
} = found;
if days == 0 {
return Vec::new();
}
let cutoff = day_start_before(now_ms, days);
let mut vars = Vec::new();
let npm_set = inherited(NPM_BEFORE)
|| inherited("NPM_CONFIG_BEFORE")
|| project_file(".npmrc").is_some_and(|text| sets_key(&text, "before"));
if !npm_set {
vars.push((NPM_BEFORE, cutoff.clone()));
}
let uv_set = inherited(UV_EXCLUDE_NEWER)
|| project_has("uv.lock")
|| ["pyproject.toml", "uv.toml"]
.iter()
.any(|f| project_file(f).is_some_and(|text| text.contains("exclude-newer")));
if !uv_set {
vars.push((UV_EXCLUDE_NEWER, cutoff));
}
vars
}
fn sets_key(npmrc: &str, key: &str) -> bool {
npmrc.lines().any(|line| {
line.trim()
.strip_prefix(key)
.is_some_and(|rest| rest.trim_start().starts_with('='))
})
}
#[cfg(test)]
mod tests;