Skip to content

Commit

Permalink
review shim
Browse files Browse the repository at this point in the history
  • Loading branch information
ityuany committed Feb 3, 2025
1 parent b5d659c commit c1dc094
Showing 1 changed file with 20 additions and 56 deletions.
76 changes: 20 additions & 56 deletions crates/snm_shim/src/pm_shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ use anyhow::bail;
use colored::Colorize;
use snm_config::snm_config::SnmConfig;
use snm_node::SNode;
use snm_pm::pm::PackageManager;
use snm_pm::{package_json::PJson, pm::SPM};
use snm_utils::{exec::exec_cli, trace_if};
use tracing::trace;

const NPM_COMMANDS: [&str; 2] = ["npm", "npx"];

pub async fn load_pm(snm_config: &SnmConfig, args: &Vec<String>) -> anyhow::Result<()> {
let [bin_name, command_args @ ..] = args.as_slice() else {
bail!(
Expand All @@ -27,53 +25,34 @@ args: {:?}"#,
);
});

let node_bin_dir = SNode::try_from(&snm_config)?.get_bin().await?;
let node_bin_dir = SNode::try_from(&snm_config)?
.ensure_node_and_return_dir()
.await?;

let node_str = String::from("node");
let node_bin_dir_str = node_bin_dir.to_string_lossy().into_owned();

let paths = vec![node_bin_dir_str];

// let command_args = args[1..].to_vec();

if let Ok(pm_bin_file) = get_package_manager_bin(&args, &snm_config).await {
let args = [
&[node_str, pm_bin_file.to_string_lossy().into_owned()],
command_args,
]
.concat();
exec_cli(&args, &paths, true)?;
} else {
if !is_npm_command(bin_name) {
if PJson::exists(&snm_config.workspace) {
if SPM::exists(&snm_config.workspace)? {
let f = get_package_manager_bin(args, &snm_config).await?;
let args = [&[node_str, f.to_string_lossy().into_owned()], command_args].concat();
exec_cli(&args, &paths, true)?;
} else {
#[cfg(target_os = "windows")]
let bin_name = format!("{}.cmd", bin_name);

#[cfg(not(target_os = "windows"))]
let bin_name = bin_name.to_string();

let pm_bin_file = node_bin_dir.join(bin_name);

trace_if!(|| {
trace!("Default bin file: {:?}", pm_bin_file);
});

let mut exec_args = vec![pm_bin_file.to_string_lossy().to_string()];
exec_args.extend(command_args.iter().map(|s| s.to_string()));

exec_cli(&exec_args, &paths, true)?;
if snm_config.strict {
bail!("You have not correctly configured packageManager in package.json");
} else {
exec_cli(&args, &paths, true)?;
}
}
} else {
exec_cli(&args, &paths, true)?;
}

Ok(())
}

// 删除 build_bin_path 函数
fn is_npm_command(command: &str) -> bool {
NPM_COMMANDS.contains(&command)
}

async fn get_package_manager_bin(
args: &Vec<String>,
config: &SnmConfig,
Expand All @@ -86,7 +65,9 @@ args: {:?}"#,
);
};

let pm = PackageManager::try_from_env(config)?;
let spm = SPM::try_from(&config.workspace, config)?;

let pm = &spm.pm;

if pm.name() != bin_name && config.restricted_list.contains(command) {
bail!(
Expand All @@ -97,26 +78,9 @@ args: {:?}"#,
);
}

let metadata = pm.metadata();

let mut dir = config
.node_modules_dir
.join(pm.full_name())
.join(pm.version());

let file = dir.join("package.json");

trace_if!(|| {
trace!("File: {:?}", file);
});

if !file.try_exists()? {
dir = snm_pm::downloader::PackageManagerDownloader::new(metadata, config)
.download_pm(pm.version())
.await?;
}
let dir = spm.ensure_bin_dir().await?;

let json = snm_pm::package_json::PackageJson::from(dir)?;
let json = PJson::from(dir)?;

let file = json.get_bin_with_name(bin_name)?;

Expand Down

0 comments on commit c1dc094

Please sign in to comment.