-
Notifications
You must be signed in to change notification settings - Fork 4
/
zsh-npm-scripts-autocomplete.plugin.zsh
62 lines (55 loc) · 1.44 KB
/
zsh-npm-scripts-autocomplete.plugin.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
local _plugin_path=$0
local _PWD=`echo $_plugin_path | sed -e 's/\/zsh-npm-scripts-autocomplete\.plugin\.zsh//'`
__zna_pwd="$_PWD"
__znsaGetScripts() {
local pkgJson="$1"
node "$__zna_pwd/getScripts.js" "$pkgJson" 2>/dev/null
}
__znsaFindFile() {
local filename="$1"
local dir=$PWD
while [ ! -e "$dir/$filename" ]; do
dir=${dir%/*}
[[ "$dir" = "" ]] && break
done
[[ ! "$dir" = "" ]] && echo "$dir/$filename"
}
__znsaArgsLength() {
echo "$#words"
}
__znsaYarnRunCompletion() {
[[ ! "$(__znsaArgsLength)" = "2" ]] && return
local pkgJson="$(__znsaFindFile package.json)"
[[ "$pkgJson" = "" ]] && return
local -a options
options=(${(f)"$(__znsaGetScripts $pkgJson)"})
[[ "$#options" = 0 ]] && return
_describe 'values' options
}
## to lazy to handler different number of arguments
## just copy and paste it
__znsaNpmRunCompletion() {
[[ ! "$(__znsaArgsLength)" = "3" ]] && return
local pkgJson="$(__znsaFindFile package.json)"
[[ "$pkgJson" = "" ]] && return
local -a options
options=(${(f)"$(__znsaGetScripts $pkgJson)"})
[[ "$#options" = 0 ]] && return
_describe 'values' options
}
__znsaHandleYarn() {
__znsaYarnRunCompletion
}
__znsaHandleNpm(){
case "${words[2]}" in
run)
__znsaNpmRunCompletion
;;
esac
}
alias nr="npm run"
compdef __znsaYarnRunCompletion yarn
compdef __znsaYarnRunCompletion nr
compdef __znsaYarnRunCompletion pnpm
compdef __znsaHandleNpm npm
compdef __znsaHandleNpm bun