husky.sh 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. debug () {
  2. if [ "$HUSKY_DEBUG" = "true" ] || [ "$HUSKY_DEBUG" = "1" ]; then
  3. echo "husky:debug $1"
  4. fi
  5. }
  6. command_exists () {
  7. command -v "$1" >/dev/null 2>&1
  8. }
  9. run_command () {
  10. if command_exists "$1"; then
  11. "$@" husky-run $hookName $gitParams
  12. exitCode="$?"
  13. debug "$* husky-run exited with $exitCode exit code"
  14. if [ $exitCode -eq 127 ]; then
  15. echo "Can't find Husky, skipping $hookName hook"
  16. echo "You can reinstall it using 'npm install husky --save-dev' or delete this hook"
  17. else
  18. exit $exitCode
  19. fi
  20. else
  21. echo "Can't find $1 in PATH: $PATH"
  22. echo "Skipping $hookName hook"
  23. exit 0
  24. fi
  25. }
  26. hookIsDefined () {
  27. grep -qs $hookName \
  28. package.json \
  29. .huskyrc \
  30. .huskyrc.json \
  31. .huskyrc.yaml \
  32. .huskyrc.yml
  33. }
  34. huskyVersion="0.0.0"
  35. gitParams="$*"
  36. hookName="$(basename "$0")"
  37. debug "husky v$huskyVersion - $hookName"
  38. # Skip if HUSKY_SKIP_HOOKS is set
  39. if [ "$HUSKY_SKIP_HOOKS" = "true" ] || [ "$HUSKY_SKIP_HOOKS" = "1" ]; then
  40. debug "HUSKY_SKIP_HOOKS is set to $HUSKY_SKIP_HOOKS, skipping hook"
  41. exit 0
  42. fi
  43. # Source user var and change directory
  44. . "$(dirname "$0")/husky.local.sh"
  45. debug "Current working directory is $(pwd)"
  46. # Skip fast if hookName is not defined
  47. # Don't skip if .huskyrc.js or .huskyrc.config.js are used as the heuristic could
  48. # fail due to the dynamic aspect of JS. For example:
  49. # `"pre-" + "commit"` or `require('./config/hooks')`)
  50. if [ ! -f .huskyrc.js ] && [ ! -f huskyrc.cjs ] && [ ! -f husky.config.js ] && [ ! -f husky.config.cjs ] && ! hookIsDefined; then
  51. debug "$hookName config not found, skipping hook"
  52. exit 0
  53. fi
  54. # Source user ~/.huskyrc
  55. if [ -f ~/.huskyrc ]; then
  56. debug "source ~/.huskyrc"
  57. . ~/.huskyrc
  58. fi
  59. # Set HUSKY_GIT_STDIN from stdin
  60. case $hookName in
  61. "pre-push"|"post-rewrite")
  62. export HUSKY_GIT_STDIN="$(cat)";;
  63. esac
  64. # Windows 10, Git Bash and Yarn 1 installer
  65. if command_exists winpty && test -t 1; then
  66. exec < /dev/tty
  67. fi
  68. # Run husky-run with the package manager used to install Husky
  69. case $packageManager in
  70. "npm") run_command npx --no-install;;
  71. "npminstall") run_command npx --no-install;;
  72. "pnpm") run_command pnpx --no-install;;
  73. "yarn") run_command yarn run --silent;;
  74. *) echo "Unknown package manager: $packageManager"; exit 0;;
  75. esac