提交 5c8ace2e 编写于 作者: L lijie371

add npm/nodejs proxy support

...@@ -407,29 +407,35 @@ func list(listtype string) { ...@@ -407,29 +407,35 @@ func list(listtype string) {
fmt.Println("No installations recognized.") fmt.Println("No installations recognized.")
} }
} else { } else {
_, stable, unstable := node.GetAvailable() _, lts, stable, _ := node.GetAvailable()
releases := len(stable) releases := len(stable)
fmt.Println("\nShowing the "+strconv.Itoa(releases)+" latest available releases.\n") fmt.Println("\nShowing the "+strconv.Itoa(releases)+" latest available releases.\n")
fmt.Println(" STABLE | UNSTABLE ") fmt.Println(" LTS | STABLE ")
fmt.Println(" ---------------------------") fmt.Println(" ---------------------------")
for i := 0; i < releases; i++ { for i := 0; i < releases; i++ {
str := "v"+stable[i] str := " "
for ii := 10-len(str); ii > 0; ii-- { if len(lts) > i {
str = " "+str str = "v"+lts[i]
for ii := 10-len(str); ii > 0; ii-- {
str = " "+str
}
} }
str = str+" | "
str2 := "v"+unstable[i] str2 := ""
for ii := 10-len(str2); ii > 0; ii-- { if len(stable) > i {
str2 = " "+str2 str2 = "v"+stable[i]
for ii := 10-len(str2); ii > 0; ii-- {
str2 = " "+str2
}
} }
fmt.Println(" "+str+str2) fmt.Println(" "+str + " | " + str2)
} }
//fmt.Println("\nFor a complete list, visit http://coreybutler.github.io/nodedistro") fmt.Println("\nFor a complete list, visit https://nodejs.org/download/release")
} }
} }
...@@ -471,7 +477,7 @@ func help() { ...@@ -471,7 +477,7 @@ func help() {
fmt.Println(" nvm off : Disable node.js version management.") fmt.Println(" nvm off : Disable node.js version management.")
fmt.Println(" nvm proxy [url] : Set a proxy to use for downloads. Leave [url] blank to see the current proxy.") fmt.Println(" nvm proxy [url] : Set a proxy to use for downloads. Leave [url] blank to see the current proxy.")
fmt.Println(" Set [url] to \"none\" to remove the proxy.") fmt.Println(" Set [url] to \"none\" to remove the proxy.")
fmt.Println(" nvm node_mirror [url] : Set a mirror to http://nodejs.org/dist/. Leave [url] blank to use default url.") fmt.Println(" nvm node_mirror [url] : Set a mirror to https://nodejs.org/dist/. Leave [url] blank to use default url.")
fmt.Println(" nvm npm_mirror [url] : Set a mirror to https://github.com/npm/npm/archive/. Leave [url] blank to default url.") fmt.Println(" nvm npm_mirror [url] : Set a mirror to https://github.com/npm/npm/archive/. Leave [url] blank to default url.")
fmt.Println(" nvm uninstall <version> : The version must be a specific version.") fmt.Println(" nvm uninstall <version> : The version must be a specific version.")
// fmt.Println(" nvm update : Automatically update nvm to the latest version.") // fmt.Println(" nvm update : Automatically update nvm to the latest version.")
...@@ -485,8 +491,10 @@ func help() { ...@@ -485,8 +491,10 @@ func help() {
// Given a node.js version, returns the associated npm version // Given a node.js version, returns the associated npm version
func getNpmVersion(nodeversion string) string { func getNpmVersion(nodeversion string) string {
npm, _,_ := node.GetAvailabeVersions()
return npm[nodeversion].(string) _, _, _, npm := node.GetAvailable()
return npm[nodeversion]
} }
func updateRootDir(path string) { func updateRootDir(path string) {
......
...@@ -6,7 +6,6 @@ import( ...@@ -6,7 +6,6 @@ import(
"regexp" "regexp"
"io/ioutil" "io/ioutil"
"encoding/json" "encoding/json"
"sort"
"../arch" "../arch"
"../file" "../file"
"../web" "../web"
...@@ -70,7 +69,7 @@ func IsVersionInstalled(root string, version string, cpu string) bool { ...@@ -70,7 +69,7 @@ func IsVersionInstalled(root string, version string, cpu string) bool {
func IsVersionAvailable(v string) bool { func IsVersionAvailable(v string) bool {
// Check the service to make sure the version is available // Check the service to make sure the version is available
avail, _, _ := GetAvailable() avail, _, _, _ := GetAvailable()
for _, b := range avail { for _, b := range avail {
if b == v { if b == v {
...@@ -108,65 +107,37 @@ func (s BySemanticVersion) Less(i, j int) bool { ...@@ -108,65 +107,37 @@ func (s BySemanticVersion) Less(i, j int) bool {
return v1.GTE(v2) return v1.GTE(v2)
} }
func GetAvailabeVersions() (map[string]interface{}, map[string]interface{},map[string]interface{}){ func GetAvailable() ([]string, []string, []string, map[string]string) {
// Check the service to make sure the version is available all := make([]string,0)
// 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? lts := make([]string,0)
//text := web.GetRemoteTextFile("https://raw.githubusercontent.com/coreybutler/nodedistro/master/nodeversions.json") stable := make([]string,0)
npm := make(map[string]string)
url := web.GetFullNodeUrl("index.json") url := web.GetFullNodeUrl("index.json")
// Check the service to make sure the version is available
text := web.GetRemoteTextFile(url) text := web.GetRemoteTextFile(url)
// Parse // Parse
var data interface{} var data = make([]map[string]interface{}, 0)
json.Unmarshal([]byte(text), &data); json.Unmarshal([]byte(text), &data);
//body := data.(map[string]interface{}) for _,element := range data {
//_all := body["all"]
//_stable := body["stable"]
//_unstable := body["unstable"]
//allkeys := _all.(map[string]interface{})
//stablekeys := _stable.(map[string]interface{})
//unstablekeys := _unstable.(map[string]interface{})
body := data.([]interface{})
allkeys := make(map[string]interface{})
stablekeys := make(map[string]interface{})
unstablekeys := make(map[string]interface{})
for _, temp := range body {
item := temp.(map[string]interface{})
key := strings.TrimLeft(item["version"].(string), "v")
value := item["npm"]
if value != nil{
allkeys[key] = value.(string)
version,_ := semver.New(key)
if (version.Major!=0 && version.Major % 2 ==0) || version.Minor % 2==0{
stablekeys[key] = value.(string)
} else{
unstablekeys[key] = value.(string)
}
}
}
return allkeys, stablekeys, unstablekeys
}
func GetAvailable() ([]string, []string, []string) { var version = element["version"].(string)[1:]
all := make([]string,0) all = append(all, version)
stable := make([]string,0)
unstable := make([]string,0)
allkeys, stablekeys, unstablekeys := GetAvailabeVersions() if val, ok := element["npm"].(string); ok {
npm[version] = val
}
for nodev, _ := range allkeys { switch v := element["lts"].(type) {
all = append(all,nodev) case bool:
} if v == false {
for nodev, _ := range stablekeys { stable = append(stable, version)
stable = append(stable,nodev) }
} case string:
for nodev, _ := range unstablekeys { lts = append(lts, version)
unstable = append(unstable,nodev) }
} }
sort.Sort(BySemanticVersion(all)) return all, lts, stable, npm
sort.Sort(BySemanticVersion(stable))
sort.Sort(BySemanticVersion(unstable))
return all, stable, unstable
} }
...@@ -14,7 +14,7 @@ import( ...@@ -14,7 +14,7 @@ import(
) )
var client = &http.Client{} var client = &http.Client{}
var nodeBaseAddress = "http://nodejs.org/dist/" var nodeBaseAddress = "https://nodejs.org/dist/"
var npmBaseAddress = "https://github.com/npm/npm/archive/" var npmBaseAddress = "https://github.com/npm/npm/archive/"
func SetProxy(p string){ func SetProxy(p string){
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册