You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nenv/src/lib.rs

75 lines
1.9 KiB
Rust

use std::ffi::OsString;
use crossterm::style::Stylize;
use mapper::Mapper;
use repository::{config::Config, NodeVersion, Repository};
mod consts;
pub mod error;
pub mod mapper;
pub mod repository;
mod utils;
mod web_api;
use dialoguer::Confirm;
use error::Result;
pub async fn install_version(version: NodeVersion) -> Result<()> {
let repo = get_repository().await?;
if repo.is_installed(&version).await? {
if !Confirm::new()
.with_prompt("The version {version} is already installed. Reinstall?")
.default(false)
.interact()
.unwrap()
{
return Ok(());
}
}
repo.install_version(&version).await?;
println!("Installed {}", version.to_string().bold());
Ok(())
}
pub async fn use_version(version: NodeVersion) -> Result<()> {
let mut mapper = get_mapper().await?;
if !mapper.repository().is_installed(&version).await?
&& Confirm::new()
.with_prompt(format!(
"The version {version} is not installed. Do you want to install it?"
))
.default(false)
.interact()
.unwrap()
{
mapper.repository().install_version(&version).await?;
}
mapper.use_version(&version).await?;
println!("Now using {}", version.to_string().bold());
Ok(())
}
pub async fn exec(command: String, args: Vec<OsString>) -> Result<i32> {
let mapper = get_mapper().await?;
let active_version = mapper.active_version();
if !mapper.repository().is_installed(active_version).await? {
mapper.repository().install_version(&active_version).await?;
}
let exit_status = mapper.exec(command, args).await?;
Ok(exit_status.code().unwrap_or(0))
}
async fn get_repository() -> Result<Repository> {
Repository::init(Config::load().await?).await
}
async fn get_mapper() -> Result<Mapper> {
Ok(Mapper::new(get_repository().await?))
}