setup pre-commit hook jshint

99封情书 提交于 2019-11-28 16:12:53
James Allardice

But is this possible...

Yes! This is possible. I recently wrote about it. Note that it's not specific to GitHub, just Git in general - as it's a pre-commit hook, it runs before any data is sent to GitHub.

Any appropriately-named executable files in the /.git/hooks directory of your repository will be run as hooks. There will likely be a bunch of example hooks in there already by default. Here's a simple shell script that I use as a JSLint pre-commit hook (you could modify it very easily to work with JSHint instead):

#!/bin/sh

files=$(git diff --cached --name-only --diff-filter=ACM | grep "\.js$")
if [ "$files" = "" ]; then 
    exit 0 
fi

pass=true

echo "\nValidating JavaScript:\n"

for file in ${files}; do
    result=$(jslint ${file} | grep "${file} is OK")
    if [ "$result" != "" ]; then
        echo "\t\033[32mJSLint Passed: ${file}\033[0m"
    else
        echo "\t\033[31mJSLint Failed: ${file}\033[0m"
        pass=false
    fi
done

echo "\nJavaScript validation complete\n"

if ! $pass; then
    echo "\033[41mCOMMIT FAILED:\033[0m Your commit contains files that should pass JSLint but do not. Please fix the JSLint errors and try again.\n"
    exit 1
else
    echo "\033[42mCOMMIT SUCCEEDED\033[0m\n"
fi

You can simply put that in an executable file named pre-commit in your Git hooks directory, and it will run before every commit.

There's an easier way of doing pre-commit checks (e.g. JSHint) in your Node.js workflow:

Install jshint from NPM:

npm install jshint

Next create a .jshintrc file in your project if you don't already have one. e.g: https://github.com/nelsonic/learn-jshint/blob/master/.jshintrc

Now install pre-commit module (and save it as a dev dependency):

npm install pre-commit --save-dev

Next you will need to define the task (script) that will be run for JSHint in your package.json

e.g:

{ "scripts": { "jshint": "jshint -c .jshintrc --exclude-path .gitignore ." } }

then you register the scripts you want to be run pre-commit (also in package.json) e.g:

"pre-commit": [ "jshint", "coverage", "etc" ]

This allows you to have more than just one check in your pre-commit workflow. (We have checks to ensure team members code complies with JSHint, Code Style and Test Coverage is 100%)

For a more detailed tutorial you can share with your team see: https://github.com/nelsonic/learn-pre-commit

igor

Some changes to @James Allardice script to accommodate JSHint. Thanks for the original code.

#!/bin/sh
#
# Run JSHint validation before commit.

files=$(git diff --cached --name-only --diff-filter=ACMR -- *.js **/*.js)
pass=true


if [ "$files" != "" ]; then
    for file in ${files}; do
        result=$(jshint ${file})

        if [ "$result" != "" ]; then
            echo "$result"
            echo "\n"
            pass=false
        fi
    done
fi


if $pass; then
    exit 0
else
    echo ""
    echo "COMMIT FAILED:"
    echo "Some JavaScript files are invalid. Please fix errors and try committing again."
    exit 1
fi

A similar script to the @igor's one with some improvements:

  • color indicators
  • no --diff-filter, grep used insead
  • help message (git style) to avoid pre-commit call

#!/bin/sh
#
# Run JSHint validation before commit.

RED='\033[0;31m'
REDBOLD='\033[1;31m'
ORANGE='\033[0;33m'
NC='\033[0m' # No Color

files=$(git diff --cached --name-only | grep .js)
pass=true
totalErrors=0

if [ "$files" != "" ]; then
    for file in ${files}; do
        result=$(jshint ${file})
        if [ "$result" != "" ]; then
            echo "${RED}$result${NC}"
            pass=false
            totalErrors=$((totalErrors+1))
        fi
        echo ""
    done
fi

if $pass; then
    exit 0
else
    echo "${ORANGE}===== ${totalErrors} JSHint Error${NC}"
    echo ""
    echo "${REDBOLD}COMMIT FAILED: Some JavaScript files are invalid. Please fix errors and try committing again.${NC}"
    echo ""
    echo "  (use -n option \"git commit -n -m <message>\" to avoid call pre-commit hook and JSHint check)"
    echo ""
    exit 1
fi
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!