Skip to content

Commit 4ce42d5

Browse files
add pre commit hook
1 parent 90d76ae commit 4ce42d5

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

.add-pre-commit-hook

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
3+
cp ./.pre-commit ./.git/hooks/pre-commit
4+
exit 0

.pre-commit

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/bin/zsh
2+
3+
# function lintit () {
4+
# OUTPUT=$(git diff --name-only | grep -E '(.js|.jsx)$')
5+
# a=("${(f)OUTPUT}")
6+
# e=$(npm run eslint $a)
7+
# echo $e
8+
# if [[ "$e" != *"0 errors"* ] || [ "$e" != *"0 errors"* ]]; then
9+
# echo "ERROR: Check eslint hints."
10+
# exit 1 # reject
11+
# fi
12+
# }
13+
# lintit
14+
# exit 0
15+
16+
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep ".jsx\{0,1\}$")
17+
18+
if [[ "$STAGED_FILES" = "" ]]; then
19+
exit 0
20+
fi
21+
22+
PASS=true
23+
24+
echo "\nValidating Javascript:\n"
25+
26+
27+
for FILE in $STAGED_FILES
28+
do
29+
npm run eslint "$FILE"
30+
31+
if [[ "$?" == 0 ]]; then
32+
echo "\t\033[32mESLint Passed: $FILE\033[0m"
33+
else
34+
echo "\t\033[41mESLint Failed: $FILE\033[0m"
35+
PASS=false
36+
fi
37+
done
38+
39+
echo "\nJavascript validation completed!\n"
40+
41+
if ! $PASS; then
42+
echo "\033[41mCOMMIT FAILED:\033[0m Your commit contains files that should pass ESLint but do not. Please fix the ESLint errors and try again.\n"
43+
exit 1
44+
else
45+
echo "\033[42mCOMMIT SUCCEEDED\033[0m\n"
46+
fi
47+
48+
exit $?

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "5.2.0",
44
"private": true,
55
"scripts": {
6-
"postinstall": "remotedev-debugger",
6+
"postinstall": "remotedev-debugger && sh .add-pre-commit-hook",
77
"start": "node_modules/react-native/packager/packager.sh",
88
"eslint": "eslint"
99
},

0 commit comments

Comments
 (0)