From 50bdb173a0f887c580fdc58817f7eb977abb8754 Mon Sep 17 00:00:00 2001 From: "Dr. Kibitz" Date: Sun, 16 Feb 2014 00:01:41 -0800 Subject: [PATCH] Use symlinks for aliases --- nvm.sh | 25 +++++++++++++------ ...uld create a file in the alias directory." | 2 +- ...m unalias\" should remove the alias file." | 4 +-- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/nvm.sh b/nvm.sh index 0016ad5..507baac 100755 --- a/nvm.sh +++ b/nvm.sh @@ -78,8 +78,8 @@ nvm_ls() { return fi - if [ -f "$NVM_DIR/alias/$PATTERN" ]; then - nvm_version `cat $NVM_DIR/alias/$PATTERN` + if [ -h "$NVM_DIR/alias/$PATTERN" ]; then + nvm_version $(basename $(readlink $NVM_DIR/alias/$PATTERN)) return fi # If it looks like an explicit version, don't do anything funny @@ -373,9 +373,11 @@ nvm() { echo "Uninstalled node $VERSION" # Rm any aliases that point to uninstalled version. - for ALIAS in `\grep -l $VERSION $NVM_DIR/alias/* 2>/dev/null` + for ALIAS in $NVM_DIR/alias/* do - nvm unalias `basename $ALIAS` + if [ $(expr $(readlink $ALIAS) : ".*${VERSION}.*") -ne 0 ]; then + nvm unalias $(basename $ALIAS) + fi done ;; @@ -491,7 +493,7 @@ nvm() { local DEST for ALIAS in $NVM_DIR/alias/$2*; do if [ -e "$ALIAS" ]; then - DEST=`cat $ALIAS` + DEST=$(basename $(readlink $ALIAS)) VERSION=`nvm_version $DEST` if [ "$DEST" = "$VERSION" ]; then echo "$(basename $ALIAS) -> $DEST" @@ -512,17 +514,24 @@ nvm() { if [ $? -ne 0 ]; then echo "! WARNING: Version '$3' does not exist." >&2 fi - echo $3 > "$NVM_DIR/alias/$2" + # We are linking directories, so explicitly remove it rather than forcing with ln -f + if [ -h "$NVM_DIR/alias/$2" ]; then + rm -f "$NVM_DIR/alias/$2" + fi if [ ! "$3" = "$VERSION" ]; then - echo "$2 -> $3 (-> $VERSION)" + # link to other alias + ln -s "./$3" "$NVM_DIR/alias/$2" + echo "$2 -> $3 (-> $VERSION)" else + # link to node installation + ln -s "../$3" "$NVM_DIR/alias/$2" echo "$2 -> $3" fi ;; "unalias" ) mkdir -p $NVM_DIR/alias [ $# -ne 2 ] && nvm help && return - [ ! -f $NVM_DIR/alias/$2 ] && echo "Alias $2 doesn't exist!" && return + [ ! -h $NVM_DIR/alias/$2 ] && echo "Alias $2 doesn't exist!" && return rm -f $NVM_DIR/alias/$2 echo "Deleted alias $2" ;; diff --git "a/test/fast/Running \"nvm alias\" should create a file in the alias directory." "b/test/fast/Running \"nvm alias\" should create a file in the alias directory." index 0ac4eb2..74de58a 100755 --- "a/test/fast/Running \"nvm alias\" should create a file in the alias directory." +++ "b/test/fast/Running \"nvm alias\" should create a file in the alias directory." @@ -2,4 +2,4 @@ . ../../nvm.sh nvm alias test v0.1.2 -[ $(cat ../../alias/test) = 'v0.1.2' ] +[ -h ../../alias/test ] && [ $(readlink ../../alias/test) = '../v0.1.2' ] diff --git "a/test/fast/Running \"nvm unalias\" should remove the alias file." "b/test/fast/Running \"nvm unalias\" should remove the alias file." index f8ed49d..52e8f1d 100755 --- "a/test/fast/Running \"nvm unalias\" should remove the alias file." +++ "b/test/fast/Running \"nvm unalias\" should remove the alias file." @@ -1,6 +1,6 @@ #!/bin/sh -echo v0.1.2 > ../../alias/test +ln -s ../v0.1.2 ../../alias/test . ../../nvm.sh nvm unalias test -! [ -e ../../alias/test ] +! [ -h ../../alias/test ]