Skip to content

Instantly share code, notes, and snippets.

@joaodubas
Last active December 12, 2015 10:19
Show Gist options
  • Save joaodubas/4758644 to your computer and use it in GitHub Desktop.
Save joaodubas/4758644 to your computer and use it in GitHub Desktop.
Add node_modules to PATH environment variable.
# environment variables
bindir=$HOME/local/bin
if [[ ":$PATH:" != *":$bindir:"* ]]; then
PATH=$bindir:$PATH
export PATH
fi
# local development
export PROJECTS=$HOME/public
# virtualenv
export WORKON_HOME=$HOME/.venv
export PROJECT_HOME=$HOME/www
. /usr/local/bin/virtualenvwrapper.sh
# todo.txt-cli configuration
export TODOTXT_CFG_FILE=$HOME/.todo/todo.cfg
. $bindir/todo_completion
alias t="todo -d $TODOTXT_CFG_FILE"
complete -F _todo t
alias punch='/usr/bin/python $bindir/punch'
# aliases
sublime() {
$bindir/sublime $@ &
}
# update bash path
pathup() {
local action="$1"; shift
case "$action" in
popd)
[[ $# -eq 0 ]] && builtin popd || builtin popd "$*" ;;
cd|pushd)
builtin $action "$*" ;;
*)
return ;;
esac
PATH=`/usr/bin/python $HOME/.path_env.py`
export PATH
}
alias cd="pathup cd"
alias pushd="pathup pushd"
alias popd="pathup popd"
#!/usr/bin/env python
# encoding: utf-8
import os
import sys
PROJECT_HOME = os.environ.get(
'PROJECTS',
os.path.join(os.environ.get('HOME'), 'public')
)
def back(path):
"""back(path) -- Get One level up in the tree from the path.
-- path: a system path
"""
return os.path.normpath(os.path.join(path, '..'))
def get_bin(path):
"""get_bin(path) -- Get the bin directory for the project.
-- path: a project path
"""
found = False
bindir = ''
while not found:
bindir = os.path.join(path, 'node_modules', '.bin')
found = os.path.exists(bindir) or path == PROJECT_HOME
path = back(path)
return bindir
def remove_project_from_path(path):
"""remove_project_from_path(path) -- Remove project from path directive.
-- path: environment $PATH variable
"""
return ':'.join([pathname for pathname in path.split(':') \
if PROJECT_HOME not in pathname])
def add_project_to_path(project, path, sep=':'):
"""add_project_to_path(project, path, sep) -- Add project path to path
directive.
-- project: project pathname
-- path: environment $PATH variable
-- sep: separator used in $PATH variable
"""
if project in path:
return path
return '{0}{1}{2}'.format(project, sep, path)
def normalize_path(path):
"""normalize_path(path) -- Normalize path by adding the current working
directory and stripping led slash.
-- path: variable passed by the shell script
"""
if not path.startswith('/'):
path = os.path.join(os.getcwd(), *path.split(os.path.sep))
if path.endswith('/'):
path = path[:-1]
return path
def main(cwd=None):
path = remove_project_from_path(os.environ.get('PATH'))
if PROJECT_HOME not in cwd or cwd == PROJECT_HOME:
return path
bindir = get_bin(cwd)
if not os.path.exists(bindir):
return path
return add_project_to_path(bindir, path)
if __name__ == '__main__':
envpath = os.getcwd()
sys.stdout.write(main(envpath))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment