UNPKG

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