Skip to content

Commit 5c8ace2

Browse files
committed
add npm/nodejs proxy support
2 parents 0ed4430 + e0d437a commit 5c8ace2

File tree

3 files changed

+47
-68
lines changed

3 files changed

+47
-68
lines changed

src/nvm.go

+22-14
Original file line numberDiff line numberDiff line change
@@ -407,29 +407,35 @@ func list(listtype string) {
407407
fmt.Println("No installations recognized.")
408408
}
409409
} else {
410-
_, stable, unstable := node.GetAvailable()
410+
_, lts, stable, _ := node.GetAvailable()
411411

412412
releases := len(stable)
413413

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

416-
fmt.Println(" STABLE | UNSTABLE ")
416+
fmt.Println(" LTS | STABLE ")
417417
fmt.Println(" ---------------------------")
418418

419419
for i := 0; i < releases; i++ {
420-
str := "v"+stable[i]
421-
for ii := 10-len(str); ii > 0; ii-- {
422-
str = " "+str
420+
str := " "
421+
if len(lts) > i {
422+
str = "v"+lts[i]
423+
for ii := 10-len(str); ii > 0; ii-- {
424+
str = " "+str
425+
}
423426
}
424-
str = str+" | "
425-
str2 := "v"+unstable[i]
426-
for ii := 10-len(str2); ii > 0; ii-- {
427-
str2 = " "+str2
427+
428+
str2 := ""
429+
if len(stable) > i {
430+
str2 = "v"+stable[i]
431+
for ii := 10-len(str2); ii > 0; ii-- {
432+
str2 = " "+str2
433+
}
428434
}
429-
fmt.Println(" "+str+str2)
435+
fmt.Println(" "+str + " | " + str2)
430436
}
431437

432-
//fmt.Println("\nFor a complete list, visit http://coreybutler.github.io/nodedistro")
438+
fmt.Println("\nFor a complete list, visit https://nodejs.org/download/release")
433439
}
434440
}
435441

@@ -471,7 +477,7 @@ func help() {
471477
fmt.Println(" nvm off : Disable node.js version management.")
472478
fmt.Println(" nvm proxy [url] : Set a proxy to use for downloads. Leave [url] blank to see the current proxy.")
473479
fmt.Println(" Set [url] to \"none\" to remove the proxy.")
474-
fmt.Println(" nvm node_mirror [url] : Set a mirror to http://nodejs.org/dist/. Leave [url] blank to use default url.")
480+
fmt.Println(" nvm node_mirror [url] : Set a mirror to https://nodejs.org/dist/. Leave [url] blank to use default url.")
475481
fmt.Println(" nvm npm_mirror [url] : Set a mirror to https://github.com/npm/npm/archive/. Leave [url] blank to default url.")
476482
fmt.Println(" nvm uninstall <version> : The version must be a specific version.")
477483
// fmt.Println(" nvm update : Automatically update nvm to the latest version.")
@@ -485,8 +491,10 @@ func help() {
485491

486492
// Given a node.js version, returns the associated npm version
487493
func getNpmVersion(nodeversion string) string {
488-
npm, _,_ := node.GetAvailabeVersions()
489-
return npm[nodeversion].(string)
494+
495+
_, _, _, npm := node.GetAvailable()
496+
497+
return npm[nodeversion]
490498
}
491499

492500
func updateRootDir(path string) {

src/nvm/node/node.go

+24-53
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"
@@ -70,7 +69,7 @@ func IsVersionInstalled(root string, version string, cpu string) bool {
7069

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

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

111-
func GetAvailabeVersions() (map[string]interface{}, map[string]interface{},map[string]interface{}){
112-
// Check the service to make sure the version is available
113-
// modified by lzm at 4-7-2016, to chinese guys github maybe blocked at anytime.why not use the http://nodejs.org/dist/index.json?
114-
//text := web.GetRemoteTextFile("https://raw.githubusercontent.com/coreybutler/nodedistro/master/nodeversions.json")
110+
func GetAvailable() ([]string, []string, []string, map[string]string) {
111+
all := make([]string,0)
112+
lts := make([]string,0)
113+
stable := make([]string,0)
114+
npm := make(map[string]string)
115115
url := web.GetFullNodeUrl("index.json")
116+
// Check the service to make sure the version is available
116117
text := web.GetRemoteTextFile(url)
118+
117119
// Parse
118-
var data interface{}
120+
var data = make([]map[string]interface{}, 0)
119121
json.Unmarshal([]byte(text), &data);
120122

121-
//body := data.(map[string]interface{})
122-
//_all := body["all"]
123-
//_stable := body["stable"]
124-
//_unstable := body["unstable"]
125-
//allkeys := _all.(map[string]interface{})
126-
//stablekeys := _stable.(map[string]interface{})
127-
//unstablekeys := _unstable.(map[string]interface{})
128-
129-
body := data.([]interface{})
130-
allkeys := make(map[string]interface{})
131-
stablekeys := make(map[string]interface{})
132-
unstablekeys := make(map[string]interface{})
133-
for _, temp := range body {
134-
item := temp.(map[string]interface{})
135-
key := strings.TrimLeft(item["version"].(string), "v")
136-
value := item["npm"]
137-
if value != nil{
138-
allkeys[key] = value.(string)
139-
version,_ := semver.New(key)
140-
if (version.Major!=0 && version.Major % 2 ==0) || version.Minor % 2==0{
141-
stablekeys[key] = value.(string)
142-
} else{
143-
unstablekeys[key] = value.(string)
144-
}
145-
}
146-
}
147-
return allkeys, stablekeys, unstablekeys
148-
}
123+
for _,element := range data {
149124

150-
func GetAvailable() ([]string, []string, []string) {
151-
all := make([]string,0)
152-
stable := make([]string,0)
153-
unstable := make([]string,0)
125+
var version = element["version"].(string)[1:]
126+
all = append(all, version)
154127

155-
allkeys, stablekeys, unstablekeys := GetAvailabeVersions()
128+
if val, ok := element["npm"].(string); ok {
129+
npm[version] = val
130+
}
156131

157-
for nodev, _ := range allkeys {
158-
all = append(all,nodev)
159-
}
160-
for nodev, _ := range stablekeys {
161-
stable = append(stable,nodev)
162-
}
163-
for nodev, _ := range unstablekeys {
164-
unstable = append(unstable,nodev)
132+
switch v := element["lts"].(type) {
133+
case bool:
134+
if v == false {
135+
stable = append(stable, version)
136+
}
137+
case string:
138+
lts = append(lts, version)
139+
}
165140
}
166141

167-
sort.Sort(BySemanticVersion(all))
168-
sort.Sort(BySemanticVersion(stable))
169-
sort.Sort(BySemanticVersion(unstable))
170-
171-
return all, stable, unstable
142+
return all, lts, stable, npm
172143
}

src/nvm/web/web.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import(
1414
)
1515

1616
var client = &http.Client{}
17-
var nodeBaseAddress = "http://nodejs.org/dist/"
17+
var nodeBaseAddress = "https://nodejs.org/dist/"
1818
var npmBaseAddress = "https://github.com/npm/npm/archive/"
1919

2020
func SetProxy(p string){

0 commit comments

Comments
 (0)