forked from filecoin-project/filecoin-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-commit-lint.sh
executable file
·93 lines (83 loc) · 2.37 KB
/
pre-commit-lint.sh
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
89
90
91
92
93
#!/bin/bash
DOCLINK="https://github.com/filecoin-project/filecoin-docs/blob/main/README.md#pre-commit-linters"
bold=$(tput bold)
normal=$(tput sgr0)
regularBar="----------------------------------------------------"
boldBar="===================================================="
errorBar="!**************************************************!"
fileList=$(git diff --diff-filter=d --cached --name-only)
mdFileList=$(echo "$fileList" | grep -E '\.(md)$')
echo " "
echo $boldBar
echo "${bold}PRE-COMMIT CHECK${normal}"
echo "We're checking all the markdown files changed in"
echo "this commit for any broken links, spelling mistakes,"
echo "or formatting errors."
echo "For details, see"
echo "$DOCLINK."
if [ ${#mdFileList} -gt 0 ]; then
errors=0
echo " "
echo "The following files were changed:"
for file in $mdFileList; do
echo " - $file"
done
echo $boldBar
echo " "
echo $regularBar
echo "${bold}Spell check${normal}"
echo $regularBar
npx mdspell -r -a -n --en-us $mdFileList "$@"
spellPassed=$?
echo " "
echo $regularBar
echo "${bold}Link check${normal}"
echo $regularBar
npx markdown-link-check --config .mdlinkcheck-config.json -q -p $mdFileList "$@"
linksPassed=$?
echo " "
echo $regularBar
echo "${bold}Formatting check${normal}"
echo $regularBar
npx markdownlint-cli2 $mdFileList "$@"
formatPassed=$?
errorDescr=""
if [ $linksPassed -ne 0 ]; then
errorDescr+="\n- Broken links."
errors=1
fi
if [ $spellPassed -ne 0 ]; then
errorDescr+="\n- Spelling errors."
errors=1
fi
if [ $formatPassed -ne 0 ]; then
errorDescr+="\n- Markdown formatting errors."
errors=1
fi
if [ "$errors" -eq 1 ]; then
echo " "
echo $errorBar
echo "${bold}ERRORS FOUND${normal}"
echo "There are some problems with your commit:"
echo "$errorDescr"
echo " "
echo "For details on how to fix these errors, see"
echo "$DOCLINK."
echo $errorBar
echo " "
exit 1
else
echo " "
echo $regularBar
echo "${bold}No errors were found!${normal}"
exit 0
fi
else
echo " "
echo $regularBar
echo "No markdown files were changed in this commit."
echo "Skipping checks..."
echo $regularBar
echo " "
exit 0
fi