Skip to content

Commit

Permalink
Use the node bin directory as a backing
Browse files Browse the repository at this point in the history
  • Loading branch information
ityuany committed Jul 11, 2024
1 parent 744431f commit 257cfff
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
6 changes: 3 additions & 3 deletions crates/snm_core/src/traits/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ pub trait AtomTrait {
Ok((dir_name_vec, default_dir))
}

fn get_default_version(&self) -> Result<String, SnmError> {
fn get_default_version(&self) -> Result<Option<String>, SnmError> {
if self.get_snm_config().get_strict() {
return Err(SnmError::NotFoundValidVersion);
return Ok(None);
} else {
let (_, default_v) = self.read_runtime_dir_name_vec()?;
default_v.ok_or(SnmError::NotFoundValidVersion)
Ok(default_v)
}
}
}
48 changes: 23 additions & 25 deletions crates/snm_shim/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ use snm_core::traits::atom::AtomTrait;
use snm_node::snm_node::SnmNode;
use snm_package_manager::snm_package_manager::SnmPackageManager;
use snm_utils::{exec::exec_cli, snm_error::SnmError};
use std::{
env::{self, current_dir},
ops::Not,
};
use std::env::{self, current_dir};

pub async fn load_package_manage_shim(prefix: &str, bin_name: &str) -> Result<String, SnmError> {
pub async fn load_package_manage_shim(prefix: &str, bin_name: &str) -> Result<(), SnmError> {
env_logger::init();

let args_all: Vec<String> = env::args().collect();
Expand All @@ -34,20 +31,8 @@ pub async fn load_package_manage_shim(prefix: &str, bin_name: &str) -> Result<St

let restricted_list = vec!["install", "i", "run"];

// if restricted_list.contains(&command.as_str()) {
// if package_manager.name != prefix {
// // error not match
// } else {
// // ok run
// }
// } else {
// // npx yarn
// // find default v by bin_name
// // if default bin_name not found redirect to node bin
// }

let version = match snm_config.get_runtime_package_manager() {
Some(package_manager) if package_manager.name == prefix => package_manager.version,
Some(package_manager) if package_manager.name == prefix => Some(package_manager.version),
Some(package_manager) if restricted_list.contains(&command.as_str()) => {
return Err(SnmError::NotMatchPackageManagerError {
raw_command: args_all.join(" "),
Expand All @@ -58,13 +43,16 @@ pub async fn load_package_manage_shim(prefix: &str, bin_name: &str) -> Result<St
_ => snm_package_manage.get_default_version()?,
};

let binary_dir_string = ensure_binary_path(snm_package_manage, &version).await?;
let binary_dir_string = match version {
Some(v) => ensure_binary_path(snm_package_manage, &v).await?,
None => "".to_string(),
};

let node_dir = get_node_bin_dir().await?;

exec_cli(vec![binary_dir_string, node_dir], bin_name, &args)?;

Ok(version)
Ok(())
}

pub async fn get_node_bin_dir() -> Result<String, SnmError> {
Expand All @@ -75,11 +63,14 @@ pub async fn get_node_bin_dir() -> Result<String, SnmError> {
let snm_node: &dyn AtomTrait = &SnmNode::new(snm_config.clone());

let version = match snm_config.get_runtime_node_version() {
Some(node_version) => node_version,
Some(node_version) => Some(node_version),
None => snm_node.get_default_version()?,
};

let binary_dir_string = ensure_binary_path(snm_node, &version).await?;
let binary_dir_string = match version {
Some(v) => ensure_binary_path(snm_node, &v).await?,
None => return Err(SnmError::NotFoundValidVersion),
};

Ok(binary_dir_string)
}
Expand All @@ -96,13 +87,20 @@ pub async fn load_node_shim(bin_name: &str) -> Result<(), SnmError> {
let snm_node: &dyn AtomTrait = &SnmNode::new(snm_config.clone());

let version = match snm_config.get_runtime_node_version() {
Some(node_version) => node_version,
Some(node_version) => Some(node_version),
None => snm_node.get_default_version()?,
};

let binary_dir_string = ensure_binary_path(snm_node, &version).await?;
match version {
Some(v) => {
let binary_dir_string = ensure_binary_path(snm_node, &v).await?;

exec_cli(vec![binary_dir_string], bin_name, &args)?;
exec_cli(vec![binary_dir_string], bin_name, &args)?;
}
None => {
return Err(SnmError::NotFoundValidVersion);
}
}

Ok(())
}

0 comments on commit 257cfff

Please sign in to comment.