Rewrote version listing system

Now sorts correctly in all instances and prints highlights
even in sorted views. Also will output a list of matching versions
for commands of the form nvm ls 0.4* rather than just printing one
version. nvm version 0.4 still prints the single latest matching
version, however.
master
Lucian Holland 2011-11-04 20:36:04 +00:00
parent 6f994715d6
commit fda6e7f8de
1 changed files with 47 additions and 26 deletions

69
nvm.sh
View File

@ -20,34 +20,56 @@ fi
nvm_version() nvm_version()
{ {
PATTERN=$1 PATTERN=$1
VERSION='' # The default version is the current one
if [ ! "$PATTERN" ]; then
PATTERN='current'
fi
VERSION=`nvm_ls $PATTERN | tail -n1`
echo "$VERSION"
if [ "$VERSION" = 'N/A' ]; then
return 13
fi
}
nvm_ls()
{
PATTERN=$1
VERSIONS=''
if [ "$PATTERN" = 'current' ]; then
VERSION=`node -v 2>/dev/null`
fi
if [ -f "$NVM_DIR/alias/$PATTERN" ]; then if [ -f "$NVM_DIR/alias/$PATTERN" ]; then
nvm_version `cat $NVM_DIR/alias/$PATTERN` nvm_version `cat $NVM_DIR/alias/$PATTERN`
return return
fi fi
# If it looks like an explicit version, don't do anything funny # If it looks like an explicit version, don't do anything funny
if [[ "$PATTERN" == v?*.?*.?* ]]; then if [[ "$PATTERN" == v?*.?*.?* ]]; then
VERSION="$PATTERN" VERSIONS="$PATTERN"
else
VERSIONS=`(cd $NVM_DIR; \ls -d v${PATTERN}* 2>/dev/null) | sort -t. -k 1.2,1n -k 2,2n -k 3,3n`
fi fi
# The default version is the current one if [ ! "$VERSIONS" ]; then
if [ ! "$PATTERN" -o "$PATTERN" = 'current' ]; then echo "N/A"
VERSION=`node -v 2>/dev/null`
fi
if [ "$PATTERN" = 'all' ]; then
(cd $NVM_DIR; \ls -dG v* 2>/dev/null || echo "N/A")
return return
fi fi
if [ ! "$VERSION" ]; then echo "$VERSIONS"
VERSION=`(cd $NVM_DIR; \ls -d v${PATTERN}* 2>/dev/null) | sort -t. -k 1.2,1n -k 2,2n -k 3,3n | tail -n1` return
fi }
if [ ! "$VERSION" ]; then
echo "N/A" print_versions()
return 13 {
elif [ -e "$NVM_DIR/$VERSION" ]; then OUTPUT=''
(cd $NVM_DIR; \ls -dG "$VERSION") for VERSION in $1; do
else PADDED_VERSION=`printf '%10s' $VERSION`
echo "$VERSION" if [[ -d "$NVM_DIR/$VERSION" ]]; then
PADDED_VERSION="\033[0;34m$PADDED_VERSION\033[0m"
fi fi
OUTPUT="$OUTPUT\n$PADDED_VERSION"
done
echo -e "$OUTPUT" | column
} }
nvm() nvm()
@ -189,13 +211,12 @@ nvm()
echo "Now using node $VERSION" echo "Now using node $VERSION"
;; ;;
"ls" | "list" ) "ls" | "list" )
if [ $# -ne 1 ]; then print_versions "`nvm_ls $2`"
nvm_version $2 if [ $# -eq 1 ]; then
return
fi
nvm_version all
echo -ne "current: \t"; nvm_version current echo -ne "current: \t"; nvm_version current
nvm alias nvm alias
fi
return
;; ;;
"alias" ) "alias" )
mkdir -p $NVM_DIR/alias mkdir -p $NVM_DIR/alias
@ -250,7 +271,7 @@ nvm()
echo "Cache cleared." echo "Cache cleared."
;; ;;
"version" ) "version" )
nvm_version $2 print_versions "`nvm_version $2`"
;; ;;
* ) * )
nvm help nvm help