[Fix] `nvm install -s`: npm detection

- add `nvm_use_if_needed`
 - add `nvm_install_npm_if_needed`
Jordan Harband 2017-03-27 15:00:50 -07:00
parent 676fff0219
commit b9f9977cdd
No known key found for this signature in database
GPG Key ID: 64A196AEE0916D55
3 changed files with 57 additions and 19 deletions

49
nvm.sh
View File

@ -1926,20 +1926,6 @@ nvm_install_source() {
command rm -f "${VERSION_PATH}" 2>/dev/null && \ command rm -f "${VERSION_PATH}" 2>/dev/null && \
$make -j "${NVM_MAKE_JOBS}" ${MAKE_CXX-} install $make -j "${NVM_MAKE_JOBS}" ${MAKE_CXX-} install
); then ); then
if ! nvm_has "npm" ; then
nvm_echo 'Installing npm...'
if nvm_version_greater 0.2.0 "$VERSION"; then
nvm_err 'npm requires node v0.2.3 or higher'
elif nvm_version_greater_than_or_equal_to "$VERSION" 0.2.0; then
if nvm_version_greater 0.2.3 "$VERSION"; then
nvm_err 'npm requires node v0.2.3 or higher'
else
nvm_download -L https://npmjs.org/install.sh -o - | clean=yes npm_install=0.2.19 sh
fi
else
nvm_download -L https://npmjs.org/install.sh -o - | clean=yes sh
fi
fi
return $? return $?
fi fi
@ -1948,6 +1934,33 @@ nvm_install_source() {
return 1 return 1
} }
nvm_use_if_needed() {
if [ "_${1-}" = "_$(nvm_ls_current)" ]; then
return
fi
nvm use "$@"
}
nvm_install_npm_if_needed() {
local VERSION
VERSION="$(nvm_ls_current)"
if ! nvm_has "npm"; then
nvm_echo 'Installing npm...'
if nvm_version_greater 0.2.0 "$VERSION"; then
nvm_err 'npm requires node v0.2.3 or higher'
elif nvm_version_greater_than_or_equal_to "$VERSION" 0.2.0; then
if nvm_version_greater 0.2.3 "$VERSION"; then
nvm_err 'npm requires node v0.2.3 or higher'
else
nvm_download -L https://npmjs.org/install.sh -o - | clean=yes npm_install=0.2.19 sh
fi
else
nvm_download -L https://npmjs.org/install.sh -o - | clean=yes sh
fi
fi
return $?
}
nvm_match_version() { nvm_match_version() {
local NVM_IOJS_PREFIX local NVM_IOJS_PREFIX
NVM_IOJS_PREFIX="$(nvm_iojs_prefix)" NVM_IOJS_PREFIX="$(nvm_iojs_prefix)"
@ -2505,7 +2518,7 @@ nvm() {
fi fi
if [ "$EXIT_CODE" -eq 0 ] && nvm use "$VERSION"; then if [ "$EXIT_CODE" -eq 0 ] && nvm_use_if_needed "${VERSION}" && nvm_install_npm_if_needed "${VERSION}"; then
if [ -n "${LTS-}" ]; then if [ -n "${LTS-}" ]; then
nvm_ensure_default_set "lts/${LTS}" nvm_ensure_default_set "lts/${LTS}"
else else
@ -2516,6 +2529,8 @@ nvm() {
nvm reinstall-packages "$REINSTALL_PACKAGES_FROM" nvm reinstall-packages "$REINSTALL_PACKAGES_FROM"
EXIT_CODE=$? EXIT_CODE=$?
fi fi
else
EXIT_CODE=$?
fi fi
return $EXIT_CODE return $EXIT_CODE
;; ;;
@ -3216,9 +3231,9 @@ nvm() {
nvm_is_iojs_version nvm_is_alias nvm_has_non_aliased \ nvm_is_iojs_version nvm_is_alias nvm_has_non_aliased \
nvm_ls_remote nvm_ls_remote_iojs nvm_ls_remote_index_tab \ nvm_ls_remote nvm_ls_remote_iojs nvm_ls_remote_index_tab \
nvm_ls nvm_remote_version nvm_remote_versions \ nvm_ls nvm_remote_version nvm_remote_versions \
nvm_install_binary nvm_clang_version \ nvm_install_binary nvm_install_source nvm_clang_version \
nvm_get_mirror nvm_get_download_slug nvm_download_artifact \ nvm_get_mirror nvm_get_download_slug nvm_download_artifact \
nvm_install_source nvm_check_file_permissions \ nvm_install_npm_if_needed nvm_use_if_needed nvm_check_file_permissions \
nvm_print_versions nvm_compute_checksum nvm_checksum \ nvm_print_versions nvm_compute_checksum nvm_checksum \
nvm_get_checksum_alg nvm_get_checksum nvm_compare_checksum \ nvm_get_checksum_alg nvm_get_checksum nvm_compare_checksum \
nvm_version nvm_rc_version nvm_match_version \ nvm_version nvm_rc_version nvm_match_version \

View File

@ -0,0 +1,24 @@
#!/bin/sh
cleanup() { unset -f nvm_ls_current nvm; }
die () { echo "$@" ; cleanup ; exit 1; }
\. ../../../nvm.sh
nvm_ls_current() { echo foo; }
nvm() {
echo "nvm: $@"
}
OUTPUT="$(nvm_use_if_needed foo)"
EXPECTED_OUTPUT=''
[ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<; got >${OUTPUT}<"
OUTPUT="$(nvm_use_if_needed bar)"
EXPECTED_OUTPUT='nvm: use bar'
[ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<; got >${OUTPUT}<"
cleanup

View File

@ -23,8 +23,7 @@ OUTPUT="$(NVM_INSTALL_THIRD_PARTY_HOOK=succeed nvm install "${VERSION}")"
USE_OUTPUT="$(nvm use "${VERSION}")" USE_OUTPUT="$(nvm use "${VERSION}")"
EXPECTED_OUTPUT="${VERSION} node std binary ${VERSION_PATH} EXPECTED_OUTPUT="${VERSION} node std binary ${VERSION_PATH}
Downloading and installing node ${VERSION}... Downloading and installing node ${VERSION}...
${USE_OUTPUT} ${USE_OUTPUT}"
${USE_OUTPUT}" # double use output is from the normal install in succeed()
[ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<; got >${OUTPUT}<" [ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<; got >${OUTPUT}<"