diff --git a/src/nvm.go b/src/nvm.go index 6ba994d..5bd4a62 100644 --- a/src/nvm.go +++ b/src/nvm.go @@ -193,6 +193,11 @@ func uninstall(version string) { // Determine if the version exists and skip if it doesn't if node.IsVersionInstalled(env.root,version,"32") || node.IsVersionInstalled(env.root,version,"64") { fmt.Printf("Uninstalling node v"+version+"...") + v, _ := node.GetCurrentVersion() + if v == version { + cmd := exec.Command(env.root+"\\elevate.cmd", "cmd", "/C", "rmdir", env.symlink) + cmd.Run() + } e := os.RemoveAll(env.root+"\\v"+version) if e != nil { fmt.Println("Error removing node v"+version) diff --git a/src/nvm/node/node.go b/src/nvm/node/node.go index 74533df..c018db7 100644 --- a/src/nvm/node/node.go +++ b/src/nvm/node/node.go @@ -38,7 +38,7 @@ func IsVersionInstalled(root string, version string, cpu string) bool { if ((e32||e64) && used) || (e32 && e64) { return true } - if !e32 && !e64 && used && arch.Validate(cpu) == arch.Validate("") { + if !e32 && !e64 && used && arch.Validate(cpu) == arch.Bit(root+"\\v"+version+"\\node.exe") { return true } if cpu == "32" { diff --git a/src/nvm/web/web.go b/src/nvm/web/web.go new file mode 100644 index 0000000..4529fb2 --- /dev/null +++ b/src/nvm/web/web.go @@ -0,0 +1,124 @@ +package web + +import( + "fmt" + "net/http" + "os" + "io" + "io/ioutil" + "strings" + "strconv" + "../arch" +) + +func Download(url string, target string) bool { + + output, err := os.Create(target) + if err != nil { + fmt.Println("Error while creating", target, "-", err) + } + defer output.Close() + + response, err := http.Get(url) + if err != nil { + fmt.Println("Error while downloading", url, "-", err) + } + defer response.Body.Close() + + n, err := io.Copy(output, response.Body) + n=n + if err != nil { + fmt.Println("Error while downloading", url, "-", err) + } + + if response.Status[0:3] != "200" { + fmt.Println("Download failed. Rolling Back.") + err := os.Remove(target) + if err != nil { + fmt.Println("Rollback failed.",err) + } + return false + } + + return true +} + +func GetNodeJS(root string, v string, a string) bool { + + a = arch.Validate(a) + + url := "" + if a == "32" { + url = "http://nodejs.org/dist/v"+v+"/node.exe" + } else { + if !IsNode64bitAvailable(v) { + fmt.Println("Node.js v"+v+" is only available in 32-bit.") + return false + } + url = "http://nodejs.org/dist/v"+v+"/x64/node.exe" + } + fileName := root+"\\v"+v+"\\node"+a+".exe" + + fmt.Printf("Downloading node.js version "+v+" ("+a+"-bit)... ") + + if Download(url,fileName) { + fmt.Printf("Complete\n") + return true + } else { + return false + } + +} + +func GetNpm(v string) bool { + url := "https://github.com/npm/npm/archive/v"+v+".zip" + fileName := os.TempDir()+"\\"+"npm-v"+v+".zip" + + fmt.Printf("Downloading npm version "+v+"... ") + if Download(url,fileName) { + fmt.Printf("Complete\n") + return true + } else { + return false + } +} + +func GetRemoteTextFile(url string) string { + response, httperr := http.Get(url) + if httperr != nil { + fmt.Println("\nCould not retrieve "+url+".\n\n") + fmt.Printf("%s", httperr) + os.Exit(1) + } else { + defer response.Body.Close() + contents, readerr := ioutil.ReadAll(response.Body) + if readerr != nil { + fmt.Printf("%s", readerr) + os.Exit(1) + } + return string(contents) + } + os.Exit(1) + return "" +} + +func IsNode64bitAvailable(v string) bool { + if v == "latest" { + return true + } + + // Anything below version 8 doesn't have a 64 bit version + vers := strings.Fields(strings.Replace(v,"."," ",-1)) + main, _ := strconv.ParseInt(vers[0],0,0) + minor, _ := strconv.ParseInt(vers[1],0,0) + if main == 0 && minor < 8 { + return false + } + + // Check online to see if a 64 bit version exists + res, err := http.Head("http://nodejs.org/dist/v"+v+"/x64/node.exe") + if err != nil { + return false + } + return res.StatusCode == 200 +}