Merge pull request #602 from ljharb/install_on_source

Support `--install` option on sourcing `nvm.sh`
diff --git a/nvm.sh b/nvm.sh
index 0a42275..f907a86 100644
--- a/nvm.sh
+++ b/nvm.sh
@@ -1102,7 +1102,7 @@
       echo "0.21.0"
     ;;
     "unload" )
-      unset -f nvm nvm_print_versions nvm_checksum nvm_ls_remote nvm_ls nvm_remote_version nvm_version nvm_rc_version nvm_version_greater nvm_version_greater_than_or_equal_to > /dev/null 2>&1
+      unset -f nvm nvm_print_versions nvm_checksum nvm_ls_remote nvm_ls nvm_remote_version nvm_version nvm_rc_version nvm_version_greater nvm_version_greater_than_or_equal_to nvm_supports_source_options > /dev/null 2>&1
       unset RC_VERSION NVM_NODEJS_ORG_MIRROR NVM_DIR NVM_CD_FLAGS > /dev/null 2>&1
     ;;
     * )
@@ -1111,7 +1111,18 @@
   esac
 }
 
-if nvm ls default >/dev/null; then
+nvm_supports_source_options() {
+  [ "_$(echo 'echo $1' | . /dev/stdin yes)" = "_yes" ]
+}
+
+if nvm_supports_source_options && [ "_$1" = "_--install" ]; then
+  VERSION="$(nvm_alias default 2>/dev/null)"
+  if [ -n "$VERSION" ]; then
+    nvm install "$VERSION" >/dev/null
+  elif nvm_rc_version >/dev/null 2>&1; then
+    nvm install >/dev/null
+  fi
+elif nvm ls default >/dev/null; then
   nvm use default >/dev/null
 elif nvm_rc_version >/dev/null 2>&1; then
   nvm use >/dev/null
diff --git a/test/sourcing/Sourcing nvm.sh with --install and .nvmrc should install it b/test/sourcing/Sourcing nvm.sh with --install and .nvmrc should install it
new file mode 100755
index 0000000..edc5614
--- /dev/null
+++ b/test/sourcing/Sourcing nvm.sh with --install and .nvmrc should install it
@@ -0,0 +1,28 @@
+#!/bin/sh
+
+die () { echo $@ ; exit 1; }
+supports_source_options () {
+  [ "_$(echo 'echo $1' | . /dev/stdin yes)" = "_yes" ]
+}
+
+if ! supports_source_options; then
+  echo 'this shell does not support passing options on sourcing'
+  exit 0;
+fi
+
+echo '0.10.2' > ../../.nvmrc || die 'creation of .nvmrc failed'
+
+. ../../nvm.sh --install
+EXIT_CODE="$(echo $?)"
+
+echo 'sourcing complete.'
+
+nvm_version 0.10.2 >/dev/null 2>&1 || die "v0.10.2 not installed: $(nvm ls)"
+
+[ "_$(nvm_rc_version | \grep -o -e 'with version .*$')" = "_with version <0.10.2>" ] || die "nvm_rc_version $(nvm_rc_version)"
+
+[ "_$EXIT_CODE" = "_0" ] || die "sourcing returned nonzero exit code: $EXIT_CODE"
+
+NVM_LS_CURRENT="$(nvm ls current | \grep -o v0.10.2)"
+[ "_$NVM_LS_CURRENT" = '_v0.10.2' ] || die "'nvm ls current' did not return '-> v0.10.2', got '$NVM_LS_CURRENT' `nvm ls`"
+
diff --git a/test/sourcing/Sourcing nvm.sh with --install should install the default b/test/sourcing/Sourcing nvm.sh with --install should install the default
new file mode 100755
index 0000000..e25ddf0
--- /dev/null
+++ b/test/sourcing/Sourcing nvm.sh with --install should install the default
@@ -0,0 +1,32 @@
+#!/bin/sh
+
+die () { echo $@ ; exit 1; }
+supports_source_options () {
+  [ "_$(echo 'echo $1' | . /dev/stdin yes)" = "_yes" ]
+}
+
+if ! supports_source_options; then
+  echo 'this shell does not support passing options on sourcing'
+  exit 0;
+fi
+
+echo '0.10.2' > ../../alias/default || die 'creation of default alias failed'
+
+echo 'sourcing nvm with --install...'
+
+. ../../nvm.sh --install
+EXIT_CODE="$(echo $?)"
+
+echo 'sourcing complete.'
+
+nvm_version 0.10.2 >/dev/null 2>&1 || die "v0.10.2 not installed: $(nvm ls)"
+
+[ "_$EXIT_CODE" = "_0" ] || die "sourcing returned nonzero exit code: $EXIT_CODE"
+
+NVM_LS_CURRENT="$(nvm ls current | \grep -o v0.10.2)"
+[ "_$NVM_LS_CURRENT" = '_v0.10.2' ] || die "'nvm ls current' did not return '-> v0.10.2', got '$NVM_LS_CURRENT'"
+
+NVM_ALIAS_DEFAULT="$(nvm alias default)"
+[ "_$NVM_ALIAS_DEFAULT" = "_default -> 0.10.2 (-> v0.10.2)" ] \
+  || die "'nvm alias default did not return 'default -> 0.10.2 (-> v0.10.2)', got '$NVM_ALIAS_DEFAULT'"
+