diff --git a/share/completions/npm.fish b/share/completions/npm.fish index d531dd1d3..0c57814a4 100644 --- a/share/completions/npm.fish +++ b/share/completions/npm.fish @@ -64,21 +64,28 @@ function __fish_complete_npm --description "Complete the commandline using npm's end # use npm completion for most of the things, -# except options completion because it sucks at it. +# except options completion (because it sucks at it) +# and run-script completion (reading package.json is faster). # see: https://github.com/npm/npm/issues/9524 # and: https://github.com/fish-shell/fish-shell/pull/2366 -complete -f -c npm -n 'not __fish_npm_needs_option' -a "(__fish_complete_npm)" +complete -f -c npm -n 'not __fish_npm_needs_option; and not __fish_npm_using_command run; and not __fish_npm_using_command run-script' -a "(__fish_complete_npm)" # list available npm scripts and their parial content +function __fish_parse_npm_run_completions + while read -l name + set -l trim 20 + read -l value + set value (string sub -l $trim -- $value) + printf "%s\t%s\n" $name $value + end +end + 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 -sq npm - command npm run | string match -r -v '^[^ ]|^$' | string trim | while read -l name - set -l trim 20 - read -l value - echo "$value" | cut -c1-$trim | read -l value - printf "%s\t%s\n" $name $value - end + if command -sq jq; and test -e package.json + jq -r '.scripts | to_entries[] | .key,.value'