Skip to content

Commit e0d437a

Browse files
committed
Merge pull request coreybutler#162 from fredericosilva/master
list available: show LTS and STABLE
2 parents 6edd36b + d786051 commit e0d437a

File tree

2 files changed

+44
-49
lines changed

2 files changed

+44
-49
lines changed

src/nvm.go

+19-22
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"io/ioutil"
99
"regexp"
1010
"bytes"
11-
"encoding/json"
1211
"strconv"
1312
"./nvm/web"
1413
"./nvm/arch"
@@ -401,29 +400,35 @@ func list(listtype string) {
401400
fmt.Println("No installations recognized.")
402401
}
403402
} else {
404-
_, stable, unstable := node.GetAvailable()
403+
_, lts, stable, _ := node.GetAvailable()
405404

406405
releases := 15
407406

408407
fmt.Println("\nShowing the "+strconv.Itoa(releases)+" latest available releases.\n")
409408

410-
fmt.Println(" STABLE | UNSTABLE ")
409+
fmt.Println(" LTS | STABLE ")
411410
fmt.Println(" ---------------------------")
412411

413412
for i := 0; i < releases; i++ {
414-
str := "v"+stable[i]
415-
for ii := 10-len(str); ii > 0; ii-- {
416-
str = " "+str
413+
str := " "
414+
if len(lts) > i {
415+
str = "v"+lts[i]
416+
for ii := 10-len(str); ii > 0; ii-- {
417+
str = " "+str
418+
}
417419
}
418-
str = str+" | "
419-
str2 := "v"+unstable[i]
420-
for ii := 10-len(str2); ii > 0; ii-- {
421-
str2 = " "+str2
420+
421+
str2 := ""
422+
if len(stable) > i {
423+
str2 = "v"+stable[i]
424+
for ii := 10-len(str2); ii > 0; ii-- {
425+
str2 = " "+str2
426+
}
422427
}
423-
fmt.Println(" "+str+str2)
428+
fmt.Println(" "+str + " | " + str2)
424429
}
425430

426-
fmt.Println("\nFor a complete list, visit http://coreybutler.github.io/nodedistro")
431+
fmt.Println("\nFor a complete list, visit https://nodejs.org/download/release")
427432
}
428433
}
429434

@@ -478,17 +483,9 @@ func help() {
478483
// Given a node.js version, returns the associated npm version
479484
func getNpmVersion(nodeversion string) string {
480485

481-
// Get raw text
482-
text := web.GetRemoteTextFile("https://raw.githubusercontent.com/coreybutler/nodedistro/master/nodeversions.json")
483-
484-
// Parse
485-
var data interface{}
486-
json.Unmarshal([]byte(text), &data);
487-
body := data.(map[string]interface{})
488-
all := body["all"]
489-
npm := all.(map[string]interface{})
486+
_, _, _, npm := node.GetAvailable()
490487

491-
return npm[nodeversion].(string)
488+
return npm[nodeversion]
492489
}
493490

494491
func updateRootDir(path string) {

src/nvm/node/node.go

+25-27
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import(
66
"regexp"
77
"io/ioutil"
88
"encoding/json"
9-
"sort"
109
"../arch"
1110
"../file"
1211
"../web"
@@ -71,7 +70,7 @@ func IsVersionInstalled(root string, version string, cpu string) bool {
7170

7271
func IsVersionAvailable(v string) bool {
7372
// Check the service to make sure the version is available
74-
avail, _, _ := GetAvailable()
73+
avail, _, _, _ := GetAvailable()
7574

7675
for _, b := range avail {
7776
if b == v {
@@ -109,38 +108,37 @@ func (s BySemanticVersion) Less(i, j int) bool {
109108
return v1.GTE(v2)
110109
}
111110

112-
func GetAvailable() ([]string, []string, []string) {
111+
func GetAvailable() ([]string, []string, []string, map[string]string) {
113112
all := make([]string,0)
113+
lts := make([]string,0)
114114
stable := make([]string,0)
115-
unstable := make([]string,0)
115+
npm := make(map[string]string)
116116

117117
// Check the service to make sure the version is available
118-
text := web.GetRemoteTextFile("https://raw.githubusercontent.com/coreybutler/nodedistro/master/nodeversions.json")
118+
text := web.GetRemoteTextFile("https://nodejs.org/download/release/index.json")
119119

120120
// Parse
121-
var data interface{}
121+
var data = make([]map[string]interface{}, 0)
122122
json.Unmarshal([]byte(text), &data);
123-
body := data.(map[string]interface{})
124-
_all := body["all"]
125-
_stable := body["stable"]
126-
_unstable := body["unstable"]
127-
allkeys := _all.(map[string]interface{})
128-
stablekeys := _stable.(map[string]interface{})
129-
unstablekeys := _unstable.(map[string]interface{})
130-
131-
for nodev, _ := range allkeys {
132-
all = append(all,nodev)
133-
}
134-
for nodev, _ := range stablekeys {
135-
stable = append(stable,nodev)
136-
}
137-
for nodev, _ := range unstablekeys {
138-
unstable = append(unstable,nodev)
139-
}
140123

141-
sort.Sort(BySemanticVersion(all))
142-
sort.Sort(BySemanticVersion(stable))
143-
sort.Sort(BySemanticVersion(unstable))
124+
for _,element := range data {
125+
126+
var version = element["version"].(string)[1:]
127+
all = append(all, version)
128+
129+
if val, ok := element["npm"].(string); ok {
130+
npm[version] = val
131+
}
132+
133+
switch v := element["lts"].(type) {
134+
case bool:
135+
if v == false {
136+
stable = append(stable, version)
137+
}
138+
case string:
139+
lts = append(lts, version)
140+
}
141+
}
144142

145-
return all, stable, unstable
143+
return all, lts, stable, npm
146144
}

0 commit comments

Comments
 (0)