diff --git a/build.bat b/build.bat index 0f9db3c51c2a74b69b3884de72f0d07cab34d384..cb62f53f97c27927908f0a39f285f9305ae2761f 100644 --- a/build.bat +++ b/build.bat @@ -19,10 +19,10 @@ SET DIST=%CD%\dist\%version% REM Build the executable echo Building NVM for Windows -rm %GOBIN%\nvm.exe +REM rm %GOBIN%\nvm.exe cd %GOPATH% -goxc -arch="386" -os="windows" -n="nvm" -d="%GOBIN%" -o="%GOBIN%\nvm{{.Ext}}" -tasks-=package cd %ORIG% +REM go build src/nvm.go -o=bin/nvm.exe rm %GOBIN%\src.exe rm %GOPATH%\src.exe rm %GOPATH%\nvm.exe diff --git a/nvm.iss b/nvm.iss index ff67850674be9e98681ad07c81eb76af8e6f7717..4c8fb0108eba89f4f41b0e41b6b9f50dd02bed1f 100644 --- a/nvm.iss +++ b/nvm.iss @@ -6,7 +6,7 @@ #define MyAppURL "http://github.com/coreybutler/nvm" #define MyAppExeName "nvm.exe" #define MyIcon "bin\nodejs.ico" -#define ProjectRoot "C:\Users\Corey\Documents\workspace\Applications\nvm" +#define ProjectRoot "E:\projects\go\src\nvm-windows" [Setup] ; NOTE: The value of AppId uniquely identifies this application. diff --git a/src/nvm.go b/src/nvm.go index 46ced54965a874ccca7ca2524dafb9052bae28a0..0fe0f2422cdc0f7c6cb4aa61adfe7c76703cb79f 100644 --- a/src/nvm.go +++ b/src/nvm.go @@ -8,7 +8,6 @@ import ( "io/ioutil" "regexp" "bytes" - "encoding/json" "strconv" "./nvm/web" "./nvm/arch" @@ -26,6 +25,8 @@ type Environment struct { root string symlink string arch string + node_mirror string + npm_mirror string proxy string originalpath string originalversion string @@ -36,6 +37,8 @@ var env = &Environment{ root: "", symlink: os.Getenv("NVM_SYMLINK"), arch: os.Getenv("PROCESSOR_ARCHITECTURE"), + node_mirror: "", + npm_mirror: "", proxy: "none", originalpath: "", originalversion: "", @@ -120,7 +123,9 @@ func update() { } func CheckVersionExceedsLatest(version string) bool{ - content := web.GetRemoteTextFile("http://nodejs.org/dist/latest/SHASUMS256.txt") + //content := web.GetRemoteTextFile("http://nodejs.org/dist/latest/SHASUMS256.txt") + url := web.GetFullNodeUrl("latest/SHASUMS256.txt"); + content := web.GetRemoteTextFile(url) re := regexp.MustCompile("node-v(.+)+msi") reg := regexp.MustCompile("node-v|-x.+") latest := reg.ReplaceAllString(re.FindString(content),"") @@ -157,7 +162,8 @@ func install(version string, cpuarch string) { // If user specifies "latest" version, find out what version is if version == "latest" { - content := web.GetRemoteTextFile("http://nodejs.org/dist/latest/SHASUMS256.txt") + url := web.GetFullNodeUrl("latest/SHASUMS256.txt"); + content := web.GetRemoteTextFile(url) re := regexp.MustCompile("node-v(.+)+msi") reg := regexp.MustCompile("node-v|-x.+") version = reg.ReplaceAllString(re.FindString(content),"") @@ -403,7 +409,7 @@ func list(listtype string) { } else { _, stable, unstable := node.GetAvailable() - releases := 15 + releases := len(stable) fmt.Println("\nShowing the "+strconv.Itoa(releases)+" latest available releases.\n") @@ -423,7 +429,7 @@ func list(listtype string) { fmt.Println(" "+str+str2) } - fmt.Println("\nFor a complete list, visit http://coreybutler.github.io/nodedistro") + //fmt.Println("\nFor a complete list, visit http://coreybutler.github.io/nodedistro") } } @@ -465,6 +471,8 @@ func help() { 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(" 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 npm_mirror [url] : Set a mirror to https://github.com/npm/npm/archive/. Leave [url] blank to default url.") fmt.Println(" nvm uninstall : The version must be a specific version.") // fmt.Println(" nvm update : Automatically update nvm to the latest version.") fmt.Println(" nvm use [version] [arch] : Switch to use the specified version. Optionally specify 32/64bit architecture.") @@ -477,17 +485,7 @@ func help() { // Given a node.js version, returns the associated npm version func getNpmVersion(nodeversion string) string { - - // Get raw text - text := web.GetRemoteTextFile("https://raw.githubusercontent.com/coreybutler/nodedistro/master/nodeversions.json") - - // Parse - var data interface{} - json.Unmarshal([]byte(text), &data); - body := data.(map[string]interface{}) - all := body["all"] - npm := all.(map[string]interface{}) - + npm, _,_ := node.GetAvailabeVersions() return npm[nodeversion].(string) } @@ -505,6 +503,7 @@ func updateRootDir(path string) { func saveSettings() { content := "root: "+strings.Trim(env.root," \n\r")+"\r\narch: "+strings.Trim(env.arch," \n\r")+"\r\nproxy: "+strings.Trim(env.proxy," \n\r")+"\r\noriginalpath: "+strings.Trim(env.originalpath," \n\r")+"\r\noriginalversion: "+strings.Trim(env.originalversion," \n\r") + content = content + "node_mirror: "+strings.Trim(env.node_mirror," \n\r")+ "npm_mirror: "+strings.Trim(env.npm_mirror," \n\r") ioutil.WriteFile(env.settings, []byte(content), 0644) } @@ -525,6 +524,10 @@ func Setup() { env.originalversion = strings.Trim(regexp.MustCompile("originalversion:").ReplaceAllString(line,"")," \r\n") } else if strings.Contains(line,"arch:"){ env.arch = strings.Trim(regexp.MustCompile("arch:").ReplaceAllString(line,"")," \r\n") + } else if strings.Contains(line, "node_mirror:"){ + env.node_mirror = strings.Trim(regexp.MustCompile("node_mirror:").ReplaceAllString(line,"")," \r\n") + } else if strings.Contains(line, "npm_mirror:"){ + env.npm_mirror = strings.Trim(regexp.MustCompile("npm_mirror:").ReplaceAllString(line,"")," \r\n") } else if strings.Contains(line,"proxy:"){ env.proxy = strings.Trim(regexp.MustCompile("proxy:").ReplaceAllString(line,"")," \r\n") if env.proxy != "none" && env.proxy != "" { @@ -535,7 +538,7 @@ func Setup() { } } } - + web.SetMirrors(env.node_mirror, env.npm_mirror) env.arch = arch.Validate(env.arch) // Make sure the directories exist diff --git a/src/nvm/node/node.go b/src/nvm/node/node.go index 739522dfed8665ceefa090aa70181c03c9a49528..cceb310b3ca647c4e698cee17d929f51b4cf6e8b 100644 --- a/src/nvm/node/node.go +++ b/src/nvm/node/node.go @@ -17,7 +17,6 @@ import( * Returns version, architecture */ func GetCurrentVersion() (string, string) { - cmd := exec.Command("node","-v") str, err := cmd.Output() if err == nil { @@ -109,24 +108,51 @@ func (s BySemanticVersion) Less(i, j int) bool { return v1.GTE(v2) } +func GetAvailabeVersions() (map[string]interface{}, map[string]interface{},map[string]interface{}){ + // Check the service to make sure the version is available + // 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? + //text := web.GetRemoteTextFile("https://raw.githubusercontent.com/coreybutler/nodedistro/master/nodeversions.json") + url := web.GetFullNodeUrl("index.json") + text := web.GetRemoteTextFile(url) + // Parse + var data interface{} + json.Unmarshal([]byte(text), &data); + + //body := data.(map[string]interface{}) + //_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) { all := make([]string,0) stable := make([]string,0) unstable := make([]string,0) - // Check the service to make sure the version is available - text := web.GetRemoteTextFile("https://raw.githubusercontent.com/coreybutler/nodedistro/master/nodeversions.json") - - // Parse - var data interface{} - json.Unmarshal([]byte(text), &data); - body := data.(map[string]interface{}) - _all := body["all"] - _stable := body["stable"] - _unstable := body["unstable"] - allkeys := _all.(map[string]interface{}) - stablekeys := _stable.(map[string]interface{}) - unstablekeys := _unstable.(map[string]interface{}) + allkeys, stablekeys, unstablekeys := GetAvailabeVersions() for nodev, _ := range allkeys { all = append(all,nodev) diff --git a/src/nvm/web/web.go b/src/nvm/web/web.go index f0d927d5b6b0c27e4a51ad39f254c3788084cd67..3ca17392f689bdc7e1c5eb49cb2b0421f7cd1b80 100644 --- a/src/nvm/web/web.go +++ b/src/nvm/web/web.go @@ -14,6 +14,8 @@ import( ) var client = &http.Client{} +var nodeBaseAddress = "http://nodejs.org/dist/" +var npmBaseAddress = "https://github.com/npm/npm/archive/" func SetProxy(p string){ if p != "" && p != "none" { @@ -24,6 +26,29 @@ func SetProxy(p string){ } } +func SetMirrors(node_mirror string, npm_mirror string){ + if node_mirror != "" && node_mirror != "none"{ + nodeBaseAddress = node_mirror; + if strings.ToLower(nodeBaseAddress[0:4]) != "http" { + nodeBaseAddress = "http://"+nodeBaseAddress + } + } + if npm_mirror != "" && npm_mirror != "none"{ + npmBaseAddress = npm_mirror; + if strings.ToLower(npmBaseAddress[0:4]) != "http" { + npmBaseAddress = "http://"+npmBaseAddress + } + } +} + +func GetFullNodeUrl(path string) string{ + return nodeBaseAddress+ path; +} + +func GetFullNpmUrl(path string) string{ + return npmBaseAddress + path; +} + func Download(url string, target string) bool { output, err := os.Create(target) @@ -99,7 +124,8 @@ func GetNodeJS(root string, v string, a string) bool { } func GetNpm(root string, v string) bool { - url := "https://github.com/npm/npm/archive/v"+v+".zip" + //url := "https://github.com/npm/npm/archive/v"+v+".zip" + url := GetFullNpmUrl("v"+v+".zip") // temp directory to download the .zip file tempDir := root+"\\temp" @@ -160,15 +186,12 @@ func IsNode64bitAvailable(v string) bool { } func getNodeUrl (v string, vpre string) string { - url := "http://nodejs.org/dist/v"+v+"/" + vpre + "/node.exe" + //url := "http://nodejs.org/dist/v"+v+"/" + vpre + "/node.exe" + url := GetFullNodeUrl("v"+v+"/" + vpre + "/node.exe") // Check online to see if a 64 bit version exists - res, err := client.Head( url ) + _, err := client.Head( url ) if err != nil { return "" } - if res.StatusCode == 200 { - return url - } else { - return "" - } + return url; }