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

Allow nvm use to be called with the part of the semver #709

Merged
merged 1 commit into from
May 12, 2022
Merged
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
24 changes: 12 additions & 12 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(result))
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,17 +518,13 @@ 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
version, cpuarch, err := getVersion(version, cpuarch, true)
coreybutler marked this conversation as resolved.
Show resolved Hide resolved

if err != nil {
vladonemo marked this conversation as resolved.
Show resolved Hide resolved
vladonemo marked this conversation as resolved.
Show resolved Hide resolved
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 {
if !strings.Contains(err.Error(), "No Major.Minor.Patch") {
fmt.Println(err.Error())
return
}
return
}

// Make sure the version is installed. If not, warn.
Expand Down