Skip to content

Instantly share code, notes, and snippets.

@calbrecht
Created August 10, 2013 09:50
Show Gist options
  • Save calbrecht/6199810 to your computer and use it in GitHub Desktop.
Save calbrecht/6199810 to your computer and use it in GitHub Desktop.
nodejs Version Manager utilizing update-alternatives
#!/bin/bash
if [ "$EUID" != "0" ]; then
sudo $0 $@;
exit 1;
fi
BIN_DIR=/usr/bin
NODE_DIR=/usr/local/node;
NODE_VERSION=""
NODE_VERSION_DIR=""
_check_version() {
local VERSION=$1
if [[ "" = "$VERSION" ]] ; then
select VERSION in $NODE_DIR/v[0-9]* ; do
if [[ ! "" = "$VERSION" ]] ; then
VERSION=$(basename $VERSION)
break
fi
done
fi
if [[ ! -d "$NODE_DIR/$VERSION" ]] ; then
echo "E: $NODE_DIR/$VERSION is not a directory"
exit 1
fi
NODE_VERSION=$VERSION
NODE_VERSION_DIR=$NODE_DIR/$NODE_VERSION;
}
_install(){
local FILE
local NAME
local COMMANDS
for FILE in $NODE_VERSION_DIR/bin/* ; do
NAME=$(basename $FILE)
if [[ "node" = "$NAME" ]] ; then
COMMANDS="--install $BIN_DIR/node node $FILE 100 $COMMANDS"
else
COMMANDS="$COMMANDS --slave $BIN_DIR/$NAME $NAME $FILE"
fi
done
update-alternatives $COMMANDS
$NODE_VERSION_DIR/lib/node_modules/npm/configure --prefix=$NODE_VERSION_DIR
}
_switch(){
update-alternatives --set node $NODE_VERSION_DIR/bin/node
}
_remove(){
update-alternatives --remove node $NODE_VERSION_DIR/bin/node
}
case "$1" in
install|switch|remove)
_check_version $2 && _$1
;;
*)
echo "Usage: $0 (install|switch|remove)";
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment