diff --git a/install.sh b/install.sh index b055eee..68c9627 100755 --- a/install.sh +++ b/install.sh @@ -6,6 +6,10 @@ nvm_has() { type "$1" > /dev/null 2>&1 } +nvm_has_http_proxy() { + [ "$NVM_HTTP_PROXY" != '' ] +} + if [ -z "$NVM_DIR" ]; then NVM_DIR="$HOME/.nvm" fi @@ -42,7 +46,11 @@ nvm_source() { nvm_download() { if nvm_has "curl"; then - curl -q $* + if nvm_has_http_proxy; then + curl -q -x $NVM_HTTP_PROXY $* + else + curl -q $* + fi elif nvm_has "wget"; then # Emulate curl with wget ARGS=$(echo "$*" | command sed -e 's/--progress-bar /--progress=bar /' \ @@ -51,7 +59,11 @@ nvm_download() { -e 's/-s /-q /' \ -e 's/-o /-O /' \ -e 's/-C - /-c /') - wget $ARGS + if nvm_has_http_proxy; then + eval wget -e use_proxy=yes -e http_proxy=$NVM_HTTP_PROXY $ARGS + else + eval wget $ARGS + fi fi } @@ -229,7 +241,11 @@ nvm_do_install() { local NVM_PROFILE NVM_PROFILE=$(nvm_detect_profile) - SOURCE_STR="\nexport NVM_DIR=\"$NVM_DIR\"\n[ -s \"\$NVM_DIR/nvm.sh\" ] && . \"\$NVM_DIR/nvm.sh\" # This loads nvm" + if nvm_has_http_proxy; then + SOURCE_STR="\nexport NVM_HTTP_PROXY=$NVM_HTTP_PROXY\nexport NVM_DIR=\"$NVM_DIR\"\n[ -s \"\$NVM_DIR/nvm.sh\" ] && . \"\$NVM_DIR/nvm.sh\" # This loads nvm" + else + SOURCE_STR="\nexport NVM_DIR=\"$NVM_DIR\"\n[ -s \"\$NVM_DIR/nvm.sh\" ] && . \"\$NVM_DIR/nvm.sh\" # This loads nvm" + fi if [ -z "$NVM_PROFILE" ] ; then echo "=> Profile not found. Tried $NVM_PROFILE (as defined in \$PROFILE), ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile." diff --git a/nvm.sh b/nvm.sh index 9980515..797d7b4 100755 --- a/nvm.sh +++ b/nvm.sh @@ -22,9 +22,17 @@ nvm_is_alias() { nvm_get_latest() { local NVM_LATEST_URL if nvm_has "curl"; then - NVM_LATEST_URL="$(curl -q -w "%{url_effective}\n" -L -s -S http://latest.nvm.sh -o /dev/null)" + if nvm_has_http_proxy; then + NVM_LATEST_URL="$(curl -q -x $NVM_HTTP_PROXY -w "%{url_effective}\n" -L -s -S http://latest.nvm.sh -o /dev/null)" + else + NVM_LATEST_URL="$(curl -q -w "%{url_effective}\n" -L -s -S http://latest.nvm.sh -o /dev/null)" + fi elif nvm_has "wget"; then - NVM_LATEST_URL="$(wget http://latest.nvm.sh --server-response -O /dev/null 2>&1 | command awk '/^ Location: /{DEST=$2} END{ print DEST }')" + if nvm_has_http_proxy; then + NVM_LATEST_URL="$(wget http://latest.nvm.sh -e use_proxy=yes -e http_proxy=$NVM_HTTP_PROXY --server-response -O /dev/null 2>&1 | command awk '/^ Location: /{DEST=$2} END{ print DEST }')" + else + NVM_LATEST_URL="$(wget http://latest.nvm.sh --server-response -O /dev/null 2>&1 | command awk '/^ Location: /{DEST=$2} END{ print DEST }')" + fi else >&2 echo 'nvm needs curl or wget to proceed.' return 1 @@ -39,7 +47,11 @@ nvm_get_latest() { nvm_download() { if nvm_has "curl"; then - curl -q $* + if nvm_has_http_proxy; then + curl -q -x $NVM_HTTP_PROXY $* + else + curl -q $* + fi elif nvm_has "wget"; then # Emulate curl with wget ARGS=$(echo "$*" | command sed -e 's/--progress-bar /--progress=bar /' \ @@ -48,7 +60,11 @@ nvm_download() { -e 's/-s /-q /' \ -e 's/-o /-O /' \ -e 's/-C - /-c /') - eval wget $ARGS + if nvm_has_http_proxy; then + eval wget -e use_proxy=yes -e http_proxy=$NVM_HTTP_PROXY $ARGS + else + eval wget $ARGS + fi fi } @@ -60,6 +76,10 @@ nvm_has_system_iojs() { [ "$(nvm deactivate >/dev/null 2>&1 && command -v iojs)" != '' ] } +nvm_has_http_proxy() { + [ "$NVM_HTTP_PROXY" != '' ] +} + nvm_print_npm_version() { if nvm_has "npm"; then echo " (npm v$(npm --version 2>/dev/null))" diff --git a/test/fast/Unit tests/nvm_has_http_proxy b/test/fast/Unit tests/nvm_has_http_proxy new file mode 100755 index 0000000..1584484 --- /dev/null +++ b/test/fast/Unit tests/nvm_has_http_proxy @@ -0,0 +1,10 @@ +#!/bin/sh + +die () { echo $@ ; exit 1; } + +. ../../../nvm.sh + +! nvm_has_http_proxy || die 'nvm_has_http_proxy: no http_proxy defined' + +NVM_HTTP_PROXY=127.0.0.1:1234 +nvm_has_http_proxy || die 'nvm_has_http_proxy: http_proxy defined'