Skip to content

Commit 8e52212

Browse files
authored
ZEA-4591: Lock available Node.js version on major version (#428)
#### Description (required) - **refactor(planner/nodejs): Update Node.js version** - **feat(planner/nodejs): Implement getNodeVersion that only extracts major version** #### Related issues & labels (optional) - Closes ZEA-4591 - Suggested label: enhancement
2 parents fc69c58 + a73733d commit 8e52212

36 files changed

+91
-56
lines changed

internal/nodejs/plan.go

+42-12
Original file line numberDiff line numberDiff line change
@@ -547,21 +547,47 @@ func GetScriptCommand(ctx *nodePlanContext, script string) string {
547547
}
548548

549549
const (
550-
defaultNodeVersion = "20"
551-
maxNodeVersion uint64 = 22
552-
maxLtsNodeVersion uint64 = 20
550+
defaultNodeVersion = "22"
551+
maxNodeVersion uint64 = 23
552+
maxLtsNodeVersion uint64 = 22
553+
minNodeVersion uint64 = 16
553554
)
554555

555-
func getNodeVersion(versionConstraint string) string {
556+
func getNodeVersion(constraint string) string {
556557
// .nvmrc extensions
557-
if versionConstraint == "node" {
558+
if constraint == "node" {
558559
return strconv.FormatUint(maxNodeVersion, 10)
559560
}
560-
if versionConstraint == "lts/*" {
561+
if constraint == "lts/*" {
561562
return strconv.FormatUint(maxLtsNodeVersion, 10)
562563
}
563564

564-
return utils.ConstraintToVersion(versionConstraint, defaultNodeVersion)
565+
// For tilde versions or wildcards, clean the constraint to extract an exact version.
566+
cleaned := constraint
567+
if strings.HasPrefix(cleaned, "~") || strings.HasPrefix(cleaned, "=") {
568+
cleaned = strings.TrimLeft(cleaned, "~=")
569+
}
570+
if strings.Contains(cleaned, "*") {
571+
cleaned = strings.ReplaceAll(cleaned, "*", "0")
572+
}
573+
574+
// Try to parse the cleaned version.
575+
if v, err := semver.NewVersion(strings.TrimSpace(cleaned)); err == nil {
576+
return strconv.FormatUint(max(v.Major(), minNodeVersion), 10)
577+
}
578+
579+
// Otherwise, treat "constraint" as a range constraint.
580+
constraints, err := semver.NewConstraint(constraint)
581+
if err == nil {
582+
for i := maxNodeVersion; i >= minNodeVersion; i-- {
583+
v := semver.MustParse(fmt.Sprintf("%d.999.999", i))
584+
if constraints.Check(v) {
585+
return strconv.FormatUint(i, 10)
586+
}
587+
}
588+
}
589+
590+
return defaultNodeVersion
565591
}
566592

567593
// GetNodeVersion gets the Node.js version of the project.
@@ -572,11 +598,15 @@ func GetNodeVersion(ctx *nodePlanContext) string {
572598

573599
// If there are ".node-version" or ".nvmrc" file, we pick
574600
// the version from them.
575-
if content, err := utils.ReadFileToUTF8(src, ".node-version"); err == nil {
576-
projectNodeVersion = strings.TrimSpace(string(content))
577-
}
578-
if content, err := utils.ReadFileToUTF8(src, ".nvmrc"); err == nil {
579-
projectNodeVersion = strings.TrimSpace(string(content))
601+
for _, f := range []string{".node-version", ".nvmrc"} {
602+
content, err := utils.ReadFileToUTF8(src, f)
603+
if err != nil {
604+
continue
605+
}
606+
607+
versionConstraint := strings.TrimSpace(string(content))
608+
609+
return getNodeVersion(versionConstraint)
580610
}
581611

582612
return getNodeVersion(projectNodeVersion)

internal/nodejs/plan_test.go

+15-10
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,18 @@ func TestGetNodeVersion_Empty(t *testing.T) {
1919
}
2020

2121
func TestGetNodeVersion_Fixed(t *testing.T) {
22-
v := getNodeVersion("10")
23-
assert.Equal(t, "10", v)
22+
v := getNodeVersion("16")
23+
assert.Equal(t, "16", v)
24+
}
25+
26+
func TestGetNodeVersion_TooOld(t *testing.T) {
27+
v := getNodeVersion("0.10")
28+
assert.Equal(t, "16", v)
2429
}
2530

2631
func TestGetNodeVersion_Or(t *testing.T) {
27-
v := getNodeVersion("^10 || ^12 || ^14")
28-
assert.Equal(t, "14", v)
32+
v := getNodeVersion("^18 || ^20")
33+
assert.Equal(t, "20", v)
2934
}
3035

3136
func TestGetNodeVersion_GreaterThanWithLessThan(t *testing.T) {
@@ -35,7 +40,7 @@ func TestGetNodeVersion_GreaterThanWithLessThan(t *testing.T) {
3540

3641
func TestGetNodeVersion_GreaterThan(t *testing.T) {
3742
v := getNodeVersion(">=4")
38-
assert.Equal(t, "4", v) // FIXME: should be the latest?
43+
assert.Equal(t, strconv.FormatUint(maxNodeVersion, 10), v)
3944
}
4045

4146
func TestGetNodeVersion_LessThan(t *testing.T) {
@@ -45,12 +50,12 @@ func TestGetNodeVersion_LessThan(t *testing.T) {
4550

4651
func TestGetNodeVersion_Exact(t *testing.T) {
4752
v := getNodeVersion("16.0.0")
48-
assert.Equal(t, "16.0", v)
53+
assert.Equal(t, "16", v)
4954
}
5055

5156
func TestGetNodeVersion_Exact_WithEqualOp(t *testing.T) {
5257
v := getNodeVersion("=16.0.0")
53-
assert.Equal(t, "16.0", v)
58+
assert.Equal(t, "16", v)
5459
}
5560

5661
func TestGetNodeVersion_CaretMinor(t *testing.T) {
@@ -60,12 +65,12 @@ func TestGetNodeVersion_CaretMinor(t *testing.T) {
6065

6166
func TestGetNodeVersion_TildeMinor(t *testing.T) {
6267
v := getNodeVersion("~16.0.1")
63-
assert.Equal(t, "16.0", v)
68+
assert.Equal(t, "16", v)
6469
}
6570

6671
func TestGetNodeVersion_ExactWithWildcard(t *testing.T) {
6772
v := getNodeVersion("16.0.*")
68-
assert.Equal(t, "16.0", v)
73+
assert.Equal(t, "16", v)
6974
}
7075

7176
func TestGetNodeVersion_TildeWithWildcard(t *testing.T) {
@@ -85,7 +90,7 @@ func TestGetNodeVersion_NvmRcLatest(t *testing.T) {
8590

8691
func TestGetNodeVersion_VPrefixedVersion(t *testing.T) {
8792
v := getNodeVersion("v20.11.0")
88-
assert.Equal(t, "20.11", v)
93+
assert.Equal(t, "20", v)
8994
}
9095

9196
func TestGetInstallCmd_CustomizeInstallCmd(t *testing.T) {

tests/snapshots/bun-bagel.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ Meta:
88
framework: "bagel"
99
initCmd: "RUN npm install -g bun@latest"
1010
installCmd: "RUN bun install"
11-
nodeVersion: "20"
11+
nodeVersion: "22"
1212
packageManager: "bun"
1313
startCmd: "bun run start"

tests/snapshots/bun-baojs.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ Meta:
88
framework: "baojs"
99
initCmd: "RUN npm install -g bun@latest"
1010
installCmd: "RUN bun install"
11-
nodeVersion: "20"
11+
nodeVersion: "22"
1212
packageManager: "bun"
1313
startCmd: "bun run start"

tests/snapshots/bun-elysia.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ Meta:
88
framework: "none"
99
initCmd: "RUN npm install -f -g pnpm@latest || npm install -f -g pnpm@8"
1010
installCmd: "RUN pnpm install"
11-
nodeVersion: "20"
11+
nodeVersion: "22"
1212
packageManager: "pnpm"
1313
startCmd: "pnpm src/index.ts"

tests/snapshots/bun-nextjs.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Meta:
88
framework: "next.js"
99
initCmd: "RUN npm install -g bun@latest"
1010
installCmd: "RUN bun install"
11-
nodeVersion: "20"
11+
nodeVersion: "22"
1212
packageManager: "bun"
1313
serverless: "true"
1414
startCmd: ""

tests/snapshots/bun-nuxtjs.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Meta:
88
framework: "nuxt.js"
99
initCmd: "RUN npm install -g bun@latest"
1010
installCmd: "RUN bun install"
11-
nodeVersion: "20"
11+
nodeVersion: "22"
1212
packageManager: "bun"
1313
serverless: "true"
1414
startCmd: ""

tests/snapshots/bun-plain.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ Meta:
88
framework: "none"
99
initCmd: "RUN npm install -g bun@latest"
1010
installCmd: "RUN bun install"
11-
nodeVersion: "20"
11+
nodeVersion: "22"
1212
packageManager: "bun"
1313
startCmd: "bun run start"

tests/snapshots/bun-without-lockfile.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ Meta:
88
framework: "none"
99
initCmd: "RUN npm install -g bun@latest"
1010
installCmd: "RUN bun install"
11-
nodeVersion: "20"
11+
nodeVersion: "22"
1212
packageManager: "bun"
1313
startCmd: "bun run src/index.ts"

tests/snapshots/bun-yarn-lockfile.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ Meta:
88
framework: "none"
99
initCmd: "RUN npm install -f -g yarn@latest"
1010
installCmd: "RUN yarn install"
11-
nodeVersion: "20"
11+
nodeVersion: "22"
1212
packageManager: "yarn"
1313
startCmd: "yarn start"

tests/snapshots/node-astro.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Meta:
77
framework: "astro"
88
initCmd: "RUN npm install -f -g pnpm@latest || npm install -f -g pnpm@8"
99
installCmd: "RUN pnpm install"
10-
nodeVersion: "20"
10+
nodeVersion: "22"
1111
packageManager: "pnpm"
1212
serverless: "true"
1313
startCmd: ""

tests/snapshots/node-sveltekit.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Meta:
77
framework: "svelte"
88
initCmd: "RUN npm install -f -g pnpm@latest || npm install -f -g pnpm@8"
99
installCmd: "RUN pnpm install"
10-
nodeVersion: "20"
10+
nodeVersion: "22"
1111
packageManager: "pnpm"
1212
serverless: "true"
1313
startCmd: ""

tests/snapshots/nodejs-a-lot-of-dependencies.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Meta:
77
framework: "next.js"
88
initCmd: "RUN npm install -f -g yarn@latest && yarn set version berry"
99
installCmd: "RUN yarn install"
10-
nodeVersion: "20"
10+
nodeVersion: "22"
1111
packageManager: "yarn"
1212
serverless: "true"
1313
startCmd: ""

tests/snapshots/nodejs-angular.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Meta:
77
framework: "angular"
88
initCmd: "RUN npm update -g npm"
99
installCmd: "RUN npm install"
10-
nodeVersion: "20"
10+
nodeVersion: "22"
1111
outputDir: "dist/angular-template/browser"
1212
packageManager: "npm"
1313
serverless: "true"

tests/snapshots/nodejs-docusaurus.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Meta:
77
framework: "docusaurus"
88
initCmd: "RUN npm install -f -g pnpm@latest || npm install -f -g pnpm@8"
99
installCmd: "RUN pnpm install"
10-
nodeVersion: "16"
10+
nodeVersion: "23"
1111
outputDir: "build"
1212
packageManager: "pnpm"
1313
serverless: "true"

tests/snapshots/nodejs-expressjs.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ Meta:
77
framework: "none"
88
initCmd: "RUN npm install -f -g pnpm@latest || npm install -f -g pnpm@8"
99
installCmd: "RUN pnpm install"
10-
nodeVersion: "20"
10+
nodeVersion: "22"
1111
packageManager: "pnpm"
1212
startCmd: "pnpm start"

tests/snapshots/nodejs-foal.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ Meta:
77
framework: "none"
88
initCmd: "RUN npm install -f -g pnpm@latest || npm install -f -g pnpm@8"
99
installCmd: "RUN pnpm install"
10-
nodeVersion: "18"
10+
nodeVersion: "23"
1111
packageManager: "pnpm"
1212
startCmd: "pnpm start"

tests/snapshots/nodejs-medusa-zb.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ Meta:
77
framework: "medusa"
88
initCmd: "RUN npm install -f -g yarn@latest"
99
installCmd: "RUN yarn install"
10-
nodeVersion: "20"
10+
nodeVersion: "23"
1111
packageManager: "yarn"
1212
startCmd: "cd .medusa/server && yarn predeploy && yarn start"

tests/snapshots/nodejs-medusa.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ Meta:
77
framework: "medusa"
88
initCmd: "RUN npm install -f -g yarn@latest"
99
installCmd: "RUN yarn install"
10-
nodeVersion: "20"
10+
nodeVersion: "23"
1111
packageManager: "yarn"
1212
startCmd: "cd .medusa/server && yarn start"

tests/snapshots/nodejs-nestjs.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ Meta:
77
framework: "nest.js"
88
initCmd: "RUN npm install -f -g pnpm@latest || npm install -f -g pnpm@8"
99
installCmd: "RUN pnpm install"
10-
nodeVersion: "20"
10+
nodeVersion: "22"
1111
packageManager: "pnpm"
1212
startCmd: "pnpm start"

tests/snapshots/nodejs-nextjs.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Meta:
77
framework: "next.js"
88
initCmd: "RUN npm install -f -g pnpm@latest || npm install -f -g pnpm@8"
99
installCmd: "RUN pnpm install"
10-
nodeVersion: "20"
10+
nodeVersion: "22"
1111
packageManager: "pnpm"
1212
serverless: "true"
1313
startCmd: ""

tests/snapshots/nodejs-nuejs.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ Meta:
77
framework: "nuejs"
88
initCmd: "RUN npm install -f -g pnpm@latest || npm install -f -g pnpm@8"
99
installCmd: "RUN pnpm install"
10-
nodeVersion: "20"
10+
nodeVersion: "22"
1111
packageManager: "pnpm"
1212
startCmd: "pnpm start"

tests/snapshots/nodejs-nuxtjs.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Meta:
77
framework: "nuxt.js"
88
initCmd: "RUN npm install -f -g pnpm@latest || npm install -f -g pnpm@8"
99
installCmd: "RUN pnpm install"
10-
nodeVersion: "20"
10+
nodeVersion: "22"
1111
packageManager: "pnpm"
1212
serverless: "true"
1313
startCmd: ""

tests/snapshots/nodejs-payload.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Meta:
77
framework: "next.js"
88
initCmd: "RUN npm install -f -g yarn@latest"
99
installCmd: "RUN yarn install"
10-
nodeVersion: "20"
10+
nodeVersion: "22"
1111
packageManager: "yarn"
1212
serverless: "true"
1313
startCmd: ""

tests/snapshots/nodejs-qwik-city.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ Meta:
77
framework: "qwik"
88
initCmd: "RUN npm update -g npm"
99
installCmd: "RUN npm install"
10-
nodeVersion: "15"
10+
nodeVersion: "23"
1111
packageManager: "npm"
1212
startCmd: "npm run deploy"

tests/snapshots/nodejs-remix.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Meta:
77
framework: "remix"
88
initCmd: "RUN npm install -f -g pnpm@latest || npm install -f -g pnpm@8"
99
installCmd: "RUN pnpm install"
10-
nodeVersion: "18"
10+
nodeVersion: "23"
1111
packageManager: "pnpm"
1212
serverless: "true"
1313
startCmd: ""

tests/snapshots/nodejs-rspress.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Meta:
77
framework: "rspress"
88
initCmd: "RUN npm install -f -g pnpm@latest || npm install -f -g pnpm@8"
99
installCmd: "RUN pnpm install"
10-
nodeVersion: "20"
10+
nodeVersion: "22"
1111
outputDir: "doc_build"
1212
packageManager: "pnpm"
1313
serverless: "true"

tests/snapshots/nodejs-slidev.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Meta:
77
framework: "sli.dev"
88
initCmd: "RUN npm install -f -g pnpm@latest || npm install -f -g pnpm@8"
99
installCmd: "RUN pnpm install"
10-
nodeVersion: "20"
10+
nodeVersion: "22"
1111
outputDir: "dist"
1212
packageManager: "pnpm"
1313
serverless: "true"

tests/snapshots/nodejs-starlight.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Meta:
77
framework: "astro-starlight"
88
initCmd: "RUN npm install -f -g pnpm@latest || npm install -f -g pnpm@8"
99
installCmd: "RUN pnpm install"
10-
nodeVersion: "20"
10+
nodeVersion: "22"
1111
outputDir: "dist"
1212
packageManager: "pnpm"
1313
serverless: "true"

tests/snapshots/nodejs-sveltekit-v2.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ Meta:
77
framework: "svelte"
88
initCmd: "RUN npm update -g npm"
99
installCmd: "RUN npm install"
10-
nodeVersion: "20"
10+
nodeVersion: "22"
1111
packageManager: "npm"
1212
startCmd: "node build/index.js"

tests/snapshots/nodejs-umi.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Meta:
77
framework: "umi"
88
initCmd: "RUN npm install -f -g pnpm@latest || npm install -f -g pnpm@8"
99
installCmd: "RUN pnpm install"
10-
nodeVersion: "20"
10+
nodeVersion: "22"
1111
outputDir: "dist"
1212
packageManager: "pnpm"
1313
serverless: "true"

0 commit comments

Comments
 (0)