Skip to content

Commit

Permalink
review snm impl
Browse files Browse the repository at this point in the history
  • Loading branch information
ityuany committed Jul 25, 2024
1 parent 9a7b6c7 commit dee0af9
Showing 1 changed file with 30 additions and 36 deletions.
66 changes: 30 additions & 36 deletions crates/cli/src/execute_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,41 @@ use snm_atom::node_atom::NodeAtom;
use snm_config::SnmConfig;
use snm_ni::trait_transform::IArgs;
use snm_ni::{CommandArgsCreatorTrait, NpmArgsTransform, PnpmArgsTransform, YarnArgsTransform};
use snm_utils::exec::exec_cli;
use snm_utils::snm_error::SnmError;
use std::process::{Command, Stdio};

use crate::fig::fig_spec_impl;
use crate::manage_command::ManageCommands;
use crate::node_manager::node_manager::{ListArgs, ListRemoteArgs, NodeManager};
use crate::snm_command::SnmCommands;
use crate::SnmCli;

fn create_transform(name: &str, version: &str) -> Box<dyn CommandArgsCreatorTrait> {
match name {
"npm" => Box::new(NpmArgsTransform {}),
"pnpm" => Box::new(PnpmArgsTransform {}),
"yarn" => Box::new(YarnArgsTransform {
version: version.to_string(),
}),
_ => panic!("Unsupported package manager: {}", name),
}
}

fn handle_command(transform: &dyn CommandArgsCreatorTrait, command: SnmCommands) -> Vec<String> {
match command {
// snm command start
SnmCommands::I(args) => transform.i(args),
SnmCommands::C(_) => transform.i(IArgs {
frozen_lockfile: true,
}),
SnmCommands::A(args) => transform.a(args),
SnmCommands::X(args) => transform.x(args),
SnmCommands::E(args) => transform.e(args),
SnmCommands::R(args) => transform.r(args),
_ => unreachable!("unreachable"),
}
}

pub async fn execute_cli(cli: SnmCli, snm_config: SnmConfig) -> Result<(), SnmError> {
match cli.command {
// manage start
Expand Down Expand Up @@ -50,43 +76,11 @@ pub async fn execute_cli(cli: SnmCli, snm_config: SnmConfig) -> Result<(), SnmEr
.package_manager
.ok_or(SnmError::NotFondPackageManagerConfigError {})?;

let transform: Box<dyn CommandArgsCreatorTrait> = match package_manager.name.as_str() {
"npm" => Box::new(NpmArgsTransform {}),
"pnpm" => Box::new(PnpmArgsTransform {}),
"yarn" => Box::new(YarnArgsTransform {
version: package_manager.version.to_string(),
}),
_ => panic!("Unsupported package manager: {}", &package_manager.name),
};

let args = match cli.command {
// snm command start
SnmCommands::I(args) => transform.i(args),
SnmCommands::C(_) => transform.i(IArgs {
frozen_lockfile: true,
}),
SnmCommands::A(args) => transform.a(args),
SnmCommands::X(args) => transform.x(args),
SnmCommands::E(args) => transform.e(args),
SnmCommands::R(args) => transform.r(args),
_ => unreachable!("unreachable"),
};
let transform = create_transform(&package_manager.name, &package_manager.version);

let output = Command::new(package_manager.name)
.args(args)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.stdin(Stdio::inherit())
.spawn()
.and_then(|process| process.wait_with_output())?;

if !output.status.success() {
return Err(SnmError::SNMBinaryProxyFail {
stderr: String::from_utf8_lossy(&output.stderr).to_string(),
});
}
let args = handle_command(&*transform.as_ref(), cli.command);

print!("{}", String::from_utf8_lossy(&output.stdout).to_string());
exec_cli(vec![], package_manager.name, args)?;
}

SnmCommands::FigSpec => {
Expand Down

0 comments on commit dee0af9

Please sign in to comment.