npm.plugin.zsh
2836 bytes
1(( $+commands[npm] )) && {
2 command rm -f "${ZSH_CACHE_DIR:-$ZSH/cache}/npm_completion"
3
4 _npm_completion() {
5 local si=$IFS
6 compadd -- $(COMP_CWORD=$((CURRENT-1)) \
7 COMP_LINE=$BUFFER \
8 COMP_POINT=0 \
9 npm completion -- "${words[@]}" \
10 2>/dev/null)
11 IFS=$si
12 }
13 compdef _npm_completion npm
14}
15
16# Install dependencies globally
17alias npmg="npm i -g "
18
19# npm package names are lowercase
20# Thus, we've used camelCase for the following aliases:
21
22# Install and save to dependencies in your package.json
23# npms is used by https://www.npmjs.com/package/npms
24alias npmS="npm i -S "
25
26# Install and save to dev-dependencies in your package.json
27# npmd is used by https://github.com/dominictarr/npmd
28alias npmD="npm i -D "
29
30# Force npm to fetch remote resources even if a local copy exists on disk.
31alias npmF='npm i -f'
32
33# Execute command from node_modules folder based on current directory
34# i.e npmE gulp
35alias npmE='PATH="$(npm bin)":"$PATH"'
36
37# Check which npm modules are outdated
38alias npmO="npm outdated"
39
40# Update all the packages listed to the latest version
41alias npmU="npm update"
42
43# Check package versions
44alias npmV="npm -v"
45
46# List packages
47alias npmL="npm list"
48
49# List top-level installed packages
50alias npmL0="npm ls --depth=0"
51
52# Run npm start
53alias npmst="npm start"
54
55# Run npm test
56alias npmt="npm test"
57
58# Run npm scripts
59alias npmR="npm run"
60
61# Run npm publish
62alias npmP="npm publish"
63
64# Run npm init
65alias npmI="npm init"
66
67# Run npm info
68alias npmi="npm info"
69
70# Run npm search
71alias npmSe="npm search"
72
73# Run npm run dev
74alias npmrd="npm run dev"
75
76# Run npm run build
77alias npmrb="npm run build"
78
79npm_toggle_install_uninstall() {
80 # Look up to the previous 2 history commands
81 local line
82 for line in "$BUFFER" \
83 "${history[$((HISTCMD-1))]}" \
84 "${history[$((HISTCMD-2))]}"
85 do
86 case "$line" in
87 "npm uninstall"*)
88 BUFFER="${line/npm uninstall/npm install}"
89 (( CURSOR = CURSOR + 2 )) # uninstall -> install: 2 chars removed
90 ;;
91 "npm install"*)
92 BUFFER="${line/npm install/npm uninstall}"
93 (( CURSOR = CURSOR + 2 )) # install -> uninstall: 2 chars added
94 ;;
95 "npm un "*)
96 BUFFER="${line/npm un/npm install}"
97 (( CURSOR = CURSOR + 5 )) # un -> install: 5 chars added
98 ;;
99 "npm i "*)
100 BUFFER="${line/npm i/npm uninstall}"
101 (( CURSOR = CURSOR + 8 )) # i -> uninstall: 8 chars added
102 ;;
103 *) continue ;;
104 esac
105 return 0
106 done
107
108 BUFFER="npm install"
109 CURSOR=${#BUFFER}
110}
111
112zle -N npm_toggle_install_uninstall
113
114# Defined shortcut keys: [F2] [F2]
115bindkey -M emacs '^[OQ^[OQ' npm_toggle_install_uninstall
116bindkey -M vicmd '^[OQ^[OQ' npm_toggle_install_uninstall
117bindkey -M viins '^[OQ^[OQ' npm_toggle_install_uninstall