Skip to content

Instantly share code, notes, and snippets.

@croach
Created March 16, 2014 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save croach/9577517 to your computer and use it in GitHub Desktop.
Save croach/9577517 to your computer and use it in GitHub Desktop.
A set of bash/zsh functions that provide simple virtual environment capabilities for node.
#
# A set of bash/zsh functions that provide simple virtual environment
# capabilities for node. To install, just copy this file into your home
# directory and add the following to your .bashrc or .zshrc file:
#
# if [[ -f "$HOME/.nenv" ]]; then
# source "$HOME/.nenv"
# fi
#
# What this script provides is a set of functions that are activated whenever
# you cd into a new directory. If that directory contains a node_modules folder,
# it will add all of the installed modules' bin folders to the PATH. Once you
# exit the folder, the PATH is returned back to its original state.
#
# In addition, if a .activate file is found in the directory, it will source
# it upon entry. Likewise, if a .deactivate file is found, it will source it
# upon exit. These files allow you to do extra setup/teardown to your
# environment as needed.
#
# Checks that the child directory is a subdirectory of the parent
is_subdirectory() {
local child="$1"
local parent="$2"
if [[ "${child##${parent}}" != "$child" ]]; then
return 0
else
return 1
fi
}
# Activates a new environment
activate_env() {
# Exit the function if npm isn't installed
if ! npm -v >/dev/null 2>&1; then
return 1
fi
# Check if the directory we've cd'ed into is a node environment directory
# (i.e., it contains a node_modules folder) and that a node envrionment
# does not already exist before creating a new one.
if [ -d "node_modules" ] && [ -z "$_ENV_DIR" ]; then
# Save the old PATH variable so we can revert back to it when we leave
# the environment
export _OLD_PATH="$PATH"
# An environment is essentially nothing more than an environment
# variable (_ENV_DIR) pointing the parent directory of our node
# environment. Create the variable and point it to $PWD.
export _ENV_DIR="$PWD"
# Add the bin folder for all local NPM installs to the PATH
export PATH="$(npm bin):$PATH"
# If an activation script exists, execute it
if [ -e ".activate" ]; then
source .activate
fi
fi
}
# Deactivates the current envrionment
deactivate_env() {
# Make sure that an envrionment does exist and that the new
# directory is not a subdirectory of the envrionment directory
if [ -n "$_ENV_DIR" ] && ! is_subdirectory "$PWD" "$_ENV_DIR"; then
# Run the deactivation script if it exists
if [[ -e "$_ENV_DIR/.deactivate" ]]; then
source "$_ENV_DIR/.deactivate"
fi
# Revert back to the original PATH
export PATH="$_OLD_PATH"
# Destroy the environment
unset _ENV_DIR
unset _OLD_PATH
fi
}
env_cd() {
builtin cd "$@" && deactivate_env && activate_env
}
alias cd="env_cd"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment