setup pre-commit hook jshint

后端 未结 4 2062
醉话见心
醉话见心 2020-12-12 18:09

I recently started a project on github. I\'ve managed to setup automatic testing after each commit using Travis. But now I would like to setup a pre-commit hook with jshint

4条回答
  •  不思量自难忘°
    2020-12-12 19:02

    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 \" to avoid call pre-commit hook and JSHint check)"
        echo ""
        exit 1
    fi
    

提交回复
热议问题