Last active
March 10, 2016 08:27
-
-
Save ryankurte/a31daedd451c39bb0a67 to your computer and use it in GitHub Desktop.
Project local node.js installer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Setup a project local environment for a nodejs project | |
# This installs and links a local node instances so that node and application versions can be coupled. | |
# Installation is both local to the project, and does not require superuser permissions. | |
# Inspired by Python's virtualenv. For more information, see http://wp.me/pZZvH-an | |
# | |
# Usage: | |
# 1. Add this file to your node project | |
# 2. Run with `source ./setup.sh` to setup a local node.js instance | |
# | |
NODEVER="v5.8.0" | |
WORKDIR=`pwd` | |
TOOLDIR="$WORKDIR/.env" | |
# Check this is running in the correct shell | |
if [ $0 = $BASH_SOURCE ]; then | |
echo "Warning: $0 should be run using source (ie. 'source nodeenv.sh')" | |
echo "The local node instance will need to be manually added to your path" | |
MODE="shell" | |
set -e | |
else | |
MODE="source" | |
fi | |
# Determine OS | |
if [ "$(uname -s)" == "Darwin" ]; then | |
OS="darwin" | |
elif [ "$(uname -s)" == "Linux" ]; then | |
OS="linux" | |
else | |
OS="unknown" | |
fi | |
# Determine architecture | |
if [ "$(uname -m)" == "x86_64" ]; then | |
ARCH="x64" | |
elif [ "$(uname -m)" == "x86" ]; then | |
ARCH="x86" | |
elif [ "$(uname -m)" == "armv7l" ]; then | |
ARCH="armv7" | |
else | |
ARCH="unknown" | |
fi | |
NODENAME="node-$NODEVER-$OS-$ARCH" | |
NODEBIN="$NODENAME.tar.gz" | |
NODESRC="node-$NODEVER.tar.gz" | |
echo "Version: $NODENAME" | |
NODEDIR="$TOOLDIR/$NODENAME/bin" | |
# Locate binaries | |
NPM="$NODEDIR/npm" | |
NODE="$NODEDIR/node" | |
function handleExit () { | |
if [ "$MODE" == "source" ]; then | |
return $1 | |
else | |
exit $1 | |
fi | |
} | |
function installFromTar { | |
CACHED="$HOME/.env/$NODEBIN" | |
if [ -f $CACHED ]; then | |
echo "Archive found at: $CACHED" | |
else | |
echo "Cached archive not found, downloading" | |
wget https://nodejs.org/dist/$NODEVER/$NODEBIN | |
mv $NODEBIN $CACHED | |
fi | |
if [ ! -f $TOOLDIR/$NODEBIN ] | |
then cp $CACHED $TOOLDIR | |
fi | |
tar -xf $TOOLDIR/$NODEBIN -C $TOOLDIR | |
} | |
function installFromSource { | |
CACHED="$HOME/.env/$NODESRC" | |
if [ -f $CACHED ]; then | |
echo "Archive found at: $CACHED" | |
else | |
echo "Cached archive not found, downloading" | |
wget https://nodejs.org/dist/$NODEVER/$NODESRC | |
mv $NODESRC $CACHED | |
fi | |
if [ ! -f $TOOLDIR/$NODESRC ] | |
then cp $CACHED $TOOLDIR | |
fi | |
tar -xf $TOOLDIR/$NODESRC -C $TOOLDIR | |
# Build and install | |
cd $TOOLDIR/node-$NODEVER; ./configure; make -j4; PREFIX=$TOOLDIR/node-$NODEVER-$OS-$ARCH make install; cd $WORKDIR | |
} | |
# Create and export expected location | |
mkdir -p $TOOLDIR | |
mkdir -p $HOME/.env | |
export PATH=$NODEDIR:$PATH | |
# Check for existing installation | |
if [ -f $NPM ]; then | |
echo "Local Node.js installation found, setup complete" | |
handleExit 0 | |
else | |
echo "Not found, installing local Node.js" | |
mkdir -p $TOOLDIR | |
# Install | |
if [ "$ARCH" != "unknown" ]; then | |
echo "Recognized architecture: $ARCH, Installing from TAR" | |
installFromTar | |
else | |
echo "Unknown architecture, installing from source" | |
installFromSource | |
fi | |
# Symlink to /node for ease of use | |
ln -sF $TOOLDIR/node-$NODEVER-$OS-$ARCH $TOOLDIR/node | |
echo "Installation complete" | |
handleExit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment