Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 133 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ itertools = "0.10.5"
node-semver = "2.1.0"
serde = { version = "1.0.151", features = ["derive"] }
serde_json = "1.0.91"
spectral = "0.6.0"
ureq = { version = "2.5.0", features = ["json"] }

[target.'cfg(unix)'.dependencies]
Expand Down
8 changes: 4 additions & 4 deletions src/archives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub fn extract_archive(bytes: Vec<u8>, path: &Path) -> Result<()> {
let mut archive = Archive::new(tar);

let version_dir_path = path.to_owned();
create_dir_all(version_dir_path.to_owned()).expect("fuck");
create_dir_all(&version_dir_path).expect("fuck");

println!("Extracting...");

Expand All @@ -72,11 +72,11 @@ pub fn extract_archive(bytes: Vec<u8>, path: &Path) -> Result<()> {
.map_err(anyhow::Error::from)?
.filter_map(|e| e.ok())
.map(|mut entry| -> Result<Unpacked> {
let file_path = entry.path()?.to_owned();
let file_path = entry.path()?;
let file_path = file_path.to_str().unwrap();

let new_path: PathBuf = if let Some(index) = file_path.find('/') {
path.to_owned().join(file_path[index + 1..].to_owned())
path.to_owned().join(&file_path[index + 1..])
} else {
// This happens if it's the root index, the base folder
path.to_owned()
Expand Down Expand Up @@ -105,7 +105,7 @@ pub fn extract_archive(bytes: Vec<u8>, path: &Path) -> Result<()> {
));
}

println!("Extracted to {:?}", version_dir_path);
println!("Extracted to {version_dir_path:?}");

Ok(())
}
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use std::{
path::{Path, PathBuf},
};

use anyhow::{bail, Result};
#[cfg(windows)]
use anyhow::bail;
use anyhow::Result;
use clap::{Parser, ValueHint};

use crate::subcommand::{
Expand Down
75 changes: 52 additions & 23 deletions src/node_version.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::{
borrow::Borrow,
cmp::Ordering,
collections::HashMap,
fs::{read_link, remove_dir_all},
Expand All @@ -12,6 +11,29 @@ use serde::Deserialize;

use crate::{utils, Config};

#[cfg(target_os = "windows")]
const PLATFORM: &str = "win";
#[cfg(target_os = "macos")]
const PLATFORM: &str = "darwin";
#[cfg(target_os = "linux")]
const PLATFORM: &str = "linux";

#[cfg(target_os = "windows")]
const EXT: &str = ".zip";
#[cfg(target_os = "macos")]
const EXT: &str = ".tar.gz";
#[cfg(target_os = "linux")]
const EXT: &str = ".tar.gz";

#[cfg(target_arch = "x86_64")]
const ARCH: &str = "x64";
#[cfg(target_arch = "x86")]
const ARCH: &str = "x86";
#[cfg(target_arch = "aarch64")]
const ARCH: &str = "arm64";

const X64: &str = "x64";

pub trait NodeVersion {
fn version(&self) -> &Version;
}
Expand Down Expand Up @@ -68,7 +90,7 @@ fn parse_version_str(version_str: &str) -> Result<Version> {
let clean_version = if version_str.starts_with('v') {
version_str.get(1..).unwrap()
} else {
version_str.borrow()
version_str
};

Version::parse(clean_version).context(version_str.to_owned())
Expand Down Expand Up @@ -99,35 +121,40 @@ impl OnlineNodeVersion {
}

pub fn download_url(&self) -> String {
let file_name = self.file();
let file_name: String;

#[cfg(target_os = "macos")]
{
let has_arm = self.has_arm();

file_name = self.file(!has_arm);
}

#[cfg(not(target_os = "macos"))]
{
file_name = self.file(false);
}

format!("https://nodejs.org/dist/v{}/{}", self.version, file_name)
}

#[cfg(target_os = "windows")]
fn file(&self) -> String {
fn file(&self, force_x64: bool) -> String {
format!(
"node-v{version}-win-{arch}.zip",
version = self.version(),
arch = if cfg!(target_arch = "x86") {
"x86"
} else {
"x64"
},
"node-v{VERSION}-{PLATFORM}-{ARCH}{EXT}",
VERSION = self.version(),
ARCH = if force_x64 { X64 } else { ARCH },
)
}

#[cfg(target_os = "macos")]
fn file(&self) -> String {
format!(
"node-v{version}-darwin-x64.tar.gz",
version = self.version()
)
}
fn has_arm(&self) -> bool {
for file in self.files.iter() {
if file.contains("osx") && file.contains("arm64") {
return true;
}
}

#[cfg(target_os = "linux")]
fn file(&self) -> String {
format!("node-v{version}-linux-x64.tar.gz", version = self.version())
false
}
}

Expand Down Expand Up @@ -253,7 +280,7 @@ impl InstalledNodeVersion {
pub fn find_matching(config: &Config, range: &Range) -> Option<InstalledNodeVersion> {
Self::list(config)
.iter()
.find(|inv| range.satisfies(inv.version().borrow()))
.find(|inv| range.satisfies(inv.version()))
.map(|inv| inv.to_owned())
}
}
Expand All @@ -276,6 +303,8 @@ mod tests {
use anyhow::Result;
use node_semver::Version;

use spectral::prelude::*;

use crate::node_version::OnlineNodeVersion;

#[test]
Expand Down Expand Up @@ -350,7 +379,7 @@ mod tests {
let result: OnlineNodeVersion = serde_json::from_str(json_str)
.expect("Failed to parse version data from nodejs.org");

assert_eq!(expected, result);
assert_that!(expected).is_equal_to(result);

Ok(())
}
Expand Down
Loading