Skip to content

Commit

Permalink
优化 npmrc 的读取逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
ityuany committed Feb 1, 2025
1 parent fd491c6 commit bb2b081
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 50 deletions.
1 change: 1 addition & 0 deletions crates/snm_npmrc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ version = "0.0.1-119"

[dependencies]
config = { workspace = true }
dirs = { workspace = true }
95 changes: 45 additions & 50 deletions crates/snm_npmrc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
path::{Path, PathBuf},
};

use config::{Config, File, FileFormat};
use config::{Config, File, FileFormat, FileSourceFile};

const DEFAULT_REGISTRY: &str = "https://registry.npmjs.org/";

Expand All @@ -18,64 +18,59 @@ pub struct NpmrcReader {

impl NpmrcReader {
pub fn from<P: AsRef<Path>>(workspace: P) -> Self {
let home_dir = match env::var_os("HOME") {
Some(home_dir) => home_dir,
None => return Self { config: None },
};

let sources = {
#[cfg(target_os = "windows")]
{
// TODO: check if this is correct
vec![
env::var_os("APPDATA")
.map(PathBuf::from)
.map(|p| p.join(FILE_NAME))
.unwrap_or_default(),
PathBuf::from(&home_dir).join(FILE_NAME),
workspace.as_ref().to_path_buf().join(FILE_NAME),
]
}
#[cfg(not(target_os = "windows"))]
{
let prefix = env::var_os("PREFIX").unwrap_or_default();
vec![
PathBuf::from("/")
.join(&prefix)
.join("etc")
.join(ETC_FILE_NAME),
PathBuf::from(&home_dir).join(FILE_NAME),
workspace.as_ref().to_path_buf().join(FILE_NAME),
]
}
};

let sources = sources
let sources = Self::collect_config_sources(workspace.as_ref());

let config = Config::builder().add_source(sources).build().ok();

Self { config }
}

fn collect_config_sources(workspace: &Path) -> Vec<File<FileSourceFile, FileFormat>> {
let mut sources = Vec::new();

// 添加系统特定路径
if let Some(sys_path) = Self::get_system_config_path() {
sources.push(sys_path);
}

// 添加通用路径
if let Some(home_dir) = dirs::home_dir() {
sources.push(home_dir.join(FILE_NAME));
}

sources.push(workspace.join(FILE_NAME));

sources
.into_iter()
.filter_map(|path| {
path
.exists()
.then(|| File::from(path).format(FileFormat::Ini))
})
.collect::<Vec<_>>();
.filter(|path| path.exists())
.map(|path| File::from(path).format(FileFormat::Ini))
.collect()
}

Self {
config: Config::builder().add_source(sources).build().ok(),
fn get_system_config_path() -> Option<PathBuf> {
#[cfg(target_os = "windows")]
{
env::var_os("APPDATA").map(|app_data| PathBuf::from(app_data).join(FILE_NAME))
}
#[cfg(not(target_os = "windows"))]
{
env::var_os("PREFIX").map(|prefix| {
PathBuf::from("/")
.join(prefix)
.join("etc")
.join(ETC_FILE_NAME)
})
}
}

pub fn read_registry_with_default(&self) -> String {
let registry = self
self
.config
.as_ref()
.and_then(|c| c.get_string("registry").ok())
.unwrap_or(DEFAULT_REGISTRY.to_string());

if registry.ends_with('/') {
registry[..registry.len() - 1].to_string()
} else {
registry
}
.unwrap_or_else(|| DEFAULT_REGISTRY.to_string())
.trim_end_matches("/")
.to_string()
}

pub fn read(&self, key: &str) -> Option<String> {
Expand Down

0 comments on commit bb2b081

Please sign in to comment.