Merge pull request #537 from ljharb/add_stable_unstable_aliases

Add stable & unstable aliases.

Fixes #336.

With these changes, the following `nvm` commands will now support "stable" and "unstable" default/implicit aliases:
 - `ls`/`list`
 - `use`
 - `alias`
 - `run`
 - `exec`
 - `uninstall`
 - `copy-packages`

For these, "stable" will be the latest even minor version that's locally installed; "unstable" will be the latest odd minor version that's locally installed.

In addition, `nvm ls-remote` and `nvm install` will accept the same implicit aliases, however, these will actually call nodejs.org and output the latest even and odd minor versions available for installation.

`nvm alias stable foo` and `nvm alias unstable foo` will *override* the default/implicit aliases. `nvm alias` output will reflect this by showing "(default)" when the alias is not user-defined.
master
Jordan Harband 2014-09-29 21:35:44 -07:00
commit fc16d20766
10 changed files with 248 additions and 2 deletions

63
nvm.sh
View File

@ -274,6 +274,11 @@ nvm_ls() {
fi
else
if [ "_$PATTERN" != "_system" ]; then
if nvm_validate_implicit_alias "$PATTERN" 2> /dev/null ; then
nvm_ls "$(nvm_print_implicit_alias local "$PATTERN" 2> /dev/null)"
return $?
fi
local NUM_VERSION_GROUPS
NUM_VERSION_GROUPS="$(nvm_num_version_groups "$PATTERN")"
if [ "_$NUM_VERSION_GROUPS" = "_2" ] || [ "_$NUM_VERSION_GROUPS" = "_1" ]; then
@ -312,7 +317,9 @@ nvm_ls_remote() {
local VERSIONS
local GREP_OPTIONS
GREP_OPTIONS=''
if [ -n "$PATTERN" ]; then
if nvm_validate_implicit_alias "$PATTERN" 2> /dev/null ; then
PATTERN="$(nvm_remote_version "$(nvm_print_implicit_alias remote "$PATTERN")")"
elif [ -n "$PATTERN" ]; then
PATTERN="$(nvm_ensure_version_prefix "$PATTERN")"
else
PATTERN=".*"
@ -368,6 +375,48 @@ nvm_print_versions() {
done
}
nvm_validate_implicit_alias() {
if [ "_$1" != "_stable" ] && [ "_$1" != "_unstable" ]; then
echo "Only implicit aliases 'stable' and 'unstable' are supported." >&2
return 1
fi
}
nvm_print_implicit_alias() {
if [ "_$1" != "_local" ] && [ "_$1" != "_remote" ]; then
echo "nvm_print_implicit_alias must be specified with local or remote as the first argument." >&2
return 1
fi
if ! nvm_validate_implicit_alias "$2"; then
return 2
fi
local LAST_TWO
if [ "_$1" = "_local" ]; then
LAST_TWO="$(nvm_ls | \grep -e '^v' | cut -c2- | cut -d . -f 1,2 | uniq)"
else
LAST_TWO="$(nvm_ls_remote | \grep -e '^v' | cut -c2- | cut -d . -f 1,2 | uniq)"
fi
local MINOR
local STABLE
local UNSTABLE
local MOD
for MINOR in $LAST_TWO; do
MOD=$(expr "$(nvm_normalize_version "$MINOR")" \/ 1000000 \% 2)
if [ $MOD -eq 0 ]; then
STABLE="$MINOR"
elif [ $MOD -eq 1 ]; then
UNSTABLE="$MINOR"
fi
done
if [ "_$2" = "_stable" ]; then
echo $STABLE
elif [ "_$2" = "_unstable" ]; then
echo $UNSTABLE
fi
}
nvm() {
if [ $# -lt 1 ]; then
nvm help
@ -471,7 +520,7 @@ nvm() {
nobinary=1
fi
provided_version=$1
provided_version="$1"
if [ -z "$provided_version" ]; then
if [ $version_not_provided -ne 1 ]; then
@ -834,6 +883,16 @@ nvm() {
fi
fi
done
for ALIAS in "stable" "unstable"; do
if [ ! -f "$NVM_DIR/alias/$ALIAS" ]; then
if [ $# -lt 2 ] || [ "~$ALIAS" = "~$2" ]; then
DEST="$(nvm_print_implicit_alias local "$ALIAS")"
VERSION="$(nvm_version "$DEST")"
echo "$ALIAS -> $DEST (-> $VERSION) (default)"
fi
fi
done
return
fi
if [ -z "$3" ]; then

View File

@ -0,0 +1,18 @@
#!/bin/sh
. ../../../nvm.sh
die () { echo $@ ; exit 1; }
NVM_ALIAS_OUTPUT=$(nvm alias)
EXPECTED_STABLE="$(nvm_print_implicit_alias local stable)"
STABLE_VERSION="$(nvm_version "$EXPECTED_STABLE")"
echo "$NVM_ALIAS_OUTPUT" | \grep -e "^stable -> $EXPECTED_STABLE (-> $STABLE_VERSION) (default)$" \
|| die "nvm alias did not contain the default local stable node version"
EXPECTED_UNSTABLE="$(nvm_print_implicit_alias local unstable)"
UNSTABLE_VERSION="$(nvm_version "$EXPECTED_UNSTABLE")"
echo "$NVM_ALIAS_OUTPUT" | \grep -e "^unstable -> $EXPECTED_UNSTABLE (-> $UNSTABLE_VERSION) (default)$" \
|| die "nvm alias did not contain the default local unstable node version"

View File

@ -0,0 +1,35 @@
#!/bin/sh
. ../../../nvm.sh
die () { echo $@ ; cleanup ; exit 1; }
cleanup () {
rm -rf ../../../v0.8.1
rm -rf ../../../v0.9.1
}
mkdir ../../../v0.8.1
mkdir ../../../v0.9.1
EXPECTED_STABLE="$(nvm_print_implicit_alias local stable)"
STABLE_VERSION="$(nvm_version "$EXPECTED_STABLE")"
EXPECTED_UNSTABLE="$(nvm_print_implicit_alias local unstable)"
UNSTABLE_VERSION="$(nvm_version "$EXPECTED_UNSTABLE")"
[ "_$STABLE_VERSION" != "_$UNSTABLE_VERSION" ] \
|| die "stable and unstable versions are the same!"
nvm alias stable "$EXPECTED_UNSTABLE"
nvm alias unstable "$EXPECTED_STABLE"
NVM_ALIAS_OUTPUT=$(nvm alias)
echo "$NVM_ALIAS_OUTPUT" | \grep -e "^stable -> $EXPECTED_UNSTABLE (-> $UNSTABLE_VERSION)$" \
|| die "nvm alias did not contain the overridden 'stable' alias"
echo "$NVM_ALIAS_OUTPUT" | \grep -e "^unstable -> $EXPECTED_STABLE (-> $STABLE_VERSION)$" \
|| die "nvm alias did not contain the overridden 'unstable' alias"
cleanup

View File

@ -6,3 +6,6 @@ for i in $(seq 1 10)
rm -rf "../../../v0.0.$i"
done
rm -f "../../../alias/stable"
rm -f "../../../alias/unstable"

View File

@ -0,0 +1,25 @@
#!/bin/sh
. ../../../nvm.sh
die () { echo $@ ; exit 1; }
EXPECTED_STABLE="$(nvm_print_implicit_alias local stable)"
STABLE_VERSION="$(nvm_version "$EXPECTED_STABLE")"
EXPECTED_UNSTABLE="$(nvm_print_implicit_alias local unstable)"
UNSTABLE_VERSION="$(nvm_version "$EXPECTED_UNSTABLE")"
nvm ls stable | \grep "$STABLE_VERSION" >/dev/null \
|| die "expected 'nvm ls stable' to give $STABLE_VERSION, got $(nvm ls stable)"
nvm ls unstable | \grep "$UNSTABLE_VERSION" >/dev/null \
|| die "expected 'nvm ls unstable' to give $UNSTABLE_VERSION, got $(nvm ls unstable)"
mkdir ../../../v0.1.2
nvm alias stable 0.1
nvm ls stable | \grep v0.1.2 >/dev/null \
&& nvm ls stable | \grep -v "$STABLE_VERSION" >/dev/null \
|| die "'nvm ls stable' did not contain an explicit 'stable' alias"

View File

@ -11,4 +11,6 @@ rmdir ../../../v0.3.3 >/dev/null 2>&1
rmdir ../../../v0.3.9 >/dev/null 2>&1
rmdir ../../../versions >/dev/null 2>&1
unalias nvm_has_system_node >/dev/null 2>&1
rm -f ../../../alias/stable >/dev/null 2>&1
rm -f ../../../alias/unstable >/dev/null 2>&1

View File

@ -464,5 +464,22 @@ v0.3.8"
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm_ls_remote 0.3 did not output 0.3.x versions; got $OUTPUT"
# Sanity checks
OUTPUT="$(nvm_print_implicit_alias remote stable)"
EXPECTED_OUTPUT="0.10"
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm_print_implicit_alias remote stable did not output $EXPECTED_OUTPUT; got $OUTPUT"
OUTPUT="$(nvm_print_implicit_alias remote unstable)"
EXPECTED_OUTPUT="0.11"
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm_print_implicit_alias remote unstable did not output $EXPECTED_OUTPUT; got $OUTPUT"
OUTPUT="$(nvm_ls_remote stable)"
EXPECTED_OUTPUT="v0.10.32"
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm_ls_remote stable did not output $EXPECTED_OUTPUT; got $OUTPUT"
OUTPUT="$(nvm_ls_remote unstable)"
EXPECTED_OUTPUT="v0.11.14"
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm_ls_remote unstable did not output $EXPECTED_OUTPUT; got $OUTPUT"
cleanup

View File

@ -0,0 +1,26 @@
#!/bin/sh
die () { echo $@ ; exit 1; }
. ../../../nvm.sh
EXPECTED_FIRST_MSG="nvm_print_implicit_alias must be specified with local or remote as the first argument."
[ "_$(nvm_print_implicit_alias 2>&1)" = "_$EXPECTED_FIRST_MSG" ] \
|| die "nvm_print_implicit_alias did not require local|remote as first argument"
[ "_$(nvm_print_implicit_alias foo 2>&1)" = "_$EXPECTED_FIRST_MSG" ] \
|| die "nvm_print_implicit_alias did not require local|remote as first argument"
FIRST_EXIT_CODE="$(nvm_print_implicit_alias > /dev/null 2>&1 ; echo $?)"
[ "_$FIRST_EXIT_CODE" = "_1" ] \
|| die "nvm_print_implicit_alias without local|remote had wrong exit code: expected 1, got $FIRST_EXIT_CODE"
EXPECTED_SECOND_MSG="Only implicit aliases 'stable' and 'unstable' are supported."
[ "_$(nvm_print_implicit_alias local 2>&1)" = "_$EXPECTED_SECOND_MSG" ] \
|| die "nvm_print_implicit_alias did not require stable|unstable as second argument"
[ "_$(nvm_print_implicit_alias local foo 2>&1)" = "_$EXPECTED_SECOND_MSG" ] \
|| die "nvm_print_implicit_alias did not require stable|unstable as second argument"
SECOND_EXIT_CODE="$(nvm_print_implicit_alias local > /dev/null 2>&1 ; echo $?)"
[ "_$SECOND_EXIT_CODE" = "_2" ] \
|| die "nvm_print_implicit_alias without stable|unstable had wrong exit code: expected 2, got $SECOND_EXIT_CODE"

View File

@ -0,0 +1,42 @@
#!/bin/sh
die () { echo $@ ; cleanup ; exit 1; }
cleanup() {
rm -rf ../../../v0.2.3
rm -rf ../../../v0.3.4
rm -rf ../../../v0.4.6
rm -rf ../../../v0.5.7
rm -rf ../../../v0.7.7
unset -f nvm_ls_remote
}
. ../../../nvm.sh
mkdir ../../../v0.2.3
mkdir ../../../v0.3.4
mkdir ../../../v0.4.6
mkdir ../../../v0.5.7
mkdir ../../../v0.7.7
LATEST_STABLE="$(nvm_print_implicit_alias local stable)"
[ "_$LATEST_STABLE" = "_0.4" ] || die "local stable is not latest even minor: expected 0.4, got $LATEST_STABLE"
LATEST_UNSTABLE="$(nvm_print_implicit_alias local unstable)"
[ "_$LATEST_UNSTABLE" = "_0.7" ] || die "local unstable is not latest odd minor: expected 0.7, got $LATEST_UNSTABLE"
nvm_ls_remote() {
echo "v0.4.3"
echo "v0.5.4"
echo "v0.6.6"
echo "v0.7.7"
echo "v0.9.7"
}
LATEST_STABLE="$(nvm_print_implicit_alias remote stable)"
[ "_$LATEST_STABLE" = "_0.6" ] || die "remote stable is not latest even minor: expected 0.6, got $LATEST_STABLE"
LATEST_UNSTABLE="$(nvm_print_implicit_alias remote unstable)"
[ "_$LATEST_UNSTABLE" = "_0.9" ] || die "remote unstable is not latest odd minor: expected 0.9, got $LATEST_UNSTABLE"
cleanup

View File

@ -0,0 +1,19 @@
#!/bin/sh
die () { echo $@ ; exit 1; }
. ../../../nvm.sh
EXPECTED_MSG="Only implicit aliases 'stable' and 'unstable' are supported."
[ "_$(nvm_validate_implicit_alias 2>&1)" = "_$EXPECTED_MSG" ] \
|| die "nvm_validate_implicit_alias did not require stable|unstable"
[ "_$(nvm_validate_implicit_alias foo 2>&1)" = "_$EXPECTED_MSG" ] \
|| die "nvm_validate_implicit_alias did not require stable|unstable"
EXIT_CODE="$(nvm_validate_implicit_alias >/dev/null 2>&1 ; echo $?)"
[ "_$EXIT_CODE" = "_1" ] \
|| die "nvm_validate_implicit_alias without stable|unstable had wrong exit code: expected 1, got $EXIT_CODE"
nvm_validate_implicit_alias stable || die "nvm_validate_implicit_alias stable did not exit 0"
nvm_validate_implicit_alias unstable || die "nvm_validate_implicit_alias unstable did not exit 0"