Skip to content

Instantly share code, notes, and snippets.

@lourenzo
Forked from natchiketa/yo-completion.sh
Last active August 29, 2015 14:21
Show Gist options
  • Save lourenzo/cc347e7a8fcb25c3451b to your computer and use it in GitHub Desktop.
Save lourenzo/cc347e7a8fcb25c3451b to your computer and use it in GitHub Desktop.
Added nodebrew support
# Bash completion for Yeoman generators - tested in Ubuntu, OS X and Windows (using Git bash)
function _yo_generator_complete_() {
# local node_modules if present
local local_modules=$(if [ -d node_modules ]; then echo "node_modules:"; fi)
# node_modules in /usr/local/lib if present
local usr_local_modules=$(if [ -d /usr/local/lib/node_modules ]; then echo "/usr/local/lib/node_modules:"; fi)
# node_modules in user's Roaming/npm (Windows) if present
local win_roam_modules=$(if [ -d $(which yo)/../node_modules ]; then echo "$(which yo)/../node_modules:"; fi)
# also, node_modules in nodebrew current
local nodebrew_modules=$(if [ -d ${HOME}/.nodebrew/current/lib/node_modules ]; then echo "${HOME}/.nodebrew/current/lib/node_modules:"; fi)
# concat and also add $NODE_PATH
local node_dirs="${local_modules}${usr_local_modules}${win_roam_modules}${nodebrew_modules}${NODE_PATH}"
# split $node_dirs and return anything starting with 'generator-', minus that prefix
local generators_all=$(for dir in $(echo $node_dirs | tr ":" "\n"); do command ls -1 $dir | grep ^generator- | cut -c11-; done)
# get the word fragment
local word=${COMP_WORDS[COMP_CWORD]}
# don't attempt to filter w/`grep` if `$word` is empty
local generators_filtered=$(if [ -z "$word" ]; then echo "$generators_all"; else echo "$generators_all" | grep $word; fi)
COMPREPLY=($generators_filtered)
}
complete -F _yo_generator_complete_ yo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment