-
Notifications
You must be signed in to change notification settings - Fork 546
/
jscs-pre-commit
69 lines (60 loc) · 2.14 KB
/
jscs-pre-commit
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
#!/bin/bash
# Runs JSCS style checker using definitions in `.jscs.json`
# See https://github.com/mdevils/node-jscs for explanation of tests
#
# to style check your JS
# 1) ln -s ../../jscs-pre-commit .git/hooks/pre-commit
# 2) npm install jscs -g
# Adapted from an original script by Timothée Boucher
# http://tech.adroll.com/blog/web/2014/03/05/adding-jscs-to-your-commit-hook.html
export JSCS_PATH=`which jscs`
# Extract staged files to a temp directory
TMPDIR=
TMPFILE=
if [ "$IS_LINUX" == "true" ]; then
TMPFILE=`mktemp jscs_tmp_XXXXXX`
TMPDIR=`mktemp -d jscs_tmp_XXXXXX`
else
TMPFILE=`mktemp -t tmp/jscs_tmp_XXXXXX`
TMPDIR=`mktemp -t tmp/jscs_tmp -d`
fi
git diff --cached --name-only --diff-filter=ACMR | xargs git checkout-index --prefix=$TMPDIR/ --
# Check JavaScript code style
RUN_JSCS=1
TOTAL_ERRORS=0
PLURAL_SUFFIX=
JSFILES=$(git diff-index --name-status --cached HEAD | grep -v ^D | egrep '.js$' | cut -c3-)
if [ -z "$JSFILES" ]; then
# No JavaScript file changed for this commit
RUN_JSCS=0
elif [ -z "$JSCS_PATH" ]; then
echo -e "\033[0;31mIf you \033[1;34mnpm install jscs -g\033[0;31m I'll check your JS style\033[0;0m"
RUN_JSCS=0
fi
# Run JSCS
if [ $RUN_JSCS -ne 0 ]; then
echo -e "\033[0;34mChecking JS style...\033[0;0m"
OUT=`$JSCS_PATH -r text $TMPDIR`
CODE=$?
# Erase last output line
# echo -ne '\r\033[K'
if [ $CODE -ne 0 ]; then
# Remove temp dir from output and color the filename
OUT=`echo "$OUT" | sed "s,$TMPDIR/\([^ ]*\),\`echo -e \"\033[0;34m\1\033[0m\"\`,"`
TOTAL_ERRORS=`echo -e "$OUT" | sed 's/[^0-9]//g' | tail -1`
# echo output minus last line
echo "$OUT" | sed '$ d'
if [ $TOTAL_ERRORS -ne 1 ]; then
PLURAL_SUFFIX="s"
fi
echo -e "\033[0;31m$TOTAL_ERRORS code style error$PLURAL_SUFFIX found.\033[0;0m"
echo "Please fix and re-commit."
echo -e "Need to commit right away? commit with the \033[0;34m-n\033[0m flag"
rm -Rf $TMPDIR $TMPFILE
exit $CODE
else
echo -e "\033[0;32mNo JS code style errors found.\033[0;0m"
fi
fi
# Clean up
rm -Rf $TMPDIR $TMPFILE