From af79f5c207492caa4ebcd4525d61c1099cb89be1 Mon Sep 17 00:00:00 2001 From: Corey Butler Date: Wed, 31 May 2017 09:53:52 -0500 Subject: [PATCH] Sort the output of installed versions using new semver library. Fixes #262. --- build.bat | 2 +- nvm.iss | 2 +- src/nvm.go | 3 ++- src/nvm/node/node.go | 34 +++++++++++++++++++++++++++++++--- 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/build.bat b/build.bat index 35f1c290..adcc2713 100644 --- a/build.bat +++ b/build.bat @@ -4,7 +4,7 @@ SET ORIG=%CD% REM SET GOPATH=%CD%\src SET GOBIN=%CD%\bin SET GOARCH=386 -SET version=1.1.5 +SET version=1.1.6 REM Get the version number from the setup file REM for /f "tokens=*" %%i in ('findstr /n . %INNOSETUP% ^| findstr ^4:#define') do set L=%%i diff --git a/nvm.iss b/nvm.iss index 5d621867..62924ead 100644 --- a/nvm.iss +++ b/nvm.iss @@ -1,7 +1,7 @@ #define MyAppName "NVM for Windows" #define MyAppShortName "nvm" #define MyAppLCShortName "nvm" -#define MyAppVersion "1.1.5" +#define MyAppVersion "1.1.6" #define MyAppPublisher "Ecor Ventures LLC" #define MyAppURL "http://github.com/coreybutler/nvm" #define MyAppExeName "nvm.exe" diff --git a/src/nvm.go b/src/nvm.go index bd213e0c..12ac7145 100644 --- a/src/nvm.go +++ b/src/nvm.go @@ -17,7 +17,7 @@ import ( ) const ( - NvmVersion = "1.1.5" + NvmVersion = "1.1.6" ) type Environment struct { @@ -446,6 +446,7 @@ func list(listtype string) { inuse, a := node.GetCurrentVersion() v := node.GetInstalled(env.root) + for i := 0; i < len(v); i++ { version := v[i] isnode, _ := regexp.MatchString("v",version) diff --git a/src/nvm/node/node.go b/src/nvm/node/node.go index 1e69dfb9..acd4e99b 100644 --- a/src/nvm/node/node.go +++ b/src/nvm/node/node.go @@ -80,28 +80,56 @@ func IsVersionAvailable(v string) bool { return false } +func reverseStringArray(str []string) []string { + for i := 0; i < len(str)/2; i++ { + j := len(str) - i - 1 + str[i], str[j] = str[j], str[i] + } + + return str +} + func GetInstalled(root string) []string { - list := make([]string,0) + list := make([]semver.Version,0) files, _ := ioutil.ReadDir(root) + for i := len(files) - 1; i >= 0; i-- { if files[i].IsDir() { isnode, _ := regexp.MatchString("v",files[i].Name()) + if isnode { - list = append(list,files[i].Name()) + currentVersionString := strings.Replace(files[i].Name(), "v", "", 1) + currentVersion, _ := semver.Make(currentVersionString) + + list = append(list, currentVersion) } } } - return list + + semver.Sort(list) + + loggableList := make([]string,0) + + for _, version := range list { + loggableList = append(loggableList, "v" + version.String()) + } + + loggableList = reverseStringArray(loggableList) + + return loggableList } // Sorting type BySemanticVersion []string + func (s BySemanticVersion) Len() int { return len(s) } + func (s BySemanticVersion) Swap(i, j int) { s[i], s[j] = s[j], s[i] } + func (s BySemanticVersion) Less(i, j int) bool { v1, _ := semver.Make(s[i]) v2, _ := semver.Make(s[j])