Skip to content

Instantly share code, notes, and snippets.

@f3r
Last active April 8, 2023 10:01
Show Gist options
  • Save f3r/13df273cf2885445636e to your computer and use it in GitHub Desktop.
Save f3r/13df273cf2885445636e to your computer and use it in GitHub Desktop.
Git pre-commit to validate HTML, CSS and JS
#!/bin/sh
#
# Run validations before commit
#
# - HTML: https://github.com/htacg/tidy-html5
# - CSS: https://github.com/CSSLint/csslint
# - JS: http://jshint.com/docs/cli/
#
# Author: [email protected]
# This will check only files added to git for this commit
# html_files=$(git diff --cached --name-only --diff-filter=ACMR -- *.html **/*.html)
html_files=$(ls -R *.html)
html_pass=true
echo "\033[32m####################################\033[0m"
echo "\033[32m# VALIDATING HTML #\033[0m"
echo "\033[32m####################################\033[0m"
if [ "$html_files" != "" ]; then
for file in ${html_files}; do
result=$(tidy-html ${file} -quiet --show-warnings no &>/dev/null)
# result=$(tidy-html ${file} -quiet -errors --show-warnings no &>/dev/null)
RETVAL=$?
if [ $RETVAL -ne 0 ]; then
echo "\033[31mtidy-html Failed: ${file}\033[0m"
echo $(tidy-html ${file} -quiet -errors --show-warnings no | grep "errors were found!" | head -1)
html_pass=false
else
echo "\033[32mtidy-html Passed: ${file}\033[0m"
fi
done
fi
# # css_files=$(git diff --cached --name-only --diff-filter=ACMR -- *.html **/*.css)
css_files=$(ls -R **/*.css | grep -v min.css)
css_pass=true
echo "\033[32m####################################\033[0m"
echo "\033[32m# VALIDATING CSS #\033[0m"
echo "\033[32m####################################\033[0m"
if [ "$css_files" != "" ]; then
for file in ${css_files}; do
# result=$(csslint ${file} &>/dev/null)
result=$(csslint ${file} &>/dev/null)
RETVAL=$?
if [ $RETVAL -ne 0 ]; then
echo "\033[31mCSSLint Failed: ${file}\033[0m"
csslint --format=compact ${file} | sed -E 's/^[^:]*://'
css_pass=false
else
echo "\033[32mCSSHint Passed: ${file}\033[0m"
fi
done
fi
# js_files=$(git diff --cached --name-only --diff-filter=ACMR -- *.js **/*.js)
js_files=$(ls -R **/*.js | grep -v min.js)
js_pass=true
echo ""
echo "\033[32m####################################\033[0m"
echo "\033[32m# VALIDATING JAVASCRIPT #\033[0m"
echo "\033[32m####################################\033[0m"
if [ "$js_files" != "" ]; then
for file in ${js_files}; do
result=$(jshint ${file} | sed -E 's/^[^:]*://')
if [ "$result" != "" ]; then
echo "\033[31mJSHint Failed: ${file}\033[0m"
echo "$result"
js_pass=false
else
echo "\033[32mJSHint Passed: ${file}\033[0m"
fi
done
fi
if $html_pass && $css_pass && $js_pass; then
exit 0
else
echo ""
echo "\033[31m####################################\033[0m"
echo "\033[31m# COMMIT FAILED!!!!! #\033[0m"
echo "\033[31m####################################\033[0m"
echo ""
echo "\033[31m Looks like some of your HTML/CSS/JS files are invalid.\033[0m"
echo "\033[31m Please fix errors and try committing again.\033[0m"
echo ""
echo "\033[31m Happy coding!\033[0m"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment