Add init command

feature/lookup-installed
trivernis 2 years ago
parent 3ad9790bc7
commit 97d73c9e36
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -40,6 +40,10 @@ pub enum Command {
/// Executes the given version specific node executable /// Executes the given version specific node executable
#[command()] #[command()]
Exec(ExecArgs), Exec(ExecArgs),
/// Initializes nenv directories and installs a default node version
#[command()]
Init,
} }
#[derive(Clone, Debug, Parser)] #[derive(Clone, Debug, Parser)]

@ -46,6 +46,7 @@ async fn main() -> Result<()> {
} }
args::Command::Refresh => nenv.refresh().await, args::Command::Refresh => nenv.refresh().await,
args::Command::ListVersions => nenv.list_versions().await, args::Command::ListVersions => nenv.list_versions().await,
args::Command::Init => nenv.init_nenv().await,
_ => xkcd_unreachable!(), _ => xkcd_unreachable!(),
}?; }?;

@ -1,8 +1,8 @@
use std::ffi::OsString; use std::{ffi::OsString, str::FromStr};
use crate::{ use crate::{
config::ConfigAccess, config::ConfigAccess,
consts::VERSION_FILE_PATH, consts::{BIN_DIR, VERSION_FILE_PATH},
error::VersionError, error::VersionError,
mapper::Mapper, mapper::Mapper,
repository::{NodeVersion, Repository}, repository::{NodeVersion, Repository},
@ -10,6 +10,7 @@ use crate::{
version_detection::{self, VersionDetector}, version_detection::{self, VersionDetector},
}; };
use crossterm::style::Stylize; use crossterm::style::Stylize;
use dialoguer::{theme::ColorfulTheme, Input, Select};
use miette::{IntoDiagnostic, Result}; use miette::{IntoDiagnostic, Result};
use tokio::fs; use tokio::fs;
@ -136,6 +137,39 @@ impl Nenv {
Ok(()) Ok(())
} }
/// Initializes nenv and prompts for a default version.
pub async fn init_nenv(&self) -> Result<()> {
let items = vec!["latest", "lts", "custom"];
let selection = Select::with_theme(&ColorfulTheme::default())
.with_prompt("Select a default node version")
.items(&items)
.default(0)
.interact()
.into_diagnostic()?;
let version = if items[selection] == "custom" {
let version_string: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Enter a version number: ")
.interact_text()
.into_diagnostic()?;
NodeVersion::from_str(&version_string).unwrap()
} else {
NodeVersion::from_str(items[selection]).unwrap()
};
self.repo.install_version(&version).await?;
println!("{}", "Initialized!".green());
println!(
"{}\n {}\n{}",
"Make sure to add".bold(),
BIN_DIR.to_string_lossy().yellow(),
"to your PATH environment variables.".bold()
);
Ok(())
}
/// Persits all changes made that aren't written to the disk yet /// Persits all changes made that aren't written to the disk yet
#[tracing::instrument(level = "debug", skip(self))] #[tracing::instrument(level = "debug", skip(self))]
pub async fn persist(&self) -> Result<()> { pub async fn persist(&self) -> Result<()> {

Loading…
Cancel
Save