mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-26 12:53:13 +00:00
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
This commit is contained in:
parent
8e2d165756
commit
a071deaf61
1 changed files with 16 additions and 9 deletions
|
@ -64,22 +64,29 @@ function __fish_complete_npm --description "Complete the commandline using npm's
|
||||||
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)
|
||||||
|
# and run-script completion (reading package.json is faster).
|
||||||
# see: https://github.com/npm/npm/issues/9524
|
# see: https://github.com/npm/npm/issues/9524
|
||||||
# and: https://github.com/fish-shell/fish-shell/pull/2366
|
# 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
|
# list available npm scripts and their parial content
|
||||||
function __fish_npm_run
|
function __fish_parse_npm_run_completions
|
||||||
# Like above, only try to call npm if there's a command by that name to facilitate aliases that call nvm.
|
while read -l name
|
||||||
if command -sq npm
|
|
||||||
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
|
||||||
echo "$value" | cut -c1-$trim | read -l value
|
set value (string sub -l $trim -- $value)
|
||||||
printf "%s\t%s\n" $name $value
|
printf "%s\t%s\n" $name $value
|
||||||
end
|
end
|
||||||
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 jq; and test -e package.json
|
||||||
|
jq -r '.scripts | to_entries[] | .key,.value' <package.json | __fish_parse_npm_run_completions
|
||||||
|
else if command -sq npm
|
||||||
|
command npm run | string match -r -v '^[^ ]|^$' | string trim | __fish_parse_npm_run_completions
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# run
|
# run
|
||||||
|
|
Loading…
Reference in a new issue