Merge pull request #408 from creationix/nvm_system_awareness
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Add `system` awareness to `nvm use`, `nvm ls`, and `nvm current`.
authored by Jordan Harband · committed by Jordan Harband · parents 1c50c5c, e7ada80 · Browse files · 4 files·+64 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+64 added−3 removed
- tests:+39 added−1 removed
- other:+25 added−2 removed
Provenance
- Commit
8cbf149- Tree
beefb37- Author
Jordan Harband <ljharb@gmail.com>- Committer
Jordan Harband <ljharb@gmail.com>- Parent 1
1c50c5c- Parent 2
e7ada80
4 files changed+64 added−3 removed
added:test/fast/Listing versions/Running "nvm ls" should include "system" when appropriate+21 added
1+#!/bin/sh2+3+die () { echo $@ ; exit 1; }4+5+. ../../../nvm.sh6+7+mkdir -p ../../../v0.0.18+mkdir -p ../../../v0.0.39+mkdir -p ../../../v0.0.910+mkdir -p ../../../v0.3.111+mkdir -p ../../../v0.3.312+mkdir -p ../../../v0.3.913+14+nvm_has_system_node() { return 0; }15+nvm ls | grep system 2>&1 > /dev/null16+[ $? -eq 0 ] || die '"nvm ls" did not contain "system" when system node is present'17+18+nvm_has_system_node() { return 1; }19+nvm ls | grep system 2>&1 > /dev/null20+[ $? -ne 0 ] || die '"nvm ls" contained "system" when system node is not present'21+modified:test/fast/Running "nvm current" should display current nvm environment.+5 added-1 removed
1 unmodified line2 3 die () { echo $@ ; exit 1; }4 5 . ../../nvm.sh6-[ "$(nvm current)" = "$(node -v)" ] || die "Failed to find current version"6+7+nvm deactivate 2>&18+9+[ "$(nvm current)" = "system" ] || [ "$(nvm current)" = "none" ] || die '"nvm current" did not report "system" or "none" when deactivated'10+added:test/fast/Running "nvm use system" should work as expected+13 added
1+#!/bin/sh2+3+die () { echo $@ ; exit 1; }4+5+. ../../nvm.sh6+7+nvm_has_system_node() { return 0; }8+[ "$(nvm use system 2>&1 | tail -n1)" = "Now using system version of node: $(node -v)." ] || die "Could not use system version of node"9+10+nvm_has_system_node() { return 1; }11+[ "$(nvm use system 2>&1 | tail -n1)" = "System version of node not found." ] || die "Did not report error, system node not found"12+nvm use system 2>&1 > /dev/null || [ $? -eq 127 ] || die "Did not return error code, system node not found"13+modified:nvm.sh+25 added-2 removed
158 unmodified lines159 [ $(nvm_normalize_version $VERSION) -ge $(nvm_normalize_version $MINIMAL) ]160 }161 162 nvm_ls_current() {163+ local NODE_PATH164+ NODE_PATH="$(which node)"165+ if [ $? -ne 0 ]; then166+ echo 'none'167+ elif nvm_tree_contains_path "$NVM_DIR" "$NODE_PATH"; then163- echo `node -v 2>/dev/null`168+ echo `node -v 2>/dev/null`169+ else170+ echo 'system'171+ fi172 }173 174 nvm_ls() {175 local PATTERN25 unmodified lines201 if [ -z "$VERSIONS" ]; then202 echo "N/A"203 return 3204 fi205+ if [ -z "$PATTERN" ] && nvm_has_system_node; then206+ VERSIONS="$VERSIONS$(printf '\n%s' 'system')"207+ fi208 echo "$VERSIONS"209 return210 }211 49 unmodified lines261 if [ "$VERSION" = "$NVM_CURRENT" ]; then262 FORMAT='\033[0;32m-> %9s\033[0m'263 elif [ -d "$NVM_DIR/$VERSION" ]; then264 FORMAT='\033[0;34m%12s\033[0m'265+ elif [ "$VERSION" = "system" ]; then266+ FORMAT='\033[0;33m%12s\033[0m'267 else268 FORMAT='%12s'269 fi270 printf "$FORMAT\n" $VERSION278 unmodified lines549 if [ -n "$NVM_RC_VERSION" ]; then550 VERSION=`nvm_version $NVM_RC_VERSION`551 fi552 else553+ if [ $2 = 'system' ]; then554+ if nvm_has_system_node && nvm deactivate; then555+ echo "Now using system version of node: $(node -v 2>/dev/null)."556+ return557+ else558+ echo "System version of node not found." >&2559+ return 127560+ fi561+ else540- VERSION=`nvm_version $2`562+ VERSION=`nvm_version $2`563+ fi564 fi565 if [ -z "$VERSION" ]; then566 nvm help567 return 127172 unmodified lines