Created
July 19, 2024 13:51
-
-
Save fathergoose/27e6772530750a0097659c2902f6e483 to your computer and use it in GitHub Desktop.
Zsh / bash dynamic completions for npx binaries installed in a project
This file contains 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
## Depends on gnu-find | |
# brew install findutils | |
## ~/.zshrc | |
# autoload bashcompinit | |
# bashcompinit | |
# source ~/.config/zsh/bashcompinit/npx.bash | |
_npx() { | |
local dir=$(pwd -P) | |
while [[ -n "$dir" ]]; do | |
if [[ ! -d $dir/node_modules/.bin ]]; then | |
dir=${dir%/*} | |
continue | |
fi | |
local execs=($( | |
cd $dir/node_modules/.bin | |
gfind -L . -type f -executable | |
)) | |
execs=(${execs[@]/#.\//}) | |
local cur=${COMP_WORDS[COMP_CWORD]} | |
COMPREPLY=($(compgen -W "${execs[*]}" -- "$cur")) | |
break | |
done | |
} | |
complete -F _npx npx |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment