Skip to content

Commit cf1d523

Browse files
authored
Set up formatting&linting in pre-commit hook (#1375) (#1378)
* Set ESLint indent rule to 2 * Replace js-beautify with prettier and use npx to run commands in format.sh * Add custom folder containing git hooks and create pre-commit hook
1 parent 5cb988e commit cf1d523

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

.eslintrc.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ parserOptions:
1111
rules:
1212
indent:
1313
- error
14-
- 4
14+
- 2
1515
linebreak-style:
1616
- error
1717
- unix

.githooks/pre-commit

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/sh
2+
set -euo pipefail
3+
4+
FILES_PRETTIER=$(git diff --cached --name-only --diff-filter=ACMR "*.js" "*.css" "*.html" "*.json" | sed 's| |\\ |g')
5+
FILES_ESLINT=$(git diff --cached --name-only --diff-filter=ACMR "*.js" | sed 's| |\\ |g')
6+
FILES_ORMOLU=$(git diff --cached --name-only --diff-filter=ACMR "*.hs" | sed 's| |\\ |g')
7+
8+
echo "Reprinting any staged JS, CSS and HTML files with Prettier ..."
9+
echo "$FILES_PRETTIER" | xargs npx prettier --write
10+
echo "Reprinting complete."
11+
echo "Linting any staged JS files with ESLint ..."
12+
echo "$FILES_ESLINT" | xargs npx eslint --fix
13+
echo "Linting complete."
14+
echo "Formatting any staged Haskell files with Ormolu ..."
15+
for f in $FILES_ORMOLU
16+
do
17+
ormolu --mode inplace $f
18+
done
19+
echo "Formatting complete."
20+
21+
# Add modified files back to staging
22+
echo "$FILES_PRETTIER" | xargs git add
23+
echo "$FILES_ESLINT" | xargs git add
24+
echo "$FILES_ORMOLU" | xargs git add
25+
26+
exit 0

format.sh

+4-6
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@ set -euo pipefail
1919
source base.sh
2020

2121
mkdir -p $BUILD/node_modules
22-
run $BUILD npm install --silent js-beautify
23-
run $BUILD npm install --silent eslint
2422

2523
function formatall_js {
26-
nodejs build/node_modules/js-beautify/js/bin/js-beautify.js -n -m 2 $(find web/js -regex .*\\.js$ -type f)
27-
nodejs build/node_modules/js-beautify/js/bin/css-beautify.js -n $(find web/css -regex .*\\.css$ -type f)
28-
nodejs build/node_modules/js-beautify/js/bin/html-beautify.js -n $(find web -regex .*\\.html$ -type f)
24+
npx prettier --write $(find web/js -regex .*\\.js$ -type f)
25+
npx prettier --write $(find web/css -regex .*\\.css$ -type f)
26+
npx prettier --write $(find web -regex .*\\.html$ -type f)
2927

30-
nodejs build/node_modules/eslint/bin/eslint.js --fix $(find web/js -regex .*\\.js$ -type f) || true
28+
npx eslint --fix $(find web/js -regex .*\\.js$ -type f) || true
3129
}
3230

3331
run . formatall_js

install.sh

+2
Original file line numberDiff line numberDiff line change
@@ -253,5 +253,7 @@ fi
253253
run . git submodule init
254254
run . git submodule update
255255

256+
run . git config core.hooksPath .githooks
257+
256258
# Go ahead and run a first build, which installs more local packages.
257259
./build.sh

0 commit comments

Comments
 (0)