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

refactor(planner/nodejs): Add classic (but fast) version determiner #158

Merged
merged 6 commits into from
Oct 16, 2023
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
22 changes: 0 additions & 22 deletions internal/nodejs/cmd/listversion/list_test.go

This file was deleted.

129 changes: 0 additions & 129 deletions internal/nodejs/cmd/listversion/main.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/nodejs/nextjs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"strings"
"text/template"

"github.com/deckarep/golang-set"
mapset "github.com/deckarep/golang-set"
esbuild "github.com/evanw/esbuild/pkg/api"
uuid2 "github.com/google/uuid"
cp "github.com/otiai10/copy"
Expand Down
63 changes: 55 additions & 8 deletions internal/nodejs/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package nodejs
import (
"log"
"os"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -328,23 +329,69 @@ func GetStartScript(ctx *nodePlanContext) string {
}

const defaultNodeVersion = "18"
const minNodeVersion uint64 = 4
const maxNodeVersion uint64 = 20
const maxLtsNodeVersion uint64 = 18

func getNodeVersion(versionRange string, versionsList []*semver.Version) string {
if versionRange == "" {
func getNodeVersion(versionConstraint string) string {
if versionConstraint == "" {
return defaultNodeVersion
}

// create a version constraint from versionRange
constraint, err := semver.NewConstraint(versionRange)
// .nvmrc extensions
if versionConstraint == "node" {
return strconv.FormatUint(maxNodeVersion, 10)
}
if versionConstraint == "lts/*" {
return strconv.FormatUint(maxLtsNodeVersion, 10)
}

// Use regex to find a version if the constraint
// has only one version condition and only limited
// in a major version.
var versionRegex = regexp.MustCompile(`^(?P<op>[~=^]?)(?P<major>[1-9]\d*)\.(?P<minor>0|[1-9]\d*|\*)\.(?P<patch>0|[1-9]\d*|\*)$`)
if matched := versionRegex.FindStringSubmatch(versionConstraint); matched != nil {
op := matched[1]
major := matched[2]
minor := matched[3]
patch := matched[4]

switch op {
case "", "=":
// Exact: Return MAJOR.MINOR.PATCH.
if patch != "*" {
return major + "." + minor + "." + patch
}

fallthrough
case "~":
// Tilde: Return MAJOR.MINOR.
if minor != "*" {
return major + "." + minor
}

fallthrough
case "^":
// Caret: Return MAJOR.
return major
}
}

/* Fallback: Use semver to find a version. Not reliable in tilde case. */

// create a version constraint from versionConstraint
constraint, err := semver.NewConstraint(versionConstraint)
if err != nil {
log.Println("invalid node version constraint", err)
return defaultNodeVersion
}

// find the latest version which satisfies the constraint
for _, version := range versionsList {
if constraint.Check(version) {
return strconv.FormatUint(version.Major(), 10)
for ver := maxNodeVersion; ver >= minNodeVersion; ver-- {
upperVersion := semver.New(ver, 99, 99, "", "")

if constraint.Check(upperVersion) {
return strconv.FormatUint(ver, 10) // We only return the major version
}
}

Expand All @@ -367,7 +414,7 @@ func GetNodeVersion(ctx *nodePlanContext) string {
projectNodeVersion = strings.TrimSpace(string(content))
}

return getNodeVersion(projectNodeVersion, nodeVersions)
return getNodeVersion(projectNodeVersion)
}

// GetEntry gets the entry file of the Node.js project.
Expand Down
82 changes: 47 additions & 35 deletions internal/nodejs/plan_test.go
Original file line number Diff line number Diff line change
@@ -1,66 +1,78 @@
package nodejs

import (
"strconv"
"testing"

"github.com/Masterminds/semver/v3"
"github.com/stretchr/testify/assert"
)

func getMockVersionsList() []*semver.Version {
rawVer := []string{
"v20",
"v19",
"v18",
"v17",
"v16",
"v15",
"v14",
"v13",
"v12",
"v10",
}

versions := make([]*semver.Version, len(rawVer))

for i, v := range rawVer {
ver, err := semver.NewVersion(v)
if err != nil {
panic(err)
}

versions[i] = ver
}

return versions
}

func TestGetNodeVersion_Empty(t *testing.T) {
v := getNodeVersion("", getMockVersionsList())
v := getNodeVersion("")
assert.Equal(t, defaultNodeVersion, v)
}

func TestGetNodeVersion_Fixed(t *testing.T) {
v := getNodeVersion("10", getMockVersionsList())
v := getNodeVersion("10")
assert.Equal(t, "10", v)
}

func TestGetNodeVersion_Or(t *testing.T) {
v := getNodeVersion("^10 || ^12 || ^14", getMockVersionsList())
v := getNodeVersion("^10 || ^12 || ^14")
assert.Equal(t, "14", v)
}

func TestGetNodeVersion_GreaterThanWithLessThan(t *testing.T) {
v := getNodeVersion(">=16 <=20", getMockVersionsList())
v := getNodeVersion(">=16 <=20")
assert.Equal(t, "20", v)
}

func TestGetNodeVersion_GreaterThan(t *testing.T) {
v := getNodeVersion(">=4", getMockVersionsList())
v := getNodeVersion(">=4")
assert.Equal(t, "20", v)
}

func TestGetNodeVersion_LessThan(t *testing.T) {
v := getNodeVersion("<18", getMockVersionsList())
v := getNodeVersion("<18")
assert.Equal(t, "17", v)
}

func TestGetNodeVersion_Exact(t *testing.T) {
v := getNodeVersion("16.0.0")
assert.Equal(t, "16.0.0", v)
}

func TestGetNodeVersion_Exact_WithEqualOp(t *testing.T) {
v := getNodeVersion("=16.0.0")
assert.Equal(t, "16.0.0", v)
}

func TestGetNodeVersion_CaretMinor(t *testing.T) {
v := getNodeVersion("^16.1.0")
assert.Equal(t, "16", v)
}

func TestGetNodeVersion_TildeMinor(t *testing.T) {
v := getNodeVersion("~16.0.1")
assert.Equal(t, "16.0", v)
}

func TestGetNodeVersion_ExactWithWildcard(t *testing.T) {
v := getNodeVersion("16.0.*")
assert.Equal(t, "16.0", v)
}

func TestGetNodeVersion_TildeWithWildcard(t *testing.T) {
v := getNodeVersion("~16.*")
assert.Equal(t, "16", v)
}

func TestGetNodeVersion_NvmRcLts(t *testing.T) {
v := getNodeVersion("lts/*")
assert.Equal(t, strconv.FormatUint(maxLtsNodeVersion, 10), v)
}

func TestGetNodeVersion_NvmRcLatest(t *testing.T) {
v := getNodeVersion("node")
assert.Equal(t, strconv.FormatUint(maxNodeVersion, 10), v)
}
Loading