Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add filtering options to ls-remote #1063

Merged
merged 21 commits into from
May 27, 2024
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
5 changes: 5 additions & 0 deletions .changeset/fifty-emus-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fnm": minor
---

feat: add remote version sorting and filtering
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not too familiar with how this changelog entry is handled, but might be relevant to include the actual flags / examples here to appear in the release notes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure either, seems like this was added fairly recently

18 changes: 18 additions & 0 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ List all remote Node.js versions
Usage: fnm list-remote [OPTIONS]

Options:
--filter <FILTER>
Filter versions by a user-defined version or a semver range

--node-dist-mirror <NODE_DIST_MIRROR>
<https://nodejs.org/dist/> mirror

Expand All @@ -92,6 +95,21 @@ Options:

[env: FNM_DIR]

--lts [<LTS>]
Show only LTS versions (optionally filter by LTS codename)

--sort <SORT>
Version sorting order

[default: asc]

Possible values:
- desc: Sort versions in descending order (latest to earliest)
- asc: Sort versions in ascending order (earliest to latest)

--latest
Only show the latest matching version

--log-level <LOG_LEVEL>
The log level of fnm commands

Expand Down
67 changes: 63 additions & 4 deletions src/commands/ls_remote.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,79 @@
use crate::config::FnmConfig;
use crate::remote_node_index;
use crate::user_version::UserVersion;

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

#[derive(clap::Parser, Debug)]
pub struct LsRemote {}
pub struct LsRemote {
/// Filter versions by a user-defined version or a semver range
#[arg(long)]
filter: Option<UserVersion>,

/// Show only LTS versions (optionally filter by LTS codename)
#[arg(long)]
#[allow(clippy::option_option)]
lts: Option<Option<String>>,

/// Version sorting order
#[arg(long, default_value = "asc")]
sort: SortingMethod,

/// Only show the latest matching version
#[arg(long)]
latest: bool,
}

#[derive(clap::ValueEnum, Clone, Debug, PartialEq)]
pub enum SortingMethod {
#[clap(name = "desc")]
/// Sort versions in descending order (latest to earliest)
Descending,
#[clap(name = "asc")]
/// Sort versions in ascending order (earliest to latest)
Ascending,
}

impl super::command::Command for LsRemote {
type Error = Error;

fn apply(self, config: &FnmConfig) -> Result<(), Self::Error> {
let all_versions = remote_node_index::list(&config.node_dist_mirror)?;
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(filter) = &self.filter {
all_versions.retain(|v| filter.matches(&v.version, config));
}

if self.latest {
all_versions.truncate(1);
}

all_versions.sort_by_key(|v| v.version.clone());
if let SortingMethod::Descending = self.sort {
all_versions.reverse();
}

if all_versions.is_empty() {
eprintln!("{}", "No versions were found!".red());
return Ok(());
}

for version in all_versions {
for version in &all_versions {
print!("{}", version.version);
if let Some(lts) = &version.lts {
print!(" ({lts})");
print!("{}", format!(" ({lts})").cyan());
}
println!();
}
Expand Down
Loading