我想在提交到 Mercurial 或 Git 存储库之前运行 JSLint。
我希望这是一个自动设置的步骤,而不是依赖开发人员(主要是我)记住事先运行 JSLint。我通常在开发时运行 JSLint,但希望在 JS 文件上指定一个契约,以便在提交到存储库之前传递 JSLint。
对于水银来说,这一页 http://hgbook.red-bean.com/read/handling-repository-events-with-hooks.html#sec:hook:precommit阐明了预提交语法,但似乎唯一可用的变量是提交中涉及的parent1和parent2变更集ID。我真正想要的是与提交相关的文件名列表,以便我可以选择 .js 文件并对其运行 jslint。
GIT 的类似问题 http://book.git-scm.com/5_git_hooks.html,作为预提交脚本的一部分可用的默认信息似乎有限。
可能有效的方法是调用 hg status/git status 作为预提交脚本的一部分,解析该输出以查找 JS 文件,然后以这种方式完成工作。我希望能有更简单的事情,但我不确定调用状态作为预提交挂钩的一部分是否反映了正确的信息。例如,在 Git 中,如果尚未添加更改文件,但 git commit 使用 -a,这些文件是否会作为提交集的一部分显示在 git status 输出的正确部分中?
Update:我得到了一些东西,它在这里可见:http://github.com/jrburke/dvcs_jslint/ http://github.com/jrburke/dvcs_jslint/
以下是 @Bitbieger 的 Git 解决方案的变体,适用于Node.js http://nodejs.org和本地副本节点jslint https://github.com/reid/node-jslint(即你需要npm install jslint
在您的根存储库目录中)。
另外脚本:
- 对所有 .html 和 .json 文件以及 .js 运行 jslint
- 仅对已添加、复制或修改的文件运行 jslint。这可以防止 jslint 在已重命名或删除的文件上出错。
- 复制任何 jslint 错误以供用户查看
- 使用
--indent 4 --white true
jslint 选项确保源代码一致性
要使其正常工作,请将以下内容复制到.git/hooks/pre-commit
并且不要忘记chmod +x .git/hooks/pre-commit
# Pre-commit hook passing files through jslint
#
# This ensures that all js, html and json files are valid and conform
# to expectations.
ROOT_DIR=$(git rev-parse --show-toplevel)
JSLINT="${ROOT_DIR}/node_modules/.bin/jslint --indent 4 --white true"
for file in $(git diff-index --name-only --diff-filter=ACM --cached HEAD -- | grep -P '\.((js)|(html)|(json))$'); do
if node $JSLINT $file 2>&1 | grep 'No errors found' ; then
echo "jslint passed ${file}"
exit 0
else
node $JSLINT $file
exit 1
fi
done
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)