Skip to content

Commit

Permalink
Select latest lts node version (#1258)
Browse files Browse the repository at this point in the history
* select latest lts node version when find latest version that matches range

* add test
  • Loading branch information
coffee-cup authored Jan 13, 2025
1 parent c19175e commit 1f5e11d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
30 changes: 26 additions & 4 deletions src/providers/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,14 +634,18 @@ fn parse_node_version_into_pkg(node_version: &str) -> String {
eprintln!("Warning: node version {node_version} is not valid, using default node version {default_node_pkg_name}");
Range::parse(DEFAULT_NODE_VERSION.to_string()).unwrap()
});
let mut available_node_versions = AVAILABLE_NODE_VERSIONS.to_vec();
let mut available_lts_node_versions = AVAILABLE_NODE_VERSIONS
.iter()
.filter(|v| *v % 2 == 0)
.collect::<Vec<_>>();

// use newest node version first
available_node_versions.sort_by(|a, b| b.cmp(a));
for version_number in available_node_versions {
available_lts_node_versions.sort_by(|a, b| b.cmp(a));
for version_number in available_lts_node_versions {
let version_range_string = format!("{version_number}.x.x");
let version_range: Range = version_range_string.parse().unwrap();
if version_range.allows_any(&range) {
return version_number_to_pkg(version_number);
return version_number_to_pkg(*version_number);
}
}
default_node_pkg_name
Expand Down Expand Up @@ -721,6 +725,24 @@ mod test {
Ok(())
}

#[test]
fn test_latest_lts_version() -> Result<()> {
assert_eq!(
NodeProvider::get_nix_node_pkg(
&PackageJson {
name: Some(String::default()),
engines: Some(engines_node(">=18")),
..Default::default()
},
&App::new("examples/node")?,
&Environment::default()
)?,
Pkg::new(version_number_to_pkg(22).as_str())
);

Ok(())
}

#[test]
fn test_simple_engine() -> Result<()> {
assert_eq!(
Expand Down
3 changes: 1 addition & 2 deletions tests/snapshots/generate_plan_tests__node_pnpm_corepack.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: tests/generate_plan_tests.rs
expression: plan
snapshot_kind: text
---
{
"providers": [],
Expand Down Expand Up @@ -44,7 +43,7 @@ snapshot_kind: text
"setup": {
"name": "setup",
"nixPkgs": [
"nodejs_23",
"nodejs_22",
"pnpm-9_x"
],
"nixOverlays": [
Expand Down
3 changes: 1 addition & 2 deletions tests/snapshots/generate_plan_tests__node_turborepo.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: tests/generate_plan_tests.rs
expression: plan
snapshot_kind: text
---
{
"providers": [],
Expand Down Expand Up @@ -45,7 +44,7 @@ snapshot_kind: text
"setup": {
"name": "setup",
"nixPkgs": [
"nodejs_23",
"nodejs_22",
"npm-8_x"
],
"nixOverlays": [
Expand Down

0 comments on commit 1f5e11d

Please sign in to comment.