Skip to content

Commit

Permalink
Update bin.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Nov 27, 2023
1 parent 983047f commit 3d65e6a
Showing 1 changed file with 22 additions and 89 deletions.
111 changes: 22 additions & 89 deletions crates/cli/src/commands/bin.rs
Original file line number Diff line number Diff line change
@@ -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<BinArgs>) {
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::<NodeTool>().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));
}
}

0 comments on commit 3d65e6a

Please sign in to comment.