Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

executable file 223 lines (176 sloc) 3.863 kb
#!/usr/bin/env sh
# Library version
VERSION="0.1.0"
PREFIX=${PREFIX-/usr/local}
N_PREFIX=${N_PREFIX-/usr/local}
VERSIONS_DIR=$N_PREFIX/n/versions
# setup
test -d $VERSIONS_DIR || mkdir -p $VERSIONS_DIR
# curl / wget support
GET=
# wget support
which wget > /dev/null && GET="wget -q -O-"
# curl support
which curl > /dev/null && GET="curl -# -L"
# Ensure we have curl or wget
test -z "$GET" && abort "curl or wget required"
#
# Log the given <msg ...>
#
log() {
echo "\033[90m...\033[0m $@"
}
#
# Exit with the given <msg ...>
#
abort() {
echo "\033[31mError: $@\033[0m" && exit 1
}
#
# Output usage information.
#
display_help() {
cat <<-help
Usage: n [options] [COMMAND] [config]
Commands:
n Output versions installed
n use <version> [args ...] Execute node <version> with [args ...]
n bin <version> Output bin path for <version>
n rm <version ...> Remove the given version(s)
n <version> Install and/or use node <version>
Options:
-V, --version Output current version of n
-h, --help Display help information
Aliases:
- rm
which bin
use as
help
exit 0
}
#
# Output n version.
#
display_n_version() {
echo $VERSION && exit 0
}
#
# Check for installed version, and populate $active
#
check_current_version() {
if test `which node`; then
active=`node --version`
active=${active##*v}
fi
}
#
# Display current node --version
# and others installed.
#
display_versions() {
check_current_version
for file in $VERSIONS_DIR/*; do
local version=${file##*/}
if test "$version" = "$active"; then
echo " \033[32m*\033[0m $version"
else
echo " $version"
fi
done
}
#
# Install node <version> [config ...]
#
install_node() {
local version=$1; shift
local config=$@
check_current_version
# remove "v"
if test "${version:0:1}" = "v"; then
version=${active##*v}
fi
# already active
test "$version" = "$active" && return
# installed
local dir=$VERSIONS_DIR/$version
if test -d $dir; then
cd $dir \
&& cp -fr $dir/include/node $PREFIX/include \
&& cp -f $dir/bin/node $PREFIX/bin/node
# install
else
local dir="node-v$version"
cd $N_PREFIX/n \
&& $GET "http://nodejs.org/dist/node-v$version.tar.gz" \
> "$dir.tar.gz" \
&& tar -zxf "$dir.tar.gz" \
&& cd $dir \
&& ./configure --prefix $VERSIONS_DIR/$version $config\
&& make install \
&& cd .. \
&& cleanup $version \
&& n $version
fi
}
#
# Cleanup after the given <version>
#
cleanup() {
local version=$1
local dir="node-v$version"
if test -d $dir; then
log "removing source"
rm -fr $dir
fi
if test -f "$dir.tar.gz"; then
log "removing tarball"
rm -fr "$dir.tar.gz"
fi
}
#
# Remove <version ...>
#
remove_version() {
test $# -eq 0 && abort "version(s) required"
while test $# -ne 0; do
rm -rf $VERSIONS_DIR/${1##*v}
shift
done
}
#
# Output bin path for <version>
#
display_bin_path_for_version() {
test -z $1 && abort "version required"
local file=$VERSIONS_DIR/${1##*v}/bin/node
if test -f $file; then
printf $file
else
abort "$1 is not installed"
fi
}
#
# Execute the given <version> of node
# with [args ...]
#
execute_with_version() {
test -z $1 && abort "version required"
local bin=`n which $1`
$bin ${@:2}
}
# Handle arguments
if test $# -eq 0; then
display_versions
else
while test $# -ne 0; do
case $1 in
-V|--version) display_n_version ;;
-h|--help) display_help ;;
bin|which) display_bin_path_for_version $2; exit ;;
as|use) execute_with_version ${@:2}; exit ;;
rm|-) remove_version ${@:2}; exit ;;
*) install_node $@; exit ;;
esac
shift
done
fi
Jump to Line
Something went wrong with that request. Please try again.