-->

设置pre-commit钩子jshint(setup pre-commit hook jshint)

2019-08-18 05:17发布

我最近开始在GitHub上项目 。 我已经成功地设置自动测试之后,每次提交使用特拉维斯。 但现在我想建立与jshint一个pre-commit钩子了。 所以,如果jshint报告错误,提交应该会失败。 但是,这是可能的,如果是这样,如何做到这一点?

Answer 1:

但是,这是可能的...

是! 这个有可能。 我最近写了一篇关于它 。 请注意,这不是具体到GitHub上,只是Git的一般-因为它是一个pre-commit钩子,它运行之前的任何数据发送到GitHub上。

在你的仓库的/.git/hooks目录中的任何适当命名的可执行文件将作为挂钩运行。 有可能会在那里一堆例子钩已默认。 这里有一个简单的shell脚本 ,我作为一个JSLint的pre-commit钩子使用(你可以修改它很容易与JSHint工作代替):

#!/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

你可以简单地说,在名为使用Git的挂钩目录提交前的一个可执行文件,它将运行每次提交之前。



Answer 2:

有做预提交您的Node.js的工作流程检查(如JSHint)的一个简单的方法

安装jshint从NPM:

npm install jshint

接下来在项目中创建一个.jshintrc文件,如果你不已经有一个。 例如: https://github.com/nelsonic/learn-jshint/blob/master/.jshintrc

现在安装预提交模块(并将其保存为一个开发依赖):

npm install pre-commit --save-dev

接下来,您将需要定义将为JSHint在您的package.json运行的任务(脚本)

例如:

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

那么你注册,你就需要运行脚本预提交(也的package.json),例如:

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

这可以让你拥有的不仅仅是一张支票更在你提交前的工作流程。 (我们有检查,以确保团队成员代码JSHint,代码样式符合性和测试覆盖率为100%)

:对于更详细的教程,你可以与你的团队看到共享https://github.com/nelsonic/learn-pre-commit



Answer 3:

某些更改@詹姆斯阿勒代斯脚本来满足JSHint 。 感谢您的原代码。

#!/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


Answer 4:

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


文章来源: setup pre-commit hook jshint