Factor out global installs/links into `nvm_npm_global_modules` function.

Jordan Harband 2015-07-02 23:47:29 -07:00
parent be5e8de0c8
commit 0b41352210
1 changed files with 25 additions and 7 deletions

32
nvm.sh
View File

@ -1116,6 +1116,25 @@ nvm_match_version() {
esac esac
} }
nvm_npm_global_modules() {
local NPMLIST
local VERSION
VERSION="$1"
if [ "_$VERSION" = "_system" ]; then
NPMLIST=$(nvm use system > /dev/null && npm list -g --depth=0 | command tail -n +2)
else
NPMLIST=$(nvm use "$VERSION" > /dev/null && npm list -g --depth=0 | command tail -n +2)
fi
local INSTALLS
INSTALLS=$(echo "$NPMLIST" | command sed -e '/ -> / d' -e '/\(empty\)/ d' -e 's/^.* \(.*@[^ ]*\).*/\1/' -e '/^npm@[^ ]*.*$/ d' | command xargs)
local LINKS
LINKS="$(echo "$NPMLIST" | command sed -n 's/.* -> \(.*\)/\1/ p')"
echo "$INSTALLS //// $LINKS"
}
nvm() { nvm() {
if [ $# -lt 1 ]; then if [ $# -lt 1 ]; then
nvm help nvm help
@ -1744,7 +1763,6 @@ $NVM_LS_REMOTE_IOJS_OUTPUT" | command grep -v "N/A" | sed '/^$/d')"
return 2 return 2
fi fi
local NPMLIST
local VERSION local VERSION
if [ "_$PROVIDED_VERSION" = "_system" ]; then if [ "_$PROVIDED_VERSION" = "_system" ]; then
if ! nvm_has_system_node && ! nvm_has_system_iojs; then if ! nvm_has_system_node && ! nvm_has_system_iojs; then
@ -1752,21 +1770,20 @@ $NVM_LS_REMOTE_IOJS_OUTPUT" | command grep -v "N/A" | sed '/^$/d')"
return 3 return 3
fi fi
VERSION="system" VERSION="system"
NPMLIST=$(nvm deactivate > /dev/null && npm list -g --depth=0 | command tail -n +2)
else else
VERSION="$(nvm_version "$PROVIDED_VERSION")" VERSION="$(nvm_version "$PROVIDED_VERSION")"
NPMLIST=$(nvm use "$VERSION" > /dev/null && npm list -g --depth=0 | command tail -n +2)
fi fi
local NPMLIST
NPMLIST="$(nvm_npm_global_modules "$VERSION")"
local INSTALLS local INSTALLS
INSTALLS=$(echo "$NPMLIST" | command sed -e '/ -> / d' -e '/\(empty\)/ d' -e 's/^.* \(.*@[^ ]*\).*/\1/' -e '/^npm@[^ ]*.*$/ d' | command xargs) local LINKS
INSTALLS="${NPMLIST%% //// *}"
LINKS="${NPMLIST##* //// }"
echo "Reinstalling global packages from $VERSION..." echo "Reinstalling global packages from $VERSION..."
echo "$INSTALLS" | command xargs npm install -g --quiet echo "$INSTALLS" | command xargs npm install -g --quiet
local LINKS
LINKS="$(echo "$NPMLIST" | command sed -n 's/.* -> \(.*\)/\1/ p')"
echo "Linking global packages from $VERSION..." echo "Linking global packages from $VERSION..."
set -f; IFS=' set -f; IFS='
' # necessary to turn off variable expansion except for newlines ' # necessary to turn off variable expansion except for newlines
@ -1796,6 +1813,7 @@ $NVM_LS_REMOTE_IOJS_OUTPUT" | command grep -v "N/A" | sed '/^$/d')"
nvm_ls_remote nvm_ls nvm_remote_version nvm_remote_versions \ nvm_ls_remote nvm_ls nvm_remote_version nvm_remote_versions \
nvm_version nvm_rc_version \ nvm_version nvm_rc_version \
nvm_version_greater nvm_version_greater_than_or_equal_to \ nvm_version_greater nvm_version_greater_than_or_equal_to \
nvm_npm_global_modules \
nvm_supports_source_options > /dev/null 2>&1 nvm_supports_source_options > /dev/null 2>&1
unset RC_VERSION NVM_NODEJS_ORG_MIRROR NVM_DIR NVM_CD_FLAGS > /dev/null 2>&1 unset RC_VERSION NVM_NODEJS_ORG_MIRROR NVM_DIR NVM_CD_FLAGS > /dev/null 2>&1
;; ;;