From be3a86e6b3c292e95c05f5d010bbfe6cef8f56b0 Mon Sep 17 00:00:00 2001 From: anusikh Date: Wed, 17 Jul 2024 11:34:01 +0530 Subject: [PATCH] (node) node version install and usev in linux --- src/commands/node/install.rs | 26 +++++++++++++++++++++++--- src/utils/env_ops.rs | 21 ++++++++++++++++----- src/utils/file_utils.rs | 12 ++++++++---- 3 files changed, 47 insertions(+), 12 deletions(-) diff --git a/src/commands/node/install.rs b/src/commands/node/install.rs index 5128084..24c3a28 100644 --- a/src/commands/node/install.rs +++ b/src/commands/node/install.rs @@ -1,6 +1,6 @@ use crate::utils::env_ops::get_download_link_node; use crate::utils::file_utils::{ - check_node_exists, create_node_dir, extract_tarball_macos, run_command, + check_node_exists, create_node_dir, extract_tarball_linux, extract_tarball_macos, run_command, }; #[cfg(target_os = "windows")] @@ -19,7 +19,27 @@ fn install_util(version: String, link: String) { fn install_util(version: String, link: String) { match check_node_exists(&version) { false => { - println!("{}", link); + create_node_dir(&version); + + let temp_directory = format!("/tmp/{}.tar.gz", version); + + if std::path::Path::new(&temp_directory).exists() { + println!("fetching tarball from cache successful"); + extract_tarball_linux(&version, "node"); + } else { + println!("fetching tarball..."); + let output = run_command("/usr/bin/curl", vec!["-o", &temp_directory, &link]); + + if output.status.success() { + println!("fetching tarball successful "); + extract_tarball_linux(&version, "node"); + } else { + println!( + "fetching tarball failed: {} ", + String::from_utf8_lossy(&output.stderr) + ); + } + } } true => { println!("node version exists already, if it doesn't run the clean command") @@ -31,7 +51,7 @@ fn install_util(version: String, link: String) { fn install_util(version: String, link: String) { match check_node_exists(&version) { false => { - create_node_dir(); + create_node_dir(&version); let temp_directory = format!("/tmp/{}.tar.gz", version); diff --git a/src/utils/env_ops.rs b/src/utils/env_ops.rs index d5adaeb..87fe56f 100644 --- a/src/utils/env_ops.rs +++ b/src/utils/env_ops.rs @@ -6,7 +6,7 @@ pub fn read_versions() -> Result<(), Box> { let available_versions = constants::AVAILABLE_VERSIONS; let versions: Vec<&str> = available_versions.split(',').collect(); - println!("Availaible Versions:"); + println!("available versions:"); for version in versions { println!("{}", version.to_ascii_lowercase()); } @@ -42,10 +42,12 @@ pub fn read_versions_node() -> Result<(), Box> { let output = run_command("curl", vec!["https://nodejs.org/dist/"]); let mut curr = 16; let mut display_vec = Vec::new(); + + println!("available versions:"); if output.status.success() { let body = String::from_utf8_lossy(&output.stdout); for line in body.lines() { - if line.contains(">v") && line.contains("/<") { + if line.contains(" Result<(), Box> { } } } + + if !display_vec.is_empty() { + println!("nodejs v{}", curr.to_string()); + println!("{:?}", display_vec); + } } else { println!( "couldn't connect to nodejs.org: {}", @@ -72,12 +79,16 @@ pub fn read_versions_node() -> Result<(), Box> { } fn parse_version(line: &str) -> Option<&str> { - let start = line.find(">v")? + 2; - let end = line.find("/<")?; + let start = line.find("v")? + 1; + let end = line.find("/\">")?; line.get(start..end) } -pub fn get_download_link_node(version: &str, os: &str, arch: &str) -> Result> { +pub fn get_download_link_node( + version: &str, + os: &str, + arch: &str, +) -> Result> { let os_mapped = match os { "windows" => "win", "macos" => "darwin", diff --git a/src/utils/file_utils.rs b/src/utils/file_utils.rs index b5ab420..a6df6d4 100644 --- a/src/utils/file_utils.rs +++ b/src/utils/file_utils.rs @@ -99,8 +99,8 @@ pub fn create_maven_dir() { fs::create_dir_all(new_dir).unwrap(); } -pub fn create_node_dir() { - let new_dir = format!("{}/.jvem/node_versions", *HOME_DIR); +pub fn create_node_dir(name: &str) { + let new_dir = format!("{}/.jvem/node_versions/{}", *HOME_DIR, name); fs::create_dir_all(new_dir).unwrap(); } @@ -163,7 +163,7 @@ pub fn clean_jvem(command: &str) { pub fn extract_tarball_linux(name: &str, command: &str) { let tar_location = match command { - "java" => &find_file_in_dir("/tmp", name), + "java" | "node" => &find_file_in_dir("/tmp", name), "maven" => "/tmp/maven.tar.gz", _ => "", }; @@ -174,6 +174,11 @@ pub fn extract_tarball_linux(name: &str, command: &str) { get_home_dir(), String::from(name) ), + "node" => &format!( + "{}/.jvem/node_versions/{}", + get_home_dir(), + String::from(name) + ), "maven" => &format!("{}/.jvem/maven", get_home_dir()), _ => "", }; @@ -221,7 +226,6 @@ pub fn extract_tarball_macos(name: &str, command: &str) { }; let res = fs::create_dir_all(tmp_path); - println!("{}", output_path); match res { Ok(_) => { let tarball_status = run_command(