[Refactor] nvm install/`nvm_install_default_packages`: move "get" logic inside helper

Jordan Harband 2022-10-13 15:03:36 -07:00
parent 0698ede92d
commit f062cca179
No known key found for this signature in database
GPG Key ID: 9F6A681E35EF8B56
1 changed files with 23 additions and 21 deletions

44
nvm.sh
View File

@ -3093,7 +3093,6 @@ nvm() {
local PROVIDED_REINSTALL_PACKAGES_FROM local PROVIDED_REINSTALL_PACKAGES_FROM
local REINSTALL_PACKAGES_FROM local REINSTALL_PACKAGES_FROM
local SKIP_DEFAULT_PACKAGES local SKIP_DEFAULT_PACKAGES
local DEFAULT_PACKAGES
while [ $# -ne 0 ]; do while [ $# -ne 0 ]; do
case "$1" in case "$1" in
@ -3293,14 +3292,6 @@ nvm() {
shift shift
done done
if [ -z "${SKIP_DEFAULT_PACKAGES-}" ]; then
DEFAULT_PACKAGES="$(nvm_get_default_packages)"
EXIT_CODE=$?
if [ $EXIT_CODE -ne 0 ]; then
return $EXIT_CODE
fi
fi
if [ -n "${PROVIDED_REINSTALL_PACKAGES_FROM-}" ] && [ "$(nvm_ensure_version_prefix "${PROVIDED_REINSTALL_PACKAGES_FROM}")" = "${VERSION}" ]; then if [ -n "${PROVIDED_REINSTALL_PACKAGES_FROM-}" ] && [ "$(nvm_ensure_version_prefix "${PROVIDED_REINSTALL_PACKAGES_FROM}")" = "${VERSION}" ]; then
nvm_err "You can't reinstall global packages from the same version of node you're installing." nvm_err "You can't reinstall global packages from the same version of node you're installing."
return 4 return 4
@ -3316,17 +3307,22 @@ nvm() {
FLAVOR="$(nvm_node_prefix)" FLAVOR="$(nvm_node_prefix)"
fi fi
local EXIT_CODE
if nvm_is_version_installed "${VERSION}"; then if nvm_is_version_installed "${VERSION}"; then
nvm_err "${VERSION} is already installed." nvm_err "${VERSION} is already installed."
if nvm use "${VERSION}"; then if nvm use "${VERSION}"; then
if [ "${NVM_UPGRADE_NPM}" = 1 ]; then if [ "${NVM_UPGRADE_NPM}" = 1 ]; then
nvm install-latest-npm nvm install-latest-npm
EXIT_CODE=$?
fi fi
if [ -z "${SKIP_DEFAULT_PACKAGES-}" ] && [ -n "${DEFAULT_PACKAGES-}" ]; then if [ $EXIT_CODE -ne 0 ] && [ -z "${SKIP_DEFAULT_PACKAGES-}" ]; then
nvm_install_default_packages "${DEFAULT_PACKAGES}" nvm_install_default_packages
EXIT_CODE=$?
fi fi
if [ -n "${REINSTALL_PACKAGES_FROM-}" ] && [ "_${REINSTALL_PACKAGES_FROM}" != "_N/A" ]; then if [ $EXIT_CODE -ne 0 ] && [ -n "${REINSTALL_PACKAGES_FROM-}" ] && [ "_${REINSTALL_PACKAGES_FROM}" != "_N/A" ]; then
nvm reinstall-packages "${REINSTALL_PACKAGES_FROM}" nvm reinstall-packages "${REINSTALL_PACKAGES_FROM}"
EXIT_CODE=$?
fi fi
fi fi
if [ -n "${LTS-}" ]; then if [ -n "${LTS-}" ]; then
@ -3336,15 +3332,14 @@ nvm() {
nvm_ensure_default_set "${provided_version}" nvm_ensure_default_set "${provided_version}"
fi fi
if [ -n "${ALIAS-}" ]; then if [ $EXIT_CODE -ne 0 ] && [ -n "${ALIAS-}" ]; then
nvm alias "${ALIAS}" "${provided_version}" nvm alias "${ALIAS}" "${provided_version}"
EXIT_CODE=$?
fi fi
return $? return $EXIT_CODE
fi fi
local EXIT_CODE
EXIT_CODE=-1
if [ -n "${NVM_INSTALL_THIRD_PARTY_HOOK-}" ]; then if [ -n "${NVM_INSTALL_THIRD_PARTY_HOOK-}" ]; then
nvm_err '** $NVM_INSTALL_THIRD_PARTY_HOOK env var set; dispatching to third-party installation method **' nvm_err '** $NVM_INSTALL_THIRD_PARTY_HOOK env var set; dispatching to third-party installation method **'
local NVM_METHOD_PREFERENCE local NVM_METHOD_PREFERENCE
@ -3413,10 +3408,11 @@ nvm() {
nvm install-latest-npm nvm install-latest-npm
EXIT_CODE=$? EXIT_CODE=$?
fi fi
if [ -z "${SKIP_DEFAULT_PACKAGES-}" ] && [ -n "${DEFAULT_PACKAGES-}" ]; then if [ $EXIT_CODE -eq 0 ] && [ -z "${SKIP_DEFAULT_PACKAGES-}" ]; then
nvm_install_default_packages "${DEFAULT_PACKAGES}" nvm_install_default_packages
EXIT_CODE=$?
fi fi
if [ -n "${REINSTALL_PACKAGES_FROM-}" ] && [ "_${REINSTALL_PACKAGES_FROM}" != "_N/A" ]; then if [ $EXIT_CODE -eq 0 ] && [ -n "${REINSTALL_PACKAGES_FROM-}" ] && [ "_${REINSTALL_PACKAGES_FROM}" != "_N/A" ]; then
nvm reinstall-packages "${REINSTALL_PACKAGES_FROM}" nvm reinstall-packages "${REINSTALL_PACKAGES_FROM}"
EXIT_CODE=$? EXIT_CODE=$?
fi fi
@ -4286,10 +4282,16 @@ nvm_get_default_packages() {
} }
nvm_install_default_packages() { nvm_install_default_packages() {
local DEFAULT_PACKAGES
DEFAULT_PACKAGES="$(nvm_get_default_packages)"
EXIT_CODE=$?
if [ $EXIT_CODE -ne 0 ] || [ -z "${DEFAULT_PACKAGES}" ]; then
return $EXIT_CODE
fi
nvm_echo "Installing default global packages from ${NVM_DIR}/default-packages..." nvm_echo "Installing default global packages from ${NVM_DIR}/default-packages..."
nvm_echo "npm install -g --quiet $1" nvm_echo "npm install -g --quiet ${DEFAULT_PACKAGES}"
if ! nvm_echo "$1" | command xargs npm install -g --quiet; then if ! nvm_echo "${DEFAULT_PACKAGES}" | command xargs npm install -g --quiet; then
nvm_err "Failed installing default packages. Please check if your default-packages file or a package in it has problems!" nvm_err "Failed installing default packages. Please check if your default-packages file or a package in it has problems!"
return 1 return 1
fi fi