Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
风弥漫了夏天
nvm-windows
提交
404bb7ba
N
nvm-windows
项目概览
风弥漫了夏天
/
nvm-windows
与 Fork 源项目一致
从无法访问的项目Fork
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
N
nvm-windows
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
404bb7ba
编写于
10月 03, 2014
作者:
C
Corey Butler
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added proxy support. Closes issue #5
上级
dbb7a10f
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
35 addition
and
129 deletion
+35
-129
src/nvm.go
src/nvm.go
+20
-2
src/nvm/web/main.go
src/nvm/web/main.go
+0
-124
src/nvm/web/web.go
src/nvm/web/web.go
+15
-3
未找到文件。
src/nvm.go
浏览文件 @
404bb7ba
...
...
@@ -25,6 +25,7 @@ type Environment struct {
root
string
symlink
string
arch
string
proxy
string
}
var
env
=
&
Environment
{
...
...
@@ -32,6 +33,7 @@ var env = &Environment{
root
:
""
,
symlink
:
""
,
arch
:
os
.
Getenv
(
"PROCESSOR_ARCHITECTURE"
),
proxy
:
"none"
,
}
func
main
()
{
...
...
@@ -72,6 +74,13 @@ func main() {
case
"arch"
:
_
,
a
:=
node
.
GetCurrentVersion
()
fmt
.
Println
(
a
+
"-bit"
)
case
"proxy"
:
if
detail
==
""
{
fmt
.
Println
(
"Current proxy: "
+
env
.
proxy
)
}
else
{
env
.
proxy
=
detail
saveSettings
()
}
default
:
help
()
}
}
...
...
@@ -375,10 +384,11 @@ func help() {
fmt
.
Println
(
" nvm install <version> [arch] : The version can be a node.js version or
\"
latest
\"
for the latest stable version."
)
fmt
.
Println
(
" Optionally specify whether to install the 32 or 64 bit version (defaults to system arch)."
)
fmt
.
Println
(
" Set [arch] to
\"
all
\"
to install 32 AND 64 bit versions."
)
fmt
.
Println
(
" nvm list : List
what is currently installed
."
)
fmt
.
Println
(
" nvm list : List
the node.js installations
."
)
fmt
.
Println
(
" nvm on : Enable 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
(
" Set [url] to
\"
none
\"
to remove the proxy."
)
fmt
.
Println
(
" nvm uninstall <version> : The version must be a specific version."
)
fmt
.
Println
(
" nvm use <version> [arch] : Switch to use the specified version. Optionally specify 32/64bit architecture."
)
fmt
.
Println
(
" nvm use <arch> will continue using the selected version, but switch to 32/64 bit mode."
)
...
...
@@ -416,7 +426,7 @@ func updateRootDir(path string) {
}
func
saveSettings
()
{
content
:=
"root: "
+
strings
.
Trim
(
env
.
root
,
"
\n\r
"
)
+
"
\r\n
path: "
+
strings
.
Trim
(
env
.
symlink
,
"
\n\r
"
)
+
"
\r\n
arch: "
+
strings
.
Trim
(
env
.
arch
,
"
\n\r
"
)
content
:=
"root: "
+
strings
.
Trim
(
env
.
root
,
"
\n\r
"
)
+
"
\r\n
path: "
+
strings
.
Trim
(
env
.
symlink
,
"
\n\r
"
)
+
"
\r\n
arch: "
+
strings
.
Trim
(
env
.
arch
,
"
\n\r
"
)
+
"
\r\n
proxy: "
+
strings
.
Trim
(
env
.
proxy
,
"
\n\r
"
)
ioutil
.
WriteFile
(
env
.
settings
,
[]
byte
(
content
),
0644
)
}
...
...
@@ -435,6 +445,14 @@ func Setup() {
env
.
symlink
=
strings
.
Trim
(
regexp
.
MustCompile
(
"path:"
)
.
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
,
"proxy:"
){
env
.
proxy
=
strings
.
Trim
(
regexp
.
MustCompile
(
"proxy:"
)
.
ReplaceAllString
(
line
,
""
),
"
\r\n
"
)
if
env
.
proxy
!=
"none"
&&
env
.
proxy
!=
""
{
if
strings
.
ToLower
(
env
.
proxy
[
0
:
4
])
!=
"http"
{
env
.
proxy
=
"http://"
+
env
.
proxy
}
web
.
SetProxy
(
env
.
proxy
)
}
}
}
...
...
src/nvm/web/main.go
已删除
100644 → 0
浏览文件 @
dbb7a10f
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"
)
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"
)
return
true
}
else
{
return
false
}
}
func
GetRemoteTextFile
(
url
string
)
string
{
response
,
httperr
:=
http
.
Get
(
url
)
if
httperr
!=
nil
{
fmt
.
Println
(
"
\n
Could 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
}
src/nvm/web/web.go
浏览文件 @
404bb7ba
...
...
@@ -3,6 +3,7 @@ package web
import
(
"fmt"
"net/http"
"net/url"
"os"
"io"
"io/ioutil"
...
...
@@ -11,6 +12,17 @@ import(
"../arch"
)
var
client
=
&
http
.
Client
{}
func
SetProxy
(
p
string
){
if
p
!=
""
&&
p
!=
"none"
{
proxyUrl
,
_
:=
url
.
Parse
(
p
)
client
=
&
http
.
Client
{
Transport
:
&
http
.
Transport
{
Proxy
:
http
.
ProxyURL
(
proxyUrl
)}}
}
else
{
client
=
&
http
.
Client
{}
}
}
func
Download
(
url
string
,
target
string
)
bool
{
output
,
err
:=
os
.
Create
(
target
)
...
...
@@ -19,7 +31,7 @@ func Download(url string, target string) bool {
}
defer
output
.
Close
()
response
,
err
:=
http
.
Get
(
url
)
response
,
err
:=
client
.
Get
(
url
)
if
err
!=
nil
{
fmt
.
Println
(
"Error while downloading"
,
url
,
"-"
,
err
)
}
...
...
@@ -84,7 +96,7 @@ func GetNpm(v string) bool {
}
func
GetRemoteTextFile
(
url
string
)
string
{
response
,
httperr
:=
http
.
Get
(
url
)
response
,
httperr
:=
client
.
Get
(
url
)
if
httperr
!=
nil
{
fmt
.
Println
(
"
\n
Could not retrieve "
+
url
+
".
\n\n
"
)
fmt
.
Printf
(
"%s"
,
httperr
)
...
...
@@ -116,7 +128,7 @@ func IsNode64bitAvailable(v string) bool {
}
// Check online to see if a 64 bit version exists
res
,
err
:=
http
.
Head
(
"http://nodejs.org/dist/v"
+
v
+
"/x64/node.exe"
)
res
,
err
:=
client
.
Head
(
"http://nodejs.org/dist/v"
+
v
+
"/x64/node.exe"
)
if
err
!=
nil
{
return
false
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录