npm.plugin.zsh 2.7 KB

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