Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
ityuany committed Jun 25, 2024
2 parents 7e224b7 + f7de549 commit 3a9f1e7
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 8 deletions.
15 changes: 9 additions & 6 deletions crates/cli/src/execute_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.")
}
Expand All @@ -85,10 +85,13 @@ pub async fn execute_cli(cli: SnmCli, snm_config: SnmConfig) -> Result<(), SnmEr
}
};

let transform: Box<dyn CommandArgsCreatorTrait> = match name.as_str() {
let transform: Box<dyn CommandArgsCreatorTrait> = 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 {
Expand All @@ -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 => {
Expand Down
3 changes: 2 additions & 1 deletion crates/snm_ni/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
clap = { version = "4.5.1", features = ["derive"] }
semver = "1.0.21"
2 changes: 2 additions & 0 deletions crates/snm_ni/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
86 changes: 86 additions & 0 deletions crates/snm_ni/src/yarn_transform.rs
Original file line number Diff line number Diff line change
@@ -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<String> {
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<String> {
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<String> {
let process_args = vec!["remove".to_string(), args.package_spec];
process_args
}

fn x(&self, args: XArgs) -> Vec<String> {
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<String> {
if self.is_greater_than_1() {
let mut process_args: Vec<String> = 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<String> {
let mut process_args: Vec<String> = vec!["run".to_string()];
process_args.append(&mut args.args.clone());
process_args
}
}
7 changes: 6 additions & 1 deletion crates/snm_utils/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
13 changes: 13 additions & 0 deletions crates/snm_utils/src/snm_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3a9f1e7

Please sign in to comment.