mirror of
https://github.com/coreybutler/nvm-windows.git
synced 2025-09-04 11:48:53 +00:00
Merge pull request #251 from Excited-ccccly/master
Fix build issue.Fix node_mirror, npm_mirror not work issue.Add cleanup work when download is interrupted by user.
This commit is contained in:
@@ -74,6 +74,8 @@ NVM for Windows is a command line tool. Simply type `nvm` in the console for hel
|
||||
- `nvm use <version> [arch]`: Switch to use the specified version. Optionally specify 32/64bit architecture. `nvm use <arch>` will continue using the selected version, but switch to 32/64 bit mode based on the value supplied to `<arch>`.
|
||||
- `nvm root <path>`: Set the directory where nvm should store different versions of node.js. If `<path>` is not set, the current root will be displayed.
|
||||
- `nvm version`: Displays the current running version of NVM for Windows.
|
||||
- `nvm node_mirror <node_mirror_url>`: Set the node mirror.People in China can use *https://npm.taobao.org/mirrors/node/*
|
||||
- `nvm npm_mirror <npm_mirror_url>`: Set the npm mirror.People in China can use *https://npm.taobao.org/mirrors/npm/*
|
||||
|
||||
### Gotcha!
|
||||
|
||||
|
2
nvm.iss
2
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\nvm-windows"
|
||||
#define ProjectRoot "."
|
||||
|
||||
[Setup]
|
||||
; NOTE: The value of AppId uniquely identifies this application.
|
||||
|
16
src/nvm.go
16
src/nvm.go
@@ -108,10 +108,22 @@ func main() {
|
||||
saveSettings()
|
||||
}
|
||||
case "update": update()
|
||||
case "node_mirror": setNodeMirror(detail)
|
||||
case "npm_mirror": setNpmMirror(detail)
|
||||
default: help()
|
||||
}
|
||||
}
|
||||
|
||||
func setNodeMirror(detail string) {
|
||||
env.node_mirror = detail
|
||||
saveSettings()
|
||||
}
|
||||
|
||||
func setNpmMirror(detail string) {
|
||||
env.npm_mirror = detail
|
||||
saveSettings()
|
||||
}
|
||||
|
||||
func update() {
|
||||
// cmd := exec.Command("cmd", "/d", "echo", "testing")
|
||||
// var output bytes.Buffer
|
||||
@@ -574,8 +586,8 @@ 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")
|
||||
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 + "\r\nnode_mirror: " + strings.Trim(env.node_mirror, " \n\r") + "\r\nnpm_mirror: " + strings.Trim(env.npm_mirror, " \n\r")
|
||||
ioutil.WriteFile(env.settings, []byte(content), 0644)
|
||||
}
|
||||
|
||||
|
@@ -5,10 +5,12 @@ import(
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/signal"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
"syscall"
|
||||
"crypto/tls"
|
||||
"strings"
|
||||
"strconv"
|
||||
"../arch"
|
||||
"../file"
|
||||
@@ -50,7 +52,7 @@ func GetFullNpmUrl(path string) string{
|
||||
return npmBaseAddress + path;
|
||||
}
|
||||
|
||||
func Download(url string, target string) bool {
|
||||
func Download(url string, target string, version string) bool {
|
||||
|
||||
output, err := os.Create(target)
|
||||
if err != nil {
|
||||
@@ -63,12 +65,28 @@ func Download(url string, target string) bool {
|
||||
fmt.Println("Error while downloading", url, "-", err)
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
c := make(chan os.Signal, 2)
|
||||
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
||||
go func() {
|
||||
<-c
|
||||
fmt.Println("Download interrupted.Rolling back...")
|
||||
output.Close()
|
||||
response.Body.Close()
|
||||
var err error
|
||||
if strings.Contains(target, "node") {
|
||||
err = os.RemoveAll(os.Getenv("NVM_HOME") + "\\v" + version)
|
||||
} else {
|
||||
err = os.Remove(target)
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Println("Error while rolling back", err)
|
||||
}
|
||||
os.Exit(1)
|
||||
}()
|
||||
_, err = io.Copy(output, response.Body)
|
||||
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)
|
||||
@@ -111,9 +129,9 @@ func GetNodeJS(root string, v string, a string) bool {
|
||||
} else {
|
||||
fileName := root+"\\v"+v+"\\node"+a+".exe"
|
||||
|
||||
fmt.Printf("Downloading node.js version "+v+" ("+a+"-bit)... ")
|
||||
fmt.Println("Downloading node.js version "+v+" ("+a+"-bit)... ")
|
||||
|
||||
if Download(url,fileName) {
|
||||
if Download(url,fileName,v) {
|
||||
fmt.Printf("Complete\n")
|
||||
return true
|
||||
} else {
|
||||
@@ -142,7 +160,7 @@ func GetNpm(root string, v string) bool {
|
||||
fileName := tempDir+"\\"+"npm-v"+v+".zip"
|
||||
|
||||
fmt.Printf("Downloading npm version "+v+"... ")
|
||||
if Download(url,fileName) {
|
||||
if Download(url,fileName,v) {
|
||||
fmt.Printf("Complete\n")
|
||||
return true
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user