-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·88 lines (75 loc) · 2.39 KB
/
pre-commit
File metadata and controls
executable file
·88 lines (75 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/sh
#
# Pre-Commit hook script for Drupal 8.x to check your code against ESLint and PHPCS code sniffer.
#
# Requirements:
# Drupal 8.x
# NodeJS
# ESLint
# PHP Code Sniffer (phpcs) with Drupal Coder (https://www.drupal.org/project/coder)
#
# Usage:
# In your terminal: $ cd /path/to/your/project/.git/hooks
# Download the pre-commit script: $ curl -O https://raw.githubusercontent.com/mecmartini/drupal-pre-commit-hook/master/pre-commit
# Make sure script is executable : $ chmod +x pre-commit
# Esit the script with your favorite editor and setup the bin tools on the #Setup section
# Ready to commit!
#
# Setup
commitfailed=0
NODE_BIN="/usr/local/bin/node"
ESLINT_BIN="./web/node_modules/.bin/eslint"
PHPCS_BIN="./vendor/bin/phpcs"
PHPCBF_BIN="./vendor/bin/phpcbf"
#### ESLint
# Get js files (added to commit) to process with ESLint
jsfiles=$(git diff --cached --name-only --diff-filter=ACM HEAD | grep -E '\.js$')
if [ "$jsfiles" != "" ]
then
jsfailed=0
echo "Running ESLint..."
$NODE_BIN $ESLINT_BIN $jsfiles >/dev/null
if [ $? != 0 ]
then
for jsfile in ${jsfiles}
do
$NODE_BIN $ESLINT_BIN --fix $jsfile >/dev/null
git add $jsfile
$NODE_BIN $ESLINT_BIN $jsfile >/dev/null
if [ $? != 0 ] ; then
jsfailed=1
fi
done;
fi
if [ $jsfailed != 0 ]
then
echo "ESLint failed, errors found not fixable automatically, git commit denied!"
commitfailed=$jsfailed
fi
fi
#### End of ESLint
#### Code Sniffer
drupalfiles=$(git diff --cached --name-only --diff-filter=ACM HEAD | grep -E '\.php$|\.module$|\.inc$|\.install$|\.test$|\.profile$|\.theme$|\.css$|\.info$|\.txt$|\.md$|\.yml$')
if [ "$drupalfiles" != "" ]
then
drupalfailed=0
echo "Running Code Sniffer..."
$PHPCS_BIN --standard=Drupal --encoding=utf-8 -n -p $drupalfiles >/dev/null
if [ $? != 0 ]
then
$PHPCBF_BIN --standard=Drupal --encoding=utf-8 -n -p $drupalfiles >/dev/null
git add $drupalfiles
$PHPCS_BIN --standard=Drupal --encoding=utf-8 -n -p $drupalfiles >/dev/null
if [ $? != 0 ]
then
drupalfailed=1
fi
fi
if [ $drupalfailed != 0 ]
then
echo "PHPCS failed, errors found not fixable automatically, git commit denied!"
commitfailed=$drupalfailed
fi
fi
#### End of Code Sniffer
exit $commitfailed