From 55850f2ea1e53f921fc1ca147d461274222e236c Mon Sep 17 00:00:00 2001 From: Vladimir Nemergut Date: Thu, 16 Dec 2021 14:55:07 +0100 Subject: [PATCH] Allow `nvm use` to be called with the part of the semver 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 --- src/nvm.go | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/src/nvm.go b/src/nvm.go index 95977e3f..1476470c 100644 --- a/src/nvm.go +++ b/src/nvm.go @@ -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() @@ -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 { @@ -482,6 +482,10 @@ 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) @@ -489,8 +493,8 @@ func findLatestSubVersion(version string, localOnly ...bool) string { 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 } @@ -501,7 +505,7 @@ func findLatestSubVersion(version string, localOnly ...bool) string { } if len(strings.TrimSpace(result)) > 0 { - return result + return versionNumberFrom(result) } } @@ -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) {