From a071deaf6176769088de5141e663c577108aa63c Mon Sep 17 00:00:00 2001 From: Thales Mello Date: Wed, 26 Jul 2017 08:31:35 -0300 Subject: [PATCH] Make npm run-script completion faster with `jq` (#4241) * Make npm run-script completion faster with `jq` When jq is available, it's actually faster to invoke jq and parse the `package.json` invoking the `npm` command. Also, prior to this commit, both `__fish_complete_npm` and `__fish_npm_run` were being run whenever completions for `npm run` subcommand was being used, which was actually making repetitive work (invoking npm command twice). This pull request is supposed to make completion without `jq` faster as well * Refactor npm.fish for code reutilization Created function to handle both cases of npm run completion parse, with or without `jq` completion. * Remove unecessary blank line --- share/completions/npm.fish | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) 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'