Skip to content

Commit

Permalink
review package_manager logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ityuany committed Jul 12, 2024
1 parent 83720e9 commit f56a870
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 18 deletions.
13 changes: 12 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@
"preLaunchTask": "cargo build",
"stopOnEntry": false,
"sourceLanguages": ["rust"]
}
},
{
"name": "debug npm -v",
"type": "lldb",
"request": "launch",
"program": "${workspaceFolder}/target/debug/npx",
"args": ["@napi-rs/cli","new"],
"cwd": "${workspaceFolder}",
"preLaunchTask": "cargo build",
"stopOnEntry": false,
"sourceLanguages": ["rust"]
}
]
}
52 changes: 35 additions & 17 deletions crates/snm_shim/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,50 @@ pub async fn load_package_manage_shim(prefix: &str, bin_name: &str) -> Result<()
let snm_package_manage: &dyn AtomTrait =
&SnmPackageManager::from_prefix(prefix, snm_config.clone());

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", "i", "run"];

let version = match snm_config.get_runtime_package_manager() {
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(" "),
expected: package_manager.name.clone(),
actual: prefix.to_string(),
});
let get_default_version = || -> Result<String, SnmError> {
if snm_config.get_strict() && restricted_list.contains(&command.as_str()) {
Err(SnmError::NotFoundValidVersion)
} else {
let (_, version) = snm_package_manage.read_runtime_dir_name_vec()?;
version.ok_or(SnmError::NotFoundValidVersion)
}
_ => snm_package_manage.get_default_version()?,
};

let binary_dir_string = match version {
Some(v) => ensure_binary_path(snm_package_manage, &v).await?,
None => "".to_string(),
let version = if let Some(package_manager) = snm_config.get_runtime_package_manager() {
if package_manager.name == prefix {
Ok(package_manager.version)
} else {
if restricted_list.contains(&command.as_str()) {
Err(SnmError::NotMatchPackageManagerError {
raw_command: args_all.join(" "),
expected: package_manager.name.clone(),
actual: prefix.to_string(),
})
} else {
get_default_version()
}
}
} else {
let default_version = get_default_version();
snm_config
.get_snm_package_json()
.and_then(|item| item.package_manager)
.map(|item| Ok(item.version))
.unwrap_or(default_version)
};

let node_dir = get_node_bin_dir().await?;

exec_cli(vec![binary_dir_string, node_dir], bin_name, &args)?;
let mut bin_dirs = vec![node_dir];

if let Ok(v) = version {
let binary_dir_string = ensure_binary_path(snm_package_manage, &v).await?;
bin_dirs.insert(0, binary_dir_string);
}

exec_cli(bin_dirs, bin_name, &args)?;

Ok(())
}
Expand Down

0 comments on commit f56a870

Please sign in to comment.