Dr. Kibitz 2014-02-18 21:51:39 -08:00
parent 12d7b6fa0c
commit 9a0c269713
1 changed files with 20 additions and 18 deletions

38
nvm.sh
View File

@ -1,5 +1,3 @@
#!/bin/sh
# Node Version Manager # Node Version Manager
# Implemented as a bash function # Implemented as a bash function
# To use source this file from your bash profile # To use source this file from your bash profile
@ -21,7 +19,11 @@ fi
# Auto detect the NVM_DIR # Auto detect the NVM_DIR
if [ ! -d "$NVM_DIR" ]; then if [ ! -d "$NVM_DIR" ]; then
export NVM_DIR=$(cd $NVM_CD_FLAGS $(dirname ${BASH_SOURCE[0]:-$0}) > /dev/null && pwd) if [ -n "$BASH_SOURCE" ]; then
export NVM_DIR=$(cd $NVM_CD_FLAGS $(dirname ${BASH_SOURCE[0]:-$0}) > /dev/null && pwd)
else
export NVM_DIR=$HOME/.nvm
fi
fi fi
# Setup mirror location if not already set # Setup mirror location if not already set
@ -33,7 +35,7 @@ nvm_set_nullglob() {
if has "setopt"; then if has "setopt"; then
# Zsh # Zsh
setopt NULL_GLOB setopt NULL_GLOB
else elif has "shopt"; then
# Bash # Bash
shopt -s nullglob shopt -s nullglob
fi fi
@ -88,7 +90,7 @@ nvm_ls() {
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 [ `expr "$PATTERN" : "v.*.\?.*.\?.*$"` != 0 ]; then
VERSIONS="$PATTERN" VERSIONS="$PATTERN"
else else
VERSIONS=`find "$NVM_DIR/" -maxdepth 1 -type d -name "v$PATTERN*" -exec basename '{}' ';' \ VERSIONS=`find "$NVM_DIR/" -maxdepth 1 -type d -name "v$PATTERN*" -exec basename '{}' ';' \
@ -98,7 +100,7 @@ nvm_ls() {
echo "N/A" echo "N/A"
return return
fi fi
echo "$VERSIONS" printf "$VERSIONS\n"
return return
} }
@ -146,7 +148,7 @@ print_versions() {
local PADDED_VERSION local PADDED_VERSION
for VERSION in $1; do for VERSION in $1; do
PADDED_VERSION=`printf '%10s' $VERSION` PADDED_VERSION=`printf '%10s' $VERSION`
if [[ -d "$NVM_DIR/$VERSION" ]]; then if [ -d "$NVM_DIR/$VERSION" ]; then
colorize_version "$PADDED_VERSION" colorize_version "$PADDED_VERSION"
else else
echo "$PADDED_VERSION" echo "$PADDED_VERSION"
@ -331,10 +333,10 @@ nvm() {
nvm use $VERSION nvm use $VERSION
if ! has "npm" ; then if ! has "npm" ; then
echo "Installing npm..." echo "Installing npm..."
if [[ "`expr match $VERSION '\(^v0\.1\.\)'`" != '' ]]; then if [ "`expr match $VERSION '\(^v0\.1\.\)'`" != '' ]; then
echo "npm requires node v0.2.3 or higher" echo "npm requires node v0.2.3 or higher"
elif [[ "`expr match $VERSION '\(^v0\.2\.\)'`" != '' ]]; then elif [ "`expr match $VERSION '\(^v0\.2\.\)'`" != '' ]; then
if [[ "`expr match $VERSION '\(^v0\.2\.[0-2]$\)'`" != '' ]]; then if [ "`expr match $VERSION '\(^v0\.2\.[0-2]$\)'`" != '' ]; then
echo "npm requires node v0.2.3 or higher" echo "npm requires node v0.2.3 or higher"
else else
curl https://npmjs.org/install.sh | clean=yes npm_install=0.2.19 sh curl https://npmjs.org/install.sh | clean=yes npm_install=0.2.19 sh
@ -350,7 +352,7 @@ nvm() {
;; ;;
"uninstall" ) "uninstall" )
[ $# -ne 2 ] && nvm help && return [ $# -ne 2 ] && nvm help && return
if [[ $2 == `nvm_version` ]]; then if [ $2 == `nvm_version` ]; then
echo "nvm: Cannot uninstall currently-active node version, $2." echo "nvm: Cannot uninstall currently-active node version, $2."
return 1 return 1
fi fi
@ -378,14 +380,14 @@ nvm() {
;; ;;
"deactivate" ) "deactivate" )
if [[ $PATH == *$NVM_DIR/*/bin* ]]; then if [ `expr $PATH : ".*$NVM_DIR/.*/bin.*"` != 0 ] ; then
export PATH=${PATH%$NVM_DIR/*/bin*}${PATH#*$NVM_DIR/*/bin:} export PATH=${PATH%$NVM_DIR/*/bin*}${PATH#*$NVM_DIR/*/bin:}
hash -r hash -r
echo "$NVM_DIR/*/bin removed from \$PATH" echo "$NVM_DIR/*/bin removed from \$PATH"
else else
echo "Could not find $NVM_DIR/*/bin in \$PATH" echo "Could not find $NVM_DIR/*/bin in \$PATH"
fi fi
if [[ $MANPATH == *$NVM_DIR/*/share/man* ]]; then if [ `expr $MANPATH : ".*$NVM_DIR/.*/share/man.*"` != 0 ] ; then
export MANPATH=${MANPATH%$NVM_DIR/*/share/man*}${MANPATH#*$NVM_DIR/*/share/man:} export MANPATH=${MANPATH%$NVM_DIR/*/share/man*}${MANPATH#*$NVM_DIR/*/share/man:}
echo "$NVM_DIR/*/share/man removed from \$MANPATH" echo "$NVM_DIR/*/share/man removed from \$MANPATH"
else else
@ -422,7 +424,7 @@ nvm() {
echo "$VERSION version is not installed yet" echo "$VERSION version is not installed yet"
return 1 return 1
fi fi
if [[ $PATH == *$NVM_DIR/*/bin* ]]; then if [ `expr $PATH : ".*$NVM_DIR/.*/bin"` != 0 ]; then
PATH=${PATH%$NVM_DIR/*/bin*}$NVM_DIR/$VERSION/bin${PATH#*$NVM_DIR/*/bin} PATH=${PATH%$NVM_DIR/*/bin*}$NVM_DIR/$VERSION/bin${PATH#*$NVM_DIR/*/bin}
else else
PATH="$NVM_DIR/$VERSION/bin:$PATH" PATH="$NVM_DIR/$VERSION/bin:$PATH"
@ -431,7 +433,7 @@ nvm() {
MANPATH=$(manpath) MANPATH=$(manpath)
fi fi
MANPATH=${MANPATH#*$NVM_DIR/*/man:} MANPATH=${MANPATH#*$NVM_DIR/*/man:}
if [[ $MANPATH == *$NVM_DIR/*/share/man* ]]; then if [ `expr $MANPATH : ".*$NVM_DIR/.*/share/man"` != 0 ]; then
MANPATH=${MANPATH%$NVM_DIR/*/share/man*}$NVM_DIR/$VERSION/share/man${MANPATH#*$NVM_DIR/*/share/man} MANPATH=${MANPATH%$NVM_DIR/*/share/man*}$NVM_DIR/$VERSION/share/man${MANPATH#*$NVM_DIR/*/share/man}
else else
MANPATH="$NVM_DIR/$VERSION/share/man:$MANPATH" MANPATH="$NVM_DIR/$VERSION/share/man:$MANPATH"
@ -471,7 +473,7 @@ nvm() {
"ls" | "list" ) "ls" | "list" )
print_versions "`nvm_ls $2`" print_versions "`nvm_ls $2`"
if [ $# -eq 1 ]; then if [ $# -eq 1 ]; then
echo -ne "current: \t"; nvm_version current printf "current: \t"; nvm_version current
nvm alias nvm alias
fi fi
return return
@ -533,7 +535,7 @@ nvm() {
# declare local INSTALLS first, otherwise it doesn't work in zsh # declare local INSTALLS first, otherwise it doesn't work in zsh
local INSTALLS local INSTALLS
INSTALLS=( `nvm use $VERSION > /dev/null && npm -g -p ll | \grep "$ROOT\/[^/]\+$" | cut -d '/' -f $(($ROOTDEPTH + 2)) | cut -d ":" -f 2 | \grep -v npm | tr "\n" " "` ) INSTALLS=`nvm use $VERSION > /dev/null && npm -g -p ll | \grep "$ROOT\/[^/]\+$" | cut -d '/' -f $(($ROOTDEPTH + 2)) | cut -d ":" -f 2 | \grep -v npm | tr "\n" " "`
npm install -g ${INSTALLS[@]} npm install -g ${INSTALLS[@]}
;; ;;
@ -550,5 +552,5 @@ nvm() {
esac esac
} }
nvm ls default &>/dev/null && nvm use default >/dev/null || true nvm ls default >/dev/null && nvm use default >/dev/null || true