diff --git a/crates/cli/src/execute_cli.rs b/crates/cli/src/execute_cli.rs index ade70482..33c35b22 100644 --- a/crates/cli/src/execute_cli.rs +++ b/crates/cli/src/execute_cli.rs @@ -4,7 +4,7 @@ use snm_core::traits::atom::AtomTrait; use snm_core::model::dispatch_manage::DispatchManage; use snm_ni::trait_transform::IArgs; -use snm_ni::{CommandArgsCreatorTrait, NpmArgsTransform, PnpmArgsTransform}; +use snm_ni::{CommandArgsCreatorTrait, NpmArgsTransform, PnpmArgsTransform, YarnArgsTransform}; use snm_node::snm_node::SnmNode; use snm_package_json::parse_package_json; use snm_package_manager::snm_package_manager::SnmPackageManager; @@ -73,9 +73,9 @@ pub async fn execute_cli(cli: SnmCli, snm_config: SnmConfig) -> Result<(), SnmEr | SnmCommands::X(_) | SnmCommands::E(_) | SnmCommands::R(_) => { - let name = match parse_package_json(&snm_config.get_workspace()?)? { + let package_manager = match parse_package_json(&snm_config.get_workspace()?)? { Some(package_json) => match package_json.package_manager { - Some(package_manager) => package_manager.name, + Some(package_manager) => package_manager, None => { panic!("No package manager found in the workspace.") } @@ -85,10 +85,13 @@ pub async fn execute_cli(cli: SnmCli, snm_config: SnmConfig) -> Result<(), SnmEr } }; - let transform: Box = match name.as_str() { + let transform: Box = match package_manager.name.as_str() { "npm" => Box::new(NpmArgsTransform {}), "pnpm" => Box::new(PnpmArgsTransform {}), - _ => panic!("Unsupported package manager"), + "yarn" => Box::new(YarnArgsTransform { + version: package_manager.version.to_string(), + }), + _ => panic!("Unsupported package manager: {}", &package_manager.name), }; let args = match cli.command { @@ -105,7 +108,7 @@ pub async fn execute_cli(cli: SnmCli, snm_config: SnmConfig) -> Result<(), SnmEr _ => unreachable!("unreachable"), }; - exec_cli(name, args); + exec_cli(package_manager.name, args); } SnmCommands::FigSpec => { diff --git a/crates/snm_ni/Cargo.toml b/crates/snm_ni/Cargo.toml index 8316000d..f3e50947 100644 --- a/crates/snm_ni/Cargo.toml +++ b/crates/snm_ni/Cargo.toml @@ -8,4 +8,5 @@ edition = "2021" [dependencies] clap_complete = "4.5.1" clap_complete_fig = "4.5.0" -clap = { version = "4.5.1", features = ["derive"] } \ No newline at end of file +clap = { version = "4.5.1", features = ["derive"] } +semver = "1.0.21" \ No newline at end of file diff --git a/crates/snm_ni/src/lib.rs b/crates/snm_ni/src/lib.rs index 76828753..eaac0712 100644 --- a/crates/snm_ni/src/lib.rs +++ b/crates/snm_ni/src/lib.rs @@ -1,7 +1,9 @@ pub mod npm_transform; pub mod pnpm_transform; pub mod trait_transform; +pub mod yarn_transform; pub use npm_transform::NpmArgsTransform; pub use pnpm_transform::PnpmArgsTransform; pub use trait_transform::CommandArgsCreatorTrait; +pub use yarn_transform::YarnArgsTransform; diff --git a/crates/snm_ni/src/yarn_transform.rs b/crates/snm_ni/src/yarn_transform.rs new file mode 100644 index 00000000..693c0ad6 --- /dev/null +++ b/crates/snm_ni/src/yarn_transform.rs @@ -0,0 +1,86 @@ +use semver::{Version, VersionReq}; + +use crate::trait_transform::{AArgs, CommandArgsCreatorTrait, DArgs, EArgs, IArgs, RArgs, XArgs}; + +pub struct YarnArgsTransform { + pub version: String, +} + +impl YarnArgsTransform { + fn is_greater_than_1(&self) -> bool { + let req = VersionReq::parse(">1").unwrap(); + let version = Version::parse(&self.version).unwrap(); + req.matches(&version) + } +} + +impl CommandArgsCreatorTrait for YarnArgsTransform { + fn i(&self, args: IArgs) -> Vec { + if self.is_greater_than_1() { + let mut process_args = vec!["install".to_string()]; + if args.frozen_lockfile { + process_args.push("--immutable".to_string()); + } + + process_args + } else { + let mut process_args = vec!["install".to_string()]; + if args.frozen_lockfile { + process_args.push("--frozen-lockfile".to_string()); + } + + process_args + } + } + + fn a(&self, args: AArgs) -> Vec { + let mut process_args = vec!["add".to_string(), args.package_spec]; + if args.save_prod { + process_args.push("--save".to_string()); + } else if args.save_dev { + process_args.push("--save-dev".to_string()); + } else if args.save_optional { + process_args.push("--save-optional".to_string()); + } else if args.save_exact { + process_args.push("--save-exact".to_string()); + } else if args.save_peer { + process_args.push("--save-peer".to_string()); + } else if args.global { + process_args.push("--global".to_string()); + } + process_args + } + + fn d(&self, args: DArgs) -> Vec { + let process_args = vec!["remove".to_string(), args.package_spec]; + process_args + } + + fn x(&self, args: XArgs) -> Vec { + if self.is_greater_than_1() { + let mut process_args = vec!["dlx".to_string()]; + process_args.append(&mut args.package_spec.clone()); + process_args + } else { + let err_msg = format!(" {} Unsupported command: yarn dlx ", self.version); + panic!("{err_msg}"); + } + } + + fn e(&self, args: EArgs) -> Vec { + if self.is_greater_than_1() { + let mut process_args: Vec = vec!["exec".to_string()]; + process_args.append(&mut args.package_spec.clone()); + process_args + } else { + let err_msg = format!(" {} Unsupported command: yarn exec ", self.version); + panic!("{err_msg}"); + } + } + + fn r(&self, args: RArgs) -> Vec { + let mut process_args: Vec = vec!["run".to_string()]; + process_args.append(&mut args.args.clone()); + process_args + } +} diff --git a/crates/snm_utils/src/exec.rs b/crates/snm_utils/src/exec.rs index e08a04bb..49108226 100644 --- a/crates/snm_utils/src/exec.rs +++ b/crates/snm_utils/src/exec.rs @@ -16,7 +16,12 @@ where .spawn() .and_then(|process| process.wait_with_output()); - if let Err(_) = output { + if let Ok(res) = output { + if !res.status.success() { + let err_msg = format!("snm proxy execute failed : {:?}", res); + panic!("{err_msg}"); + } + } else { panic!("snm proxy execute failed"); } } diff --git a/crates/snm_utils/src/snm_error.rs b/crates/snm_utils/src/snm_error.rs index be35271d..a0b3c39b 100644 --- a/crates/snm_utils/src/snm_error.rs +++ b/crates/snm_utils/src/snm_error.rs @@ -52,6 +52,9 @@ pub enum SnmError { #[error("Parse package manager error , raw is {0}")] ParsePackageManagerError(String), + + #[error("Unsupported command: {raw_command}")] + UnsupportedCommandError { raw_command: String }, } pub fn friendly_error_message(error: SnmError) { @@ -233,6 +236,16 @@ pub fn friendly_error_message(error: SnmError) { file_path.to_string_lossy().bold().red() ); } + SnmError::UnsupportedCommandError { raw_command } => { + eprintln!( + r##" + 👹 You exec command is unsupported + + {} + "##, + raw_command + ); + } SnmError::HttpStatusCodeUnOk | SnmError::NotFoundPackageJsonError(_) | SnmError::GetWorkspaceError