Skip to content

Commit

Permalink
Optimize judgment logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ityuany committed Jul 1, 2024
1 parent 5522910 commit f3795a9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 37 deletions.
33 changes: 20 additions & 13 deletions crates/snm_config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use config::{Config, Environment};
use serde::Deserialize;
use snm_node_version::{parse_node_version, NodeVersion};
use snm_npmrc::parse_npmrc;
use snm_package_json::{parse_package_json, PackageJson};
use snm_package_json::{package_manager_meta::PackageManager, parse_package_json, PackageJson};
use snm_utils::snm_error::SnmError;
use std::{env, path::PathBuf};

Expand Down Expand Up @@ -96,18 +96,25 @@ impl SnmConfig {
}
}

pub fn get_runtime_package_manager_name(&self) -> Option<String> {
match env::var(SNM_PACKAGE_MANAGER_NAME_ENV_KEY) {
Ok(v) => Some(v),
Err(_) => None,
}
}

pub fn get_runtime_package_manager_version(&self) -> Option<String> {
match env::var(SNM_PACKAGE_MANAGER_VERSION_ENV_KEY) {
Ok(v) => Some(v),
Err(_) => None,
}
pub fn get_runtime_package_manager(&self) -> Option<PackageManager> {
let name = match env::var(SNM_PACKAGE_MANAGER_NAME_ENV_KEY) {
Ok(v) => v,
Err(_) => {
return None;
}
};
let version = match env::var(SNM_PACKAGE_MANAGER_VERSION_ENV_KEY) {
Ok(v) => v,
Err(_) => {
return None;
}
};
Some(PackageManager {
name,
version,
hash: None,
raw: "".to_string(),
})
}

pub fn get_snm_node_version(&self) -> Option<NodeVersion> {
Expand Down
36 changes: 12 additions & 24 deletions crates/snm_shim/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,22 @@ pub async fn load_package_manage_shim(prefix: &str, bin_name: &str) -> Result<St
let snm_package_manage: &dyn AtomTrait =
&SnmPackageManager::from_prefix(prefix, snm_config.clone());

if snm_config.get_strict() && snm_config.get_runtime_package_manager_name().is_none() {
if snm_config.get_strict() && snm_config.get_runtime_package_manager().is_none() {
return Err(SnmError::NotFoundPackageJsonError(dir.to_path_buf()));
}

let restricted_list = vec!["install".to_string(), "run".to_string()];

let version = match snm_config.get_runtime_package_manager_name() {
Some(name) if name == bin_name => {
if let Some(v) = snm_config.get_runtime_package_manager_version().clone() {
v
} else if restricted_list.contains(command) {
return Err(SnmError::NotFoundPackageManagerVersionInEnvError { name: name });
} else {
snm_package_manage.get_default_version()?
}
}
Some(name) => {
if restricted_list.contains(command) {
return Err(SnmError::NotMatchPackageManagerError {
raw_command: args_all.join(" "),
expected: name.clone(),
actual: bin_name.to_string(),
});
} else {
snm_package_manage.get_default_version()?
}
let restricted_list = vec!["install", "i", "run"];

let version = match snm_config.get_runtime_package_manager() {
Some(package_manager) if package_manager.name == prefix => package_manager.version,
Some(package_manager) if restricted_list.contains(&command.as_str()) => {
return Err(SnmError::NotMatchPackageManagerError {
raw_command: args_all.join(" "),
expected: package_manager.name.clone(),
actual: prefix.to_string(),
});
}
None => snm_package_manage.get_default_version()?,
_ => snm_package_manage.get_default_version()?,
};

let binary_path_buf = ensure_binary_path(bin_name, snm_package_manage, &version).await?;
Expand Down

0 comments on commit f3795a9

Please sign in to comment.