mirror of
https://github.com/tj/n.git
synced 2024-11-22 11:37:26 +01:00
153 lines
2.2 KiB
Bash
Executable File
153 lines
2.2 KiB
Bash
Executable File
#!/usr/bin/env sh
|
|
|
|
# Library version
|
|
|
|
VERSION="0.0.1"
|
|
PREFIX=${N_PREFIX-/usr/local}
|
|
VERSIONS_DIR=$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 "... $@"
|
|
}
|
|
|
|
#
|
|
# Exit with the given <msg ...>
|
|
#
|
|
|
|
abort() {
|
|
echo "Error: $@" && exit 1
|
|
}
|
|
|
|
#
|
|
# Output usage information.
|
|
#
|
|
|
|
display_help() {
|
|
cat <<-help
|
|
|
|
Usage: n [options] <version>
|
|
|
|
Commands:
|
|
|
|
n Output versions installed
|
|
n <version> Install and/or use node <version>
|
|
|
|
Options:
|
|
|
|
-V, --version Output current version of n
|
|
-h, --help Display help information
|
|
|
|
help
|
|
exit 0
|
|
}
|
|
|
|
#
|
|
# Output n version.
|
|
#
|
|
|
|
display_n_version() {
|
|
echo $VERSION && exit 0
|
|
}
|
|
|
|
#
|
|
# Display current node --version
|
|
#
|
|
|
|
display_current_version() {
|
|
if test `which node`; then
|
|
local version=`node --version`
|
|
echo " current: $version"
|
|
else
|
|
echo " current: none"
|
|
fi
|
|
}
|
|
|
|
#
|
|
# Display current node --version
|
|
# and others installed.
|
|
#
|
|
|
|
display_versions() {
|
|
display_current_version
|
|
}
|
|
|
|
#
|
|
# Install node <version>
|
|
#
|
|
|
|
install_node() {
|
|
local version=$1
|
|
|
|
# remove "v"
|
|
if test "${version:0:1}" = "v"; then
|
|
version=${version:1:${#version}}
|
|
fi
|
|
|
|
# install
|
|
local dir="node-v$version"
|
|
cd $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\
|
|
&& make install \
|
|
&& cd .. \
|
|
&& cleanup $version
|
|
}
|
|
|
|
#
|
|
# 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
|
|
}
|
|
|
|
# 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 ;;
|
|
*) install_node $1 ;;
|
|
esac
|
|
shift
|
|
done
|
|
fi |