From 3d65e6abfdfe1b0e232177477cd47c4bc6102cf5 Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Mon, 27 Nov 2023 11:50:47 -0800 Subject: [PATCH] Update bin. --- crates/cli/src/commands/bin.rs | 111 +++++++-------------------------- 1 file changed, 22 insertions(+), 89 deletions(-) diff --git a/crates/cli/src/commands/bin.rs b/crates/cli/src/commands/bin.rs index cd716728f24..d78593f4793 100644 --- a/crates/cli/src/commands/bin.rs +++ b/crates/cli/src/commands/bin.rs @@ -1,99 +1,32 @@ -use clap::{Args, ValueEnum}; -use moon_config::PlatformType; -use moon_node_tool::NodeTool; -use moon_platform::PlatformManager; +use clap::Args; +use miette::IntoDiagnostic; use moon_terminal::safe_exit; -use moon_tool::Tool; +use moon_tool::{get_proto_paths, prepend_path_env_var}; +use proto_core::ProtoEnvironment; use starbase::system; - -#[derive(ValueEnum, Clone, Debug)] -#[value(rename_all = "lowercase")] -pub enum BinTool { - // JavaScript - Bun, - Node, - Npm, - Pnpm, - Yarn, - // Rust - Rust, -} +use tokio::process::Command; #[derive(Args, Clone, Debug)] pub struct BinArgs { - #[arg(value_enum, help = "The tool to query")] - tool: BinTool, -} - -enum BinExitCodes { - NotConfigured = 1, - NotInstalled = 2, -} - -fn is_installed(tool: &dyn Tool) { - // TODO - // if let Some(shim_path) = tool.get_shim_path() { - // println!("{}", shim_path.display()); - // } else { - // match tool.get_bin_path() { - // Ok(path) => { - // println!("{}", path.display()); - // } - // Err(_) => { - // safe_exit(BinExitCodes::NotInstalled as i32); - // } - // } - // } -} - -fn not_configured() -> ! { - safe_exit(BinExitCodes::NotConfigured as i32); + #[arg(help = "The tool to query")] + tool: String, } #[system] pub async fn bin(args: ArgsRef) { - match &args.tool { - BinTool::Bun => { - let bun = PlatformManager::read().get(PlatformType::Bun)?.get_tool()?; - - is_installed(*bun); - } - BinTool::Node => { - let node = PlatformManager::read() - .get(PlatformType::Node)? - .get_tool()?; - - is_installed(*node); - } - BinTool::Npm | BinTool::Pnpm | BinTool::Yarn => { - let node = PlatformManager::read() - .get(PlatformType::Node)? - .get_tool()? - .as_any(); - let node = node.downcast_ref::().unwrap(); - - match &args.tool { - BinTool::Npm => match node.get_npm() { - Ok(npm) => is_installed(npm), - Err(_) => not_configured(), - }, - BinTool::Pnpm => match node.get_pnpm() { - Ok(pnpm) => is_installed(pnpm), - Err(_) => not_configured(), - }, - BinTool::Yarn => match node.get_yarn() { - Ok(yarn) => is_installed(yarn), - Err(_) => not_configured(), - }, - _ => {} - }; - } - BinTool::Rust => { - let rust = PlatformManager::read() - .get(PlatformType::Rust)? - .get_tool()?; - - is_installed(*rust); - } - }; + let proto = ProtoEnvironment::new()?; + + let result = Command::new("proto") + .arg("bin") + .arg(&args.tool) + .env("PATH", prepend_path_env_var(get_proto_paths(&proto))) + .spawn() + .into_diagnostic()? + .wait() + .await + .into_diagnostic()?; + + if !result.success() { + safe_exit(result.code().unwrap_or(1)); + } }