Skip to content

Commit

Permalink
feat(list-remote): introduce --latest-lts option
Browse files Browse the repository at this point in the history
  • Loading branch information
younggglcy committed Mar 11, 2025
1 parent 64ef825 commit e525841
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
3 changes: 3 additions & 0 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ Options:
[env: FNM_ARCH]
--latest-lts
Show the latest version of each LTS
--version-file-strategy <VERSION_FILE_STRATEGY>
A strategy for how to resolve the Node version. Used whenever `fnm use` or `fnm install` is called without a version, or when `--use-on-cd` is configured on evaluation
Expand Down
40 changes: 28 additions & 12 deletions src/commands/ls_remote.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::config::FnmConfig;
use crate::remote_node_index;
use crate::remote_node_index::{self, IndexedNodeVersion};
use crate::user_version::UserVersion;

use colored::Colorize;
use thiserror::Error;

Expand All @@ -11,7 +10,7 @@ pub struct LsRemote {
#[arg(long)]
filter: Option<UserVersion>,

/// Show only LTS versions (optionally filter by LTS codename)
/// Show only LTS versions (optionally filter by LTS codename)
#[arg(long)]
#[expect(
clippy::option_option,
Expand All @@ -26,6 +25,10 @@ pub struct LsRemote {
/// Only show the latest matching version
#[arg(long)]
latest: bool,

/// Show the latest version of each LTS
#[arg(long)]
latest_lts: bool,
}

#[derive(clap::ValueEnum, Clone, Debug, PartialEq)]
Expand Down Expand Up @@ -53,15 +56,28 @@ impl super::command::Command for LsRemote {
fn apply(self, config: &FnmConfig) -> Result<(), Self::Error> {
let mut all_versions = remote_node_index::list(&config.node_dist_mirror)?;

if let Some(lts) = &self.lts {
match lts {
Some(codename) => all_versions.retain(|v| {
v.lts
.as_ref()
.is_some_and(|v_lts| v_lts.eq_ignore_ascii_case(codename))
}),
None => all_versions.retain(|v| v.lts.is_some()),
};
if let Some(Some(lts_codename)) = &self.lts {
all_versions.retain(|v| {
v.lts
.as_ref()
.is_some_and(|v_lts| v_lts.eq_ignore_ascii_case(lts_codename))
});
} else if self.lts.is_some() || self.latest_lts {
all_versions.retain(|v| v.lts.is_some());
}

if self.latest_lts {
let mut latest_lts: Vec<IndexedNodeVersion> = Vec::new();
let mut iter = all_versions.iter().peekable();
while let Some(version) = iter.next() {
if let Some(next) = iter.peek() {
if version.lts == next.lts {
continue;
}
}
latest_lts.push(version.clone());
}
all_versions = latest_lts;
}

if let Some(filter) = &self.filter {
Expand Down
2 changes: 1 addition & 1 deletion src/remote_node_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ mod lts_status {
}
}

#[derive(Deserialize, Debug)]
#[derive(Deserialize, Debug, Clone)]
pub struct IndexedNodeVersion {
pub version: Version,
#[serde(with = "lts_status")]
Expand Down

0 comments on commit e525841

Please sign in to comment.