Skip to content

Instantly share code, notes, and snippets.

@nweldev
Forked from natchiketa/yo-completion.sh
Last active January 30, 2016 23:41
Show Gist options
  • Save nweldev/95d9aa4d37348e11fac6 to your computer and use it in GitHub Desktop.
Save nweldev/95d9aa4d37348e11fac6 to your computer and use it in GitHub Desktop.
# Bash completion for Yeoman generators by Noel Mace
# forked from https://gist.github.com/natchiketa/6095984
# support angular "sub" generators (as angular:controller)
# just tested on Debian for now
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/lib if present
local usr_local_modules=$(if [ -d /usr/lib/node_modules ]; then
echo "/usr/lib/node_modules:";
else
if [ -d /usr/local/lib/node_modules ]; then
echo "/usr/local/lib/node_modules:";
fi
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)
# concat and also add $NODE_PATH
local node_dirs="${local_modules}${usr_local_modules}${win_roam_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)
# sub generators for angular
local angular_sub_gens="controller directive filter route service provider factory value constant decorator view"
if [[ "$generators_all" == *"angular"* ]]; then
for sub_gen in $angular_sub_gens; do
generators_all=$generators_all" angular\:"$sub_gen
done
fi
local prev=${COMP_WORDS[COMP_CWORD-1]}
# get the word fragment whitout considering colon as a separator
# see http://stackoverflow.com/questions/10528695/how-to-reset-comp-wordbreaks-without-effecting-other-completion-script
local cur
_get_comp_words_by_ref -n : cur
#local cur=${COMP_WORDS[COMP_CWORD]}
if [[ $cur != "-*" ]] ; then
COMPREPLY=( $(compgen -W "$generators_all" -- "$cur") )
__ltrim_colon_completions "$cur"
return 0
fi
}
complete -F _yo_generator_complete_ yo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment