Created
November 27, 2012 18:13
-
-
Save geek/4155985 to your computer and use it in GitHub Desktop.
Set path to node_modules bin
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
export PATH="$HOME/bin:$PATH" | |
##### NPM STUFF | |
# 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() { | |
# 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