npm completions: Check command npm existence

Really fixes #3158 and #3152.
This commit is contained in:
Fabian Homborg 2016-06-21 17:42:22 +02:00
parent f9edcbbbe2
commit 10661bb024

View file

@ -35,6 +35,16 @@ function __fish_npm_needs_option
end end
function __fish_complete_npm --description "Complete the commandline using npm's 'completion' tool" function __fish_complete_npm --description "Complete the commandline using npm's 'completion' tool"
# Note that this function will generate undescribed completion options, and current fish
# will sometimes pick these over versions with descriptions.
# However, this seems worth it because it means automatically getting _some_ completions if npm updates.
# Defining an npm alias that automatically calls nvm if necessary is a popular convenience measure.
# Because that is a function, these local variables won't be inherited and the completion would fail
# with weird output on stdout (!). But before the function is called, no npm command is defined,
# so calling the command would fail.
# So we'll only try if we have an npm command.
if command -s npm >/dev/null
# npm completion is bash-centric, so we need to translate fish's "commandline" stuff to bash's $COMP_* stuff # npm completion is bash-centric, so we need to translate fish's "commandline" stuff to bash's $COMP_* stuff
# COMP_LINE is an array with the words in the commandline # COMP_LINE is an array with the words in the commandline
set -lx COMP_LINE (commandline -o) set -lx COMP_LINE (commandline -o)
@ -51,6 +61,7 @@ function __fish_complete_npm --description "Complete the commandline using npm's
end end
command npm completion -- $COMP_LINE ^/dev/null command npm completion -- $COMP_LINE ^/dev/null
end end
end
# use npm completion for most of the things, # use npm completion for most of the things,
# except options completion because it sucks at it. # except options completion because it sucks at it.
@ -60,6 +71,8 @@ complete -f -c npm -n 'not __fish_npm_needs_option' -a "(__fish_complete_npm)"
# list available npm scripts and their parial content # list available npm scripts and their parial content
function __fish_npm_run function __fish_npm_run
# Like above, only try to call npm if there's a command by that name to facilitate aliases that call nvm.
if command -s npm >/dev/null
command npm run | string match -r -v '^[^ ]|^$' | string trim | while read -l name command npm run | string match -r -v '^[^ ]|^$' | string trim | while read -l name
set -l trim 20 set -l trim 20
read -l value read -l value
@ -67,6 +80,7 @@ function __fish_npm_run
printf "%s\t%s\n" $name $value printf "%s\t%s\n" $name $value
end end
end end
end
# run # run
for c in run run-script for c in run run-script