Skip to content

Commit

Permalink
Sort the output of installed versions using new semver library. Fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
coreybutler committed May 31, 2017
1 parent 472411e commit af79f5c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion nvm.iss
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
3 changes: 2 additions & 1 deletion src/nvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

const (
NvmVersion = "1.1.5"
NvmVersion = "1.1.6"
)

type Environment struct {
Expand Down Expand Up @@ -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)
Expand Down
34 changes: 31 additions & 3 deletions src/nvm/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down

0 comments on commit af79f5c

Please sign in to comment.