Skip to content

Commit f8b143c

Browse files
committed
Merge pull request #602 from ljharb/install_on_source
Support `--install` option on sourcing `nvm.sh`
2 parents 274369d + 9a4e906 commit f8b143c

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

nvm.sh

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,7 @@ nvm() {
11021102
echo "0.21.0"
11031103
;;
11041104
"unload" )
1105-
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
1105+
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
11061106
unset RC_VERSION NVM_NODEJS_ORG_MIRROR NVM_DIR NVM_CD_FLAGS > /dev/null 2>&1
11071107
;;
11081108
* )
@@ -1111,7 +1111,18 @@ nvm() {
11111111
esac
11121112
}
11131113

1114-
if nvm ls default >/dev/null; then
1114+
nvm_supports_source_options() {
1115+
[ "_$(echo 'echo $1' | . /dev/stdin yes)" = "_yes" ]
1116+
}
1117+
1118+
if nvm_supports_source_options && [ "_$1" = "_--install" ]; then
1119+
VERSION="$(nvm_alias default 2>/dev/null)"
1120+
if [ -n "$VERSION" ]; then
1121+
nvm install "$VERSION" >/dev/null
1122+
elif nvm_rc_version >/dev/null 2>&1; then
1123+
nvm install >/dev/null
1124+
fi
1125+
elif nvm ls default >/dev/null; then
11151126
nvm use default >/dev/null
11161127
elif nvm_rc_version >/dev/null 2>&1; then
11171128
nvm use >/dev/null
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/sh
2+
3+
die () { echo $@ ; exit 1; }
4+
supports_source_options () {
5+
[ "_$(echo 'echo $1' | . /dev/stdin yes)" = "_yes" ]
6+
}
7+
8+
if ! supports_source_options; then
9+
echo 'this shell does not support passing options on sourcing'
10+
exit 0;
11+
fi
12+
13+
echo '0.10.2' > ../../.nvmrc || die 'creation of .nvmrc failed'
14+
15+
. ../../nvm.sh --install
16+
EXIT_CODE="$(echo $?)"
17+
18+
echo 'sourcing complete.'
19+
20+
nvm_version 0.10.2 >/dev/null 2>&1 || die "v0.10.2 not installed: $(nvm ls)"
21+
22+
[ "_$(nvm_rc_version | \grep -o -e 'with version .*$')" = "_with version <0.10.2>" ] || die "nvm_rc_version $(nvm_rc_version)"
23+
24+
[ "_$EXIT_CODE" = "_0" ] || die "sourcing returned nonzero exit code: $EXIT_CODE"
25+
26+
NVM_LS_CURRENT="$(nvm ls current | \grep -o v0.10.2)"
27+
[ "_$NVM_LS_CURRENT" = '_v0.10.2' ] || die "'nvm ls current' did not return '-> v0.10.2', got '$NVM_LS_CURRENT' `nvm ls`"
28+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/sh
2+
3+
die () { echo $@ ; exit 1; }
4+
supports_source_options () {
5+
[ "_$(echo 'echo $1' | . /dev/stdin yes)" = "_yes" ]
6+
}
7+
8+
if ! supports_source_options; then
9+
echo 'this shell does not support passing options on sourcing'
10+
exit 0;
11+
fi
12+
13+
echo '0.10.2' > ../../alias/default || die 'creation of default alias failed'
14+
15+
echo 'sourcing nvm with --install...'
16+
17+
. ../../nvm.sh --install
18+
EXIT_CODE="$(echo $?)"
19+
20+
echo 'sourcing complete.'
21+
22+
nvm_version 0.10.2 >/dev/null 2>&1 || die "v0.10.2 not installed: $(nvm ls)"
23+
24+
[ "_$EXIT_CODE" = "_0" ] || die "sourcing returned nonzero exit code: $EXIT_CODE"
25+
26+
NVM_LS_CURRENT="$(nvm ls current | \grep -o v0.10.2)"
27+
[ "_$NVM_LS_CURRENT" = '_v0.10.2' ] || die "'nvm ls current' did not return '-> v0.10.2', got '$NVM_LS_CURRENT'"
28+
29+
NVM_ALIAS_DEFAULT="$(nvm alias default)"
30+
[ "_$NVM_ALIAS_DEFAULT" = "_default -> 0.10.2 (-> v0.10.2)" ] \
31+
|| die "'nvm alias default did not return 'default -> 0.10.2 (-> v0.10.2)', got '$NVM_ALIAS_DEFAULT'"
32+

0 commit comments

Comments
 (0)