-
Notifications
You must be signed in to change notification settings - Fork 3
/
pre-commit.example
47 lines (38 loc) · 1019 Bytes
/
pre-commit.example
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
#!/bin/sh
#
# The hook should exit with non-zero status after issuing an appropriate message if it wants to stop the commit.
# Installation:
# - copy this file to ./.git/hooks
# - rename it by removing the extension
# - make executable by $chmod +x pre-commit
# Steps:
#
# * Use prettier
# * Lint (with fix)
#
# Set working directory to project root
WORKDIR=$PWD
PROJECTDIR=src
# Determine if any JS files changed
cd $WORKDIR
JSCHANGES=$(git diff --cached --name-only --diff-filter=ACM -- "$PROJECTDIR/**/*.js" "$PROJECTDIR/**/*.js" "!($PROJECTROOT/node_modules)" | tr '\n' ' ')
if [ ! -z "$JSCHANGES" ]
then
# Prettify all staged .js files
cd $WORKDIR/$PROJECTDIR
npm run prettier
# Add back the modified/prettified files to staging
cd $WORKDIR
git add -A $PROJECTDIR
fi
# Enter app core directory
cd $WORKDIR/$PROJECTDIR
# Lint and abort if fails
npm run lint:fix
RESULT=$?
if [ "$RESULT" -ne 0 ]
then
echo Linting failed. Please resolve. Not committing.
exit 1
fi
exit 0