setup pre-commit hook jshint

后端 未结 4 2066
醉话见心
醉话见心 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 18:54

    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
    

提交回复
热议问题