2
0

npm.plugin.zsh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. # Run npm run build
  56. alias npmrb="npm run build"
  57. npm_toggle_install_uninstall() {
  58. # Look up to the previous 2 history commands
  59. local line
  60. for line in "$BUFFER" \
  61. "${history[$((HISTCMD-1))]}" \
  62. "${history[$((HISTCMD-2))]}"
  63. do
  64. case "$line" in
  65. "npm uninstall"*)
  66. BUFFER="${line/npm uninstall/npm install}"
  67. (( CURSOR = CURSOR + 2 )) # uninstall -> install: 2 chars removed
  68. ;;
  69. "npm install"*)
  70. BUFFER="${line/npm install/npm uninstall}"
  71. (( CURSOR = CURSOR + 2 )) # install -> uninstall: 2 chars added
  72. ;;
  73. "npm un "*)
  74. BUFFER="${line/npm un/npm install}"
  75. (( CURSOR = CURSOR + 5 )) # un -> install: 5 chars added
  76. ;;
  77. "npm i "*)
  78. BUFFER="${line/npm i/npm uninstall}"
  79. (( CURSOR = CURSOR + 8 )) # i -> uninstall: 8 chars added
  80. ;;
  81. *) continue ;;
  82. esac
  83. return 0
  84. done
  85. BUFFER="npm install"
  86. CURSOR=${#BUFFER}
  87. }
  88. zle -N npm_toggle_install_uninstall
  89. # Defined shortcut keys: [F2] [F2]
  90. bindkey -M emacs '^[OQ^[OQ' npm_toggle_install_uninstall
  91. bindkey -M vicmd '^[OQ^[OQ' npm_toggle_install_uninstall
  92. bindkey -M viins '^[OQ^[OQ' npm_toggle_install_uninstall