1

我正在尝试创建一个 git 挂钩来评估 package.json 的更改并自动运行收缩包装器并将更改的文件提交到存储库。我已经尝试了很多,但无法获得有效的解决方案。有谁知道怎么做?

此外,每当我添加一个 npm 模块并尝试运行 npm shrinkwrap 时,我都会收到此错误

 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

如果我手动更新 package.json 或使用 npm 安装模块,由于某种原因,npm shrink 不起作用。另外,我不确定节点或 npm 版本是否重要。我正在使用 npm 3.10.10 和节点 v6.11.3

这是我的依赖

  "dependencies": {
    "apn": "^1.7.7",
    "bluebird": "^3.4.1",
    "body-parser": "^1.15.0",
    "busboy": "^0.2.13",
    "connect-redis": "^3.1.0",
    "cookie-parser": "^1.4.1",
    "cors": "^2.7.1",
    "debug": "^2.2.0",
    "destroy": "^1.0.4",
    "express": "^4.13.4",
    "git-rev": "^0.2.1",
    "glob": "^7.0.3",
    "helmet": "^1.3.0",
    "hiredis": "^0.4.1",
    "humps": "^1.1.0",
    "lodash": "^4.14.1",
    "methods": "^1.1.2",
    "mysql": "^2.11.1",
    "node-gcm": "^0.14.4",
    "node-inspector": "^0.12.8",
    "node-resource": "^1.2.0",
    "on-finished": "^2.3.0",
    "on-headers": "^1.0.1",
    "parseurl": "^1.3.1",
    "path-to-regexp": "^1.5.3",
    "redis": "^2.6.0-0",
    "request": "^2.69.0",
    "sequelize": "^3.23.6",
    "serve-favicon": "^2.3.0",
    "socket.io": "^1.4.6",
    "through2": "^2.0.1"
  },
4

1 回答 1

2

可能这就是您想要的,或者至少它会为您指明正确的方向。我已经在 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

现在,如果您执行以下操作:

  1. 您手动更新 package.json 可能会更新依赖项的版本。
  2. 你跑git add package.json
  3. 你跑git commit -m "Your commit message"

将发生以下情况:

  1. npm-shrinkwrap.json 将被更新
  2. npm-shrinkwrap.json 将自动添加到提交中

注意:您可以使用其他钩子,请在此处查看它们的详细说明。

于 2017-09-23T02:28:31.037 回答