npm.sh 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/bin/bash
  2. ###-begin-npm-completion-###
  3. #
  4. # npm command completion script
  5. #
  6. # Installation: npm completion >> ~/.bashrc (or ~/.zshrc)
  7. # Or, maybe: npm completion > /usr/local/etc/bash_completion.d/npm
  8. #
  9. if type complete &>/dev/null; then
  10. _npm_completion () {
  11. local words cword
  12. if type _get_comp_words_by_ref &>/dev/null; then
  13. _get_comp_words_by_ref -n = -n @ -w words -i cword
  14. else
  15. cword="$COMP_CWORD"
  16. words=("${COMP_WORDS[@]}")
  17. fi
  18. local si="$IFS"
  19. IFS=$'\n' COMPREPLY=($(COMP_CWORD="$cword" \
  20. COMP_LINE="$COMP_LINE" \
  21. COMP_POINT="$COMP_POINT" \
  22. npm completion -- "${words[@]}" \
  23. 2>/dev/null)) || return $?
  24. IFS="$si"
  25. }
  26. complete -o default -F _npm_completion npm
  27. elif type compdef &>/dev/null; then
  28. _npm_completion() {
  29. local si=$IFS
  30. compadd -- $(COMP_CWORD=$((CURRENT-1)) \
  31. COMP_LINE=$BUFFER \
  32. COMP_POINT=0 \
  33. npm completion -- "${words[@]}" \
  34. 2>/dev/null)
  35. IFS=$si
  36. }
  37. compdef _npm_completion npm
  38. elif type compctl &>/dev/null; then
  39. _npm_completion () {
  40. local cword line point words si
  41. read -Ac words
  42. read -cn cword
  43. let cword-=1
  44. read -l line
  45. read -ln point
  46. si="$IFS"
  47. IFS=$'\n' reply=($(COMP_CWORD="$cword" \
  48. COMP_LINE="$line" \
  49. COMP_POINT="$point" \
  50. npm completion -- "${words[@]}" \
  51. 2>/dev/null)) || return $?
  52. IFS="$si"
  53. }
  54. compctl -K _npm_completion npm
  55. fi
  56. ###-end-npm-completion-###