添加git钩子以监视文件';变化
我正在尝试创建一个git钩子,用于评估package.json的更改,并自动运行shrinkwrapper并将更改的文件提交到存储库。我尝试了很多,但都没有找到有效的解决方案。有人知道怎么做吗 此外,每当我添加一个npm模块,并尝试运行npm shrinkwrap时,我都会遇到这个错误添加git钩子以监视文件';变化,git,githooks,npm-shrinkwrap,Git,Githooks,Npm Shrinkwrap,我正在尝试创建一个git钩子,用于评估package.json的更改,并自动运行shrinkwrapper并将更改的文件提交到存储库。我尝试了很多,但都没有找到有效的解决方案。有人知道怎么做吗 此外,每当我添加一个npm模块,并尝试运行npm shrinkwrap时,我都会遇到这个错误 npm ERR! Darwin 16.7.0 npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "shrinkwrap" npm ERR! nod
npm ERR! Darwin 16.7.0
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "shrinkwrap"
npm ERR! node v6.11.3
npm ERR! npm v3.10.10
npm ERR! Problems were encountered
npm ERR! Please correct and try again.
npm ERR! missing: request-promise@^4.2.2, required by Redux-React@1.0.0
npm ERR!
npm ERR! If you need help, you may report this error at:
npm ERR! <https://github.com/npm/npm/issues>
npm ERR! Please include the following file with any support request:
npm ERR! /Users/maxDoung/Desktop/Redux-React/npm-debug.log
也许这就是你想要的,或者至少它会为你指明正确的方向。我已经在Linux上测试过了 请遵循以下步骤(根据您的具体需要进行调整): 1-将以下文件放在.git/hooks/pre-commit:
#!/bin/bash
is_package_json_at_git_index() {
echo "> Getting list of files at git index."
# Get the list of files added to the git index and verify that package.json is there.
git diff --cached --name-only | grep -x package.json
}
update_shrinkwrap_and_at_to_git_index() {
echo "> Updating npm-shrinkwrap.json and adding to this commit."
# Run shrinkwrap in the case that it was never ran
npm shrinkwrap
# Install dependencies that could be added to package.json and update npm-shrinkwrap.json
npm install
# Add the updated- npm-shrinkwrap.json to the git index so we include it in the current commit
git add npm-shrinkwrap.json
}
if is_package_json_at_git_index; then
update_shrinkwrap_and_at_to_git_index
fi
2-将运行权限添加到预提交
chmod+x.git/hooks/pre-commit
现在,如果您执行以下操作:
git add package.json
注意:您可以使用其他挂钩,请查看它们的详细说明。能否显示存储库的文件夹结构,主要是与
npm shrinkwrap
相关的部分?您希望在推送过程中对每个提交进行评估还是仅对每个推送进行评估?感谢Gabriel的回答。如果手动更新package.json,则不起作用;例如,如果将“请求承诺”:“^4.2.2”添加到package.json,然后尝试提交。您将收到一个错误,说明缺少请求承诺。如果您使用npm安装而不是手动安装,那么您的解决方案可以正常工作。有没有允许手动更新package.json的解决方案?@MaxDoung,我已经在Ubuntu 16.04上使用npm 5.3.0进行了测试,可以很好地执行以下操作:1。我编辑了package.json并在依赖项部分添加了“请求承诺”:“^4.2.2”。2.git add package.json,3。git commit-m“添加了请求承诺依赖项”。在此之后,npm-shrinkwrap.json将所有更改连同更新的package.json一起提交到存储库。@MaxDoung您能尝试用npm 5.3.0运行我的示例吗?我现在累了,我不断地得到npm错误!遇到问题npm ERR!请更正并重试。npm错误!缺少:请求承诺@^4.2.2,Redux要求-React@1.0.0. 我不确定问题出在哪里。我使用的是npm 3.10.10,应该是fine@MaxDoung当然,您遇到的问题是依赖项解析,它实际上可能是npm shrinkwrap的一个bug,您必须进一步调试它,但这与git挂钩无关。
#!/bin/bash
is_package_json_at_git_index() {
echo "> Getting list of files at git index."
# Get the list of files added to the git index and verify that package.json is there.
git diff --cached --name-only | grep -x package.json
}
update_shrinkwrap_and_at_to_git_index() {
echo "> Updating npm-shrinkwrap.json and adding to this commit."
# Run shrinkwrap in the case that it was never ran
npm shrinkwrap
# Install dependencies that could be added to package.json and update npm-shrinkwrap.json
npm install
# Add the updated- npm-shrinkwrap.json to the git index so we include it in the current commit
git add npm-shrinkwrap.json
}
if is_package_json_at_git_index; then
update_shrinkwrap_and_at_to_git_index
fi