Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 174 lines (135 sloc) 2.838 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
#!/usr/bin/env sh

# Library version

VERSION="0.0.1"
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 "... $@"
}

#
# Exit with the given <msg ...>
#

abort() {
  echo "Error: $@" && exit 1
}

#
# Output usage information.
#

display_help() {
  cat <<-help

Usage: n [options] <version> [config ...]

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
}

#
# Check for installed version, and populate $version
#

check_current_version() {
  if test `which node`; then
installed=`node --version`
    installed=${installed##*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" = "$installed"; 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=${installed##*v}
  fi

  # already active
  test "$version" = "$installed" && 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
}

# 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 $@; exit ;;
    esac
shift
done
fi
Something went wrong with that request. Please try again.