Skip to content

Commit

Permalink
(node) node version install and usev in linux
Browse files Browse the repository at this point in the history
  • Loading branch information
anusikh committed Jul 17, 2024
1 parent 43a25ce commit be3a86e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 12 deletions.
26 changes: 23 additions & 3 deletions src/commands/node/install.rs
Original file line number Diff line number Diff line change
@@ -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")]
Expand All @@ -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")
Expand All @@ -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);

Expand Down
21 changes: 16 additions & 5 deletions src/utils/env_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub fn read_versions() -> Result<(), Box<dyn std::error::Error>> {
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());
}
Expand Down Expand Up @@ -42,10 +42,12 @@ pub fn read_versions_node() -> Result<(), Box<dyn std::error::Error>> {
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("<a href=\"v") {
if let Some(version) = parse_version(&line) {
let num_ver: u32 = version.split('.').next().unwrap().parse().unwrap();
// NOTE: We are supporting versions above 16
Expand All @@ -61,6 +63,11 @@ pub fn read_versions_node() -> Result<(), Box<dyn std::error::Error>> {
}
}
}

if !display_vec.is_empty() {
println!("nodejs v{}", curr.to_string());
println!("{:?}", display_vec);
}
} else {
println!(
"couldn't connect to nodejs.org: {}",
Expand All @@ -72,12 +79,16 @@ pub fn read_versions_node() -> Result<(), Box<dyn std::error::Error>> {
}

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<String, Box<dyn std::error::Error>> {
pub fn get_download_link_node(
version: &str,
os: &str,
arch: &str,
) -> Result<String, Box<dyn std::error::Error>> {
let os_mapped = match os {
"windows" => "win",
"macos" => "darwin",
Expand Down
12 changes: 8 additions & 4 deletions src/utils/file_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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",
_ => "",
};
Expand All @@ -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()),
_ => "",
};
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit be3a86e

Please sign in to comment.