Skip to content

Instantly share code, notes, and snippets.

@cstipkovic
Last active January 31, 2017 17:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cstipkovic/7b8b881817f6f50970159b7ef88659ed to your computer and use it in GitHub Desktop.
Save cstipkovic/7b8b881817f6f50970159b7ef88659ed to your computer and use it in GitHub Desktop.
NodeJS wrapper for run locally using binary

NodeJS Wrapper - run binary locally

Node.js wrapper which will download and proxy to node locally. This allows you to automatically manage the installed version of node.

CONFIGURATION

NODEW_INSTALL_VERSION - an env variable to specify the version of node to install - Defaults to 4.4.7 NODEW_DIST_BASE_URL - an env variable to specify the binary download location - Defaults to NodeJS website

Sample usage: ./npmw install express --save

#!/bin/bash
# Node.js wrapper which will download and proxy to node locally.
# This allows you to automatically manage the installed version of node.
#
# CONFIGURATION
# =============
# NODEW_INSTALL_VERSION - an env variable to specify the version of node to install - Defaults to 4.4.7
# NODEW_DIST_BASE_URL - an env variable to specify the binary download location - Defaults to NodeJS website
#
# Sample usage: ./nodew script.js
INSTALL_PATH="./bin"
INSTALL_VERSION=$NODEW_INSTALL_VERSION
DIST_BASE_URL=$NODEW_DIST_BASE_URL
# default version if not set
if [[ -z "$INSTALL_VERSION" ]]; then
INSTALL_VERSION="6.9.1";
fi
# default download location if not set
if [[ -z "$DIST_BASE_URL" ]]; then
DIST_BASE_URL="https://nodejs.org/dist/";
fi
# create the install directory
if [ ! -d "$INSTALL_PATH" ]; then
echo "Creating directory: $INSTALL_PATH"
mkdir -p "$INSTALL_PATH"
fi
# calculate the absolute path of the location to install files
cd $INSTALL_PATH
INSTALL_PATH=`pwd`
cd - > /dev/null
# identify the OS architecture to download the correct binary
MACHINE_TYPE=`uname -m`
OS=`uname`
if [ ${MACHINE_TYPE} == 'x86_64' ]; then
if [[ ${OS} == 'Darwin' ]]; then
MACHINE_ARCH="darwin-x64"
else
MACHINE_ARCH="linux-x64"
fi
else
MACHINE_ARCH="linux-x86"
fi
# fetch the desired version of node if we don't have a cached version copy
VERSION_FOLDER="node-v$INSTALL_VERSION-$MACHINE_ARCH"
FULL_PATH="$INSTALL_PATH/$VERSION_FOLDER"
if [ ! -d "$FULL_PATH" ]; then
echo "Fetching Node $INSTALL_VERSION to $INSTALL_PATH/node.tar.gz";
wget -O $INSTALL_PATH/node.tar.gz $DIST_BASE_URL/v$INSTALL_VERSION/$VERSION_FOLDER.tar.gz
tar xzf $INSTALL_PATH/node.tar.gz -C $INSTALL_PATH/
rm $INSTALL_PATH/node.tar.gz
fi
# start the node executable
"$FULL_PATH"/bin/node "$@"
#!/bin/bash
# Node.js wrapper which will download and proxy to node locally.
# This allows you to automatically manage the installed version of node.
#
# CONFIGURATION
# =============
# NODEW_INSTALL_VERSION - an env variable to specify the version of node to install - Defaults to 4.4.7
# NODEW_DIST_BASE_URL - an env variable to specify the binary download location - Defaults to NodeJS website
#
# Sample usage: ./npmw install express --save
#
INSTALL_PATH="./bin"
INSTALL_VERSION=$NODEW_INSTALL_VERSION
DIST_BASE_URL=$NODEW_DIST_BASE_URL
# default version if not set
if [[ -z "$INSTALL_VERSION" ]]; then
INSTALL_VERSION="6.9.1";
fi
# default download location if not set
if [[ -z "$DIST_BASE_URL" ]]; then
DIST_BASE_URL="https://nodejs.org/dist/";
fi
# create the install directory
if [ ! -d "$INSTALL_PATH" ]; then
echo "Creating directory: $INSTALL_PATH"
mkdir -p "$INSTALL_PATH"
fi
# calculate the absolute path of the location to install files
cd $INSTALL_PATH
INSTALL_PATH=`pwd`
cd - > /dev/null
# identify the OS architecture to download the correct binary
MACHINE_TYPE=`uname -m`
OS=`uname`
if [ ${MACHINE_TYPE} == 'x86_64' ]; then
if [[ ${OS} == 'Darwin' ]]; then
MACHINE_ARCH="darwin-x64"
else
MACHINE_ARCH="linux-x64"
fi
else
MACHINE_ARCH="linux-x86"
fi
# fetch the desired version of node if we don't have a cached version copy
VERSION_FOLDER="node-v$INSTALL_VERSION-$MACHINE_ARCH"
FULL_PATH="$INSTALL_PATH/$VERSION_FOLDER"
if [ ! -d "$FULL_PATH" ]; then
echo "Fetching Node $INSTALL_VERSION to $INSTALL_PATH/node.tar.gz";
wget -O $INSTALL_PATH/node.tar.gz $DIST_BASE_URL/v$INSTALL_VERSION/$VERSION_FOLDER.tar.gz
tar xzf $INSTALL_PATH/node.tar.gz -C $INSTALL_PATH/
rm $INSTALL_PATH/node.tar.gz
fi
# start the npm executable
"$FULL_PATH"/bin/node "$FULL_PATH"/bin/npm "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment