Skip to content

Commit

Permalink
Allow nvm use to be called with the part of the semver
Browse files Browse the repository at this point in the history
For instance, the `nvm use 12` will switch to the latest installed version of 12.*. Similarly, `nvm use 12.22` will switch to the latest installed version of 12.22.*

Fixes #708
  • Loading branch information
vladonemo committed Dec 16, 2021
1 parent 1f7a706 commit 55850f2
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions src/nvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func getVersion(version string, cpuarch string, localInstallsOnly ...bool) (stri
version = v
}

version = strings.Replace(version, "v", "", 1)
version = versionNumberFrom(version)
v, err := semver.Make(version)
if err == nil {
err = v.Validate()
Expand All @@ -240,7 +240,7 @@ func getVersion(version string, cpuarch string, localInstallsOnly ...bool) (stri
version = cleanVersion(version)
}

version = strings.Replace(version, "v", "", 1)
version = versionNumberFrom(version)
} else if strings.Contains(err.Error(), "No Major.Minor.Patch") {
latestLocalInstall := false
if len(localInstallsOnly) > 0 {
Expand Down Expand Up @@ -482,15 +482,19 @@ func uninstall(version string) {
return
}

func versionNumberFrom(version string) string {
return strings.Replace(version, "v", "", 1)
}

func findLatestSubVersion(version string, localOnly ...bool) string {
if len(localOnly) > 0 && localOnly[0] {
installed := node.GetInstalled(env.root)
result := ""
for _, v := range installed {
if strings.HasPrefix(v, "v"+version) {
if result != "" {
current, _ := semver.New(strings.Replace(result, "v", "", 1))
next, _ := semver.New(strings.Replace(v, "v", "", 1))
current, _ := semver.New(versionNumberFrom(v))
next, _ := semver.New(versionNumberFrom(v))
if current.LT(*next) {
result = v
}
Expand All @@ -501,7 +505,7 @@ func findLatestSubVersion(version string, localOnly ...bool) string {
}

if len(strings.TrimSpace(result)) > 0 {
return result
return versionNumberFrom(result)
}
}

Expand All @@ -514,18 +518,7 @@ func findLatestSubVersion(version string, localOnly ...bool) string {
}

func use(version string, cpuarch string, reload ...bool) {
v, a, err := getVersion(version, cpuarch, true)
version = v
cpuarch = a

if err != nil {
if strings.Contains(err.Error(), "No Major.Minor.Patch") {
fmt.Printf("node v%v (%v-bit) is not installed or cannot be found.", v, a)
} else {
fmt.Println(err.Error())
}
return
}
version, cpuarch, err := getVersion(version, cpuarch, true)

// Make sure the version is installed. If not, warn.
if !node.IsVersionInstalled(env.root, version, cpuarch) {
Expand Down

0 comments on commit 55850f2

Please sign in to comment.