提交 0ee8d12b 编写于 作者: C Corey Butler

Merge pull request #160 from lijie371/master

add node_mirror and npm_mirror
......@@ -25,6 +25,8 @@ type Environment struct {
root string
symlink string
arch string
node_mirror string
npm_mirror string
proxy string
originalpath string
originalversion string
......@@ -35,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: "",
......@@ -119,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),"")
......@@ -156,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),"")
......@@ -402,7 +409,7 @@ func list(listtype string) {
} else {
_, lts, stable, _ := node.GetAvailable()
releases := 15
releases := len(stable)
fmt.Println("\nShowing the "+strconv.Itoa(releases)+" latest available releases.\n")
......@@ -470,6 +477,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 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 uninstall <version> : 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.")
......@@ -502,6 +511,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)
}
......@@ -522,6 +532,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 != "" {
......@@ -532,7 +546,7 @@ func Setup() {
}
}
}
web.SetMirrors(env.node_mirror, env.npm_mirror)
env.arch = arch.Validate(env.arch)
// Make sure the directories exist
......
......@@ -16,7 +16,6 @@ import(
* Returns version, architecture
*/
func GetCurrentVersion() (string, string) {
cmd := exec.Command("node","-v")
str, err := cmd.Output()
if err == nil {
......@@ -113,9 +112,9 @@ func GetAvailable() ([]string, []string, []string, map[string]string) {
lts := make([]string,0)
stable := make([]string,0)
npm := make(map[string]string)
url := web.GetFullNodeUrl("index.json")
// Check the service to make sure the version is available
text := web.GetRemoteTextFile("https://nodejs.org/download/release/index.json")
text := web.GetRemoteTextFile(url)
// Parse
var data = make([]map[string]interface{}, 0)
......
......@@ -14,6 +14,8 @@ import(
)
var client = &http.Client{}
var nodeBaseAddress = "https://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;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册