Consolidate "alias" directory to a method.

master
Jordan Harband 2015-01-12 01:27:53 -08:00
parent 835c006b5e
commit 7807a9f09e
2 changed files with 26 additions and 10 deletions

28
nvm.sh
View File

@ -152,6 +152,10 @@ nvm_version_dir() {
fi fi
} }
nvm_alias_path() {
echo "$(nvm_version_dir old)/alias"
}
nvm_version_path() { nvm_version_path() {
local VERSION local VERSION
VERSION="$1" VERSION="$1"
@ -261,7 +265,7 @@ nvm_alias() {
fi fi
local NVM_ALIAS_PATH local NVM_ALIAS_PATH
NVM_ALIAS_PATH="$NVM_DIR/alias/$ALIAS" NVM_ALIAS_PATH="$(nvm_alias_path)/$ALIAS"
if [ ! -f "$NVM_ALIAS_PATH" ]; then if [ ! -f "$NVM_ALIAS_PATH" ]; then
echo >&2 'Alias does not exist.' echo >&2 'Alias does not exist.'
return 2 return 2
@ -804,7 +808,7 @@ nvm() {
echo "Uninstalled node $VERSION" echo "Uninstalled node $VERSION"
# Rm any aliases that point to uninstalled version. # Rm any aliases that point to uninstalled version.
for ALIAS in `command grep -l $VERSION $NVM_DIR/alias/* 2>/dev/null` for ALIAS in `command grep -l $VERSION "$(nvm_alias_path)/*" 2>/dev/null`
do do
nvm unalias "$(command basename "$ALIAS")" nvm unalias "$(command basename "$ALIAS")"
done done
@ -1030,10 +1034,12 @@ nvm() {
echo "$NVM_VERSION_DIR/bin/node" echo "$NVM_VERSION_DIR/bin/node"
;; ;;
"alias" ) "alias" )
command mkdir -p "$NVM_DIR/alias" local NVM_ALIAS_DIR
NVM_ALIAS_DIR="$(nvm_alias_path)"
command mkdir -p "$NVM_ALIAS_DIR"
if [ $# -le 2 ]; then if [ $# -le 2 ]; then
local DEST local DEST
for ALIAS_PATH in "$NVM_DIR"/alias/"$2"*; do for ALIAS_PATH in "$NVM_ALIAS_DIR"/"$2"*; do
ALIAS="$(command basename "$ALIAS_PATH")" ALIAS="$(command basename "$ALIAS_PATH")"
DEST="$(nvm_alias "$ALIAS" 2> /dev/null)" DEST="$(nvm_alias "$ALIAS" 2> /dev/null)"
if [ -n "$DEST" ]; then if [ -n "$DEST" ]; then
@ -1047,7 +1053,7 @@ nvm() {
done done
for ALIAS in "stable" "unstable"; do for ALIAS in "stable" "unstable"; do
if [ ! -f "$NVM_DIR/alias/$ALIAS" ]; then if [ ! -f "$NVM_ALIAS_DIR/$ALIAS" ]; then
if [ $# -lt 2 ] || [ "~$ALIAS" = "~$2" ]; then if [ $# -lt 2 ] || [ "~$ALIAS" = "~$2" ]; then
DEST="$(nvm_print_implicit_alias local "$ALIAS")" DEST="$(nvm_print_implicit_alias local "$ALIAS")"
if [ "_$DEST" != "_" ]; then if [ "_$DEST" != "_" ]; then
@ -1060,7 +1066,7 @@ nvm() {
return return
fi fi
if [ -z "$3" ]; then if [ -z "$3" ]; then
command rm -f "$NVM_DIR/alias/$2" command rm -f "$NVM_ALIAS_DIR/$2"
echo "$2 -> *poof*" echo "$2 -> *poof*"
return return
fi fi
@ -1068,7 +1074,7 @@ nvm() {
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "! WARNING: Version '$3' does not exist." >&2 echo "! WARNING: Version '$3' does not exist." >&2
fi fi
echo "$3" > "$NVM_DIR/alias/$2" echo "$3" > "$NVM_ALIAS_DIR/$2"
if [ ! "_$3" = "_$VERSION" ]; then if [ ! "_$3" = "_$VERSION" ]; then
echo "$2 -> $3 (-> $VERSION)" echo "$2 -> $3 (-> $VERSION)"
else else
@ -1076,10 +1082,12 @@ nvm() {
fi fi
;; ;;
"unalias" ) "unalias" )
command mkdir -p "$NVM_DIR/alias" local NVM_ALIAS_DIR
NVM_ALIAS_DIR="$(nvm_alias_path)"
command mkdir -p "$NVM_ALIAS_DIR"
[ $# -ne 2 ] && nvm help && return 127 [ $# -ne 2 ] && nvm help && return 127
[ ! -f "$NVM_DIR/alias/$2" ] && echo "Alias $2 doesn't exist!" >&2 && return [ ! -f "$NVM_ALIAS_DIR/$2" ] && echo "Alias $2 doesn't exist!" >&2 && return
command rm -f $NVM_DIR/alias/$2 command rm -f "$NVM_ALIAS_DIR/$2"
echo "Deleted alias $2" echo "Deleted alias $2"
;; ;;
"reinstall-packages" | "copy-packages" ) "reinstall-packages" | "copy-packages" )

View File

@ -0,0 +1,8 @@
#!/bin/sh
die () { echo $@ ; exit 1; }
. ../../../nvm.sh
[ "_$(nvm_alias_path)" = "_$NVM_DIR/alias" ] || die "nvm_alias_path did not requrn correct location"