Browse Source

Refactor into functions

pull/2/head
Luke Childs 8 years ago
parent
commit
1d21efa230
  1. 57
      zsh-better-npm-completion.plugin.zsh

57
zsh-better-npm-completion.plugin.zsh

@ -1,21 +1,45 @@
_zsh_better_npm_completion() {
_zbnc_npm_command() {
echo "${words[2]}"
}
_zbnc_recursively_look_for() {
local filename="$1"
local dir=$PWD
while [ ! -e "$dir/$filename" ]; do
dir=${dir%/*}
[ "$dir" = "" ] && break
done
[ ! "$dir" = "" ] && echo "$dir/$filename"
}
_zbnc_parse_package_json_for_script_suggestions() {
local package_json="$1"
cat "$package_json" |
sed -nE '/^ "scripts": \{$/,/^ \},?$/p' | # Grab scripts object
sed '1d;$d' | # Remove first/last lines
sed -E 's/ "([^"]+)": "([^"]+)",?/\1:$ \2/' | # Parse commands into suggestions
sed 's/\(:\)[^ ]*:/\\&/' # Escape ":" in commands
}
_zbnc_default_npm_completion() {
compadd -- $(COMP_CWORD=$((CURRENT-1)) \
COMP_LINE=$BUFFER \
COMP_POINT=0 \
npm completion -- "${words[@]}" \
2>/dev/null)
}
_zbnc_zsh_better_npm_completion() {
# If we're on the run command
if [ "${words[2]}" = "run" ]; then
if [ "$(_zbnc_npm_command)" = "run" ]; then
# Look for a package.json file
local filename="package.json"
local dir=$PWD
while [ ! -e "$dir/$filename" ]; do
dir=${dir%/*}
if [ "$dir" = "" ]; then
break
fi
done
local package_json="$(_zbnc_recursively_look_for package.json)"
# If we have one, parse the scripts
if [ ! "$dir" = "" ]; then
local options=("${(@f)$(cat "$dir/$filename" | sed -nE '/^ "scripts": \{$/,/^ \},?$/p' | sed '1d;$d' | sed -E 's/ "([^"]+)": "([^"]+)",?/\1:$ \2/' | sed 's/\(:\)[^ ]*:/\\&/')}")
if [ ! "$package_json" = "" ]; then
local options=("${(@f)$(_zbnc_parse_package_json_for_script_suggestions "$package_json")}")
if [ ! "$#options" = 0 ]; then
_describe 'values' options
return
@ -24,12 +48,7 @@ _zsh_better_npm_completion() {
fi
# Fall back to default completion if anything above failed
compadd -- $(COMP_CWORD=$((CURRENT-1)) \
COMP_LINE=$BUFFER \
COMP_POINT=0 \
npm completion -- "${words[@]}" \
2>/dev/null)
IFS=$si
_zbnc_default_npm_completion
}
compdef _zsh_better_npm_completion npm
compdef _zbnc_zsh_better_npm_completion npm

Loading…
Cancel
Save