Skip to content

Commit 1b830e9

Browse files
committed
[IMP] husky: allow use of pre-commit hook for windows
The PATH variable for husky in windows was sometimes missing the "/usr/bin" directory. I'm not sure what caused this, I had the bug sometimes but couldn't reproduce it consistently. On Windows, use `npx.cmd` instead of `npx`. Add a HUSKY_INPUT_ENV environment variable to disable user input in the pre-commit hook. It should be set to 0 if you use git with a GUI, in which you cannot use console input for commits closes #1522 X-original-commit: 0e0a1cd Signed-off-by: Minne Adrien (adrm) <[email protected]> Signed-off-by: Rémi Rahir (rar) <[email protected]>
1 parent 6306bad commit 1b830e9

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

.husky/pre-commit

+28-14
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,39 @@
11
#!/bin/sh
2-
. "$(dirname "$0")/_/husky.sh"
32

3+
# Husky Env Variables :
4+
# HUSKY_PRE_COMMIT : if set to 0, disable pre-commit hook
5+
# HUSKY_INPUT_ENV: if set to 0, disable the possibility of user input for the hook
6+
7+
npx="npx"
8+
9+
# For Windows
10+
if [ "$OSTYPE" = "msys" ]; then
11+
export PATH="/usr/bin:/usr/local/bin:$PATH"
12+
npx="npx.cmd"
13+
fi
14+
. "$(dirname "$0")/_/husky.sh"
415

516
if [ "$HUSKY_PRE_COMMIT" != 0 ]; then
6-
npx lint-staged
7-
# ativate user inputs
8-
exec < /dev/tty
17+
$npx lint-staged
918

1019
consoleregexp='console\.|debugger'
11-
12-
# CHECK
1320
if test $(git diff --cached | grep -nE $consoleregexp | wc -l) != 0
14-
then
15-
exec git diff --cached --color | grep -nE $consoleregexp -A 2 -B 2
16-
read -p "There are some occurrences of forbidden patterns at your modification. Are you sure want to continue? (y/n)" yn
17-
echo $yn | grep ^[Yy]$
18-
if [ $? -eq 0 ]
19-
then
20-
exit 0; #THE USER WANTS TO CONTINUE
21+
then
22+
if [ "$HUSKY_INPUT_ENV" != 0 ] && [ "$OSTYPE" != "msys" ]; then
23+
# activate user inputs
24+
exec < /dev/tty
25+
exec git diff --cached --color | grep -nE $consoleregexp -A 2 -B 2
26+
read -p "There are some occurrences of forbidden patterns at your modification. Are you sure want to continue? (y/n)" yn
27+
echo $yn | grep ^[Yy]$
28+
if [ $? -eq 0 ]
29+
then
30+
exit 0; #THE USER WANTS TO CONTINUE
31+
else
32+
exit 1; # THE USER DONT WANT TO CONTINUE SO ROLLBACK
33+
fi
2134
else
22-
exit 1; # THE USER DONT WANT TO CONTINUE SO ROLLBACK
35+
echo -e "\033[1;31mWARNING: Some occurences of forbidden patterns were detected \033[0m"
36+
exec git diff --cached --color | grep -nE $consoleregexp -A 2 -B 2
2337
fi
2438
fi
2539
fi

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.eslintrc.json
22
.prettierignore
33

4+
.husky/
45
.github/
56
.idea/
67
.vscode/

0 commit comments

Comments
 (0)