Repository: jhipster/jhipster-lite
Pull request #9630 (2024-04-24 14:53:45)

Generate a package.json requiring only the recent major node.js version, not the latest patch version
Following #9407, applying the same logic on generated application


Patch:
diff --git a/src/main/java/tech/jhipster/lite/generator/init/domain/InitModuleFactory.java b/src/main/java/tech/jhipster/lite/generator/init/domain/InitModuleFactory.java
index a5f4c8a3a..93a2458aa 100644
--- a/src/main/java/tech/jhipster/lite/generator/init/domain/InitModuleFactory.java
+++ b/src/main/java/tech/jhipster/lite/generator/init/domain/InitModuleFactory.java
@@ -27,7 +27,7 @@ public class InitModuleFactory {
     return moduleBuilder(properties)
       .context()
         .put("dasherizedBaseName", properties.projectBaseName().kebabCase())
-        .put("nodeVersion", npmVersions.getNodeVersion())
+        .put("nodeMajorVersion", npmVersions.nodeVersion().majorVersion())
         .put("endOfLine", endOfLine(properties))
         .and()
       .files()
diff --git a/src/main/java/tech/jhipster/lite/generator/server/javatool/frontendmaven/domain/FrontendMavenModuleFactory.java b/src/main/java/tech/jhipster/lite/generator/server/javatool/frontendmaven/domain/FrontendMavenModuleFactory.java
index 0c2ebb95a..e3a44fa5a 100644
--- a/src/main/java/tech/jhipster/lite/generator/server/javatool/frontendmaven/domain/FrontendMavenModuleFactory.java
+++ b/src/main/java/tech/jhipster/lite/generator/server/javatool/frontendmaven/domain/FrontendMavenModuleFactory.java
@@ -36,7 +36,7 @@ public class FrontendMavenModuleFactory {
     //@formatter:off
     return moduleBuilder(properties)
       .javaBuildProperties()
-        .set(buildPropertyKey("node.version"), buildPropertyValue("v" + npmVersions.getNodeVersion()))
+        .set(buildPropertyKey("node.version"), buildPropertyValue("v" + npmVersions.nodeVersion().get()))
         .set(buildPropertyKey("npm.version"), buildPropertyValue(npmVersions.get("npm", NpmVersionSource.COMMON).get()))
         .and()
       .mavenPlugins()
diff --git a/src/main/java/tech/jhipster/lite/module/domain/npm/NpmPackageVersion.java b/src/main/java/tech/jhipster/lite/module/domain/npm/NpmPackageVersion.java
index f03d8e95a..d58e73c50 100644
--- a/src/main/java/tech/jhipster/lite/module/domain/npm/NpmPackageVersion.java
+++ b/src/main/java/tech/jhipster/lite/module/domain/npm/NpmPackageVersion.java
@@ -10,4 +10,8 @@ public record NpmPackageVersion(String version) {
   public String get() {
     return version();
   }
+
+  public String majorVersion() {
+    return version().split("\\.")[0];
+  }
 }
diff --git a/src/main/java/tech/jhipster/lite/module/domain/npm/NpmVersions.java b/src/main/java/tech/jhipster/lite/module/domain/npm/NpmVersions.java
index 922a8865e..054903c62 100644
--- a/src/main/java/tech/jhipster/lite/module/domain/npm/NpmVersions.java
+++ b/src/main/java/tech/jhipster/lite/module/domain/npm/NpmVersions.java
@@ -42,9 +42,9 @@ public interface NpmVersions {
   }
 
   /**
-   * @return The version of the NodeJS.
+   * @return The version of Node.js.
    */
-  default String getNodeVersion() {
-    return get("node", NpmVersionSource.COMMON).get();
+  default NpmPackageVersion nodeVersion() {
+    return get("node", NpmVersionSource.COMMON);
   }
 }
diff --git a/src/main/resources/generator/init/package.json.mustache b/src/main/resources/generator/init/package.json.mustache
index ee23d6ac7..cefc833da 100644
--- a/src/main/resources/generator/init/package.json.mustache
+++ b/src/main/resources/generator/init/package.json.mustache
@@ -5,6 +5,6 @@
   "description": "{{projectName}}",
   "license": "UNLICENSED",
   "engines": {
-    "node": ">={{nodeVersion}}"
+    "node": ">={{nodeMajorVersion}}"
   }
 }


Test patch:
diff --git a/src/test/java/tech/jhipster/lite/generator/init/domain/InitModuleFactoryTest.java b/src/test/java/tech/jhipster/lite/generator/init/domain/InitModuleFactoryTest.java
index 3643486d7..b82556ceb 100644
--- a/src/test/java/tech/jhipster/lite/generator/init/domain/InitModuleFactoryTest.java
+++ b/src/test/java/tech/jhipster/lite/generator/init/domain/InitModuleFactoryTest.java
@@ -1,6 +1,6 @@
 package tech.jhipster.lite.generator.init.domain;
 
-import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.*;
 import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.*;
 
 import org.junit.jupiter.api.Test;
@@ -12,6 +12,7 @@ import tech.jhipster.lite.TestFileUtils;
 import tech.jhipster.lite.UnitTest;
 import tech.jhipster.lite.module.domain.JHipsterModule;
 import tech.jhipster.lite.module.domain.JHipsterModulesFixture;
+import tech.jhipster.lite.module.domain.npm.NpmPackageVersion;
 import tech.jhipster.lite.module.domain.npm.NpmVersions;
 import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;
 
@@ -29,7 +30,7 @@ class InitModuleFactoryTest {
   void shouldBuildModule() {
     String folder = TestFileUtils.tmpDirForTest();
     JHipsterModuleProperties properties = properties(folder);
-    when(npmVersions.getNodeVersion()).thenReturn("16.0.0");
+    when(npmVersions.nodeVersion()).thenReturn(new NpmPackageVersion("16.0.0"));
 
     JHipsterModule module = factory.buildModule(properties);
 
@@ -45,7 +46,7 @@ class InitModuleFactoryTest {
       .hasFile("package.json")
       .containing("test-project")
       .containing("Test Project")
-      .containing("\"node\": \">=16.0.0\"")
+      .containing("\"node\": \">=16\"")
       .notContaining("scripts");
   }
 
diff --git a/src/test/java/tech/jhipster/lite/generator/server/javatool/frontendmaven/domain/FrontendMavenModuleFactoryTest.java b/src/test/java/tech/jhipster/lite/generator/server/javatool/frontendmaven/domain/FrontendMavenModuleFactoryTest.java
index 8d717612a..7e4efead3 100644
--- a/src/test/java/tech/jhipster/lite/generator/server/javatool/frontendmaven/domain/FrontendMavenModuleFactoryTest.java
+++ b/src/test/java/tech/jhipster/lite/generator/server/javatool/frontendmaven/domain/FrontendMavenModuleFactoryTest.java
@@ -1,8 +1,7 @@
 package tech.jhipster.lite.generator.server.javatool.frontendmaven.domain;
 
-import static org.mockito.Mockito.when;
-import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.assertThatModuleWithFiles;
-import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.pomFile;
+import static org.mockito.Mockito.*;
+import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.*;
 
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
@@ -36,7 +35,7 @@ class FrontendMavenModuleFactoryTest {
       .build();
 
     when(npmVersions.get("npm", NpmVersionSource.COMMON)).thenReturn(new NpmPackageVersion("4.0.0"));
-    when(npmVersions.getNodeVersion()).thenReturn("16.0.0");
+    when(npmVersions.nodeVersion()).thenReturn(new NpmPackageVersion("16.0.0"));
 
     JHipsterModule module = factory.buildModule(properties);