1

I am trying to create a git hook that evaluate the package.json for changes and auto-runs the shrinkwrapper and commits the changed file to a repository. I have tried a lot, but could not get a working solution. Does anyone know how do that ?

Also, whenever I add a npm module, and try to run npm shrinkwrap, I get this error

 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

for some reason npm shrink does not work if I update package.json manually or install a module using npm. Also, I am not sure if the node or npm version matters. I am using npm 3.10.10, and node v6.11.3

Here is my dependency

  "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"
  },
Max Doung
  • 221
  • 3
  • 14

1 Answers1

2

Probably this is what you want or at least it will point you to the right direction. I've tested this on Linux.

Please follow these steps (adapt them as your specific needs):

1 - Put the following file at .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 - Add run permissions to the pre-commit chmod +x .git/hooks/pre-commit

Now, if you do the following:

  1. You update manually package.json maybe updating a version of a dependency.
  2. You run git add package.json
  3. You run git commit -m "Your commit message"

The following will happen:

  1. npm-shrinkwrap.json will be updated
  2. npm-shrinkwrap.json will be automatically added to the commit

NOTE: You could use other hooks, please take a look at a detailed explanation of them here.

Gabriel Avellaneda
  • 742
  • 1
  • 7
  • 14
  • Thanks Gabriel for your answer. It does not work if you update the package.json manually; for example if you add "request-promise": "^4.2.2" to package.json, then try to commit. You will get an error saying that request-promise is missing. Your solution works fine if you use npm install but not manually. Is there a solution that allows updating package.json manually ? – Max Doung Sep 24 '17 at 11:25
  • @MaxDoung, I've tested with npm 5.3.0 on Ubuntu 16.04 and works fine doing the following: 1. I edited package.json and added "request-promise": "^4.2.2" to the dependencies section. 2. git add package.json, 3. git commit -m "Added request-promise dependency". After this, the npm-shrinkwrap.json has all the changes commited to the repository along with the updated package.json. – Gabriel Avellaneda Sep 24 '17 at 13:45
  • @MaxDoung Can you try to run my example with npm 5.3.0? – Gabriel Avellaneda Sep 24 '17 at 13:53
  • I tired now and I keep getting 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. I am not sure where the problem is. I am using npm 3.10.10 which should be fine – Max Doung Sep 24 '17 at 13:56
  • @MaxDoung, Sure, the problem that you are having is with the dependencies resolution, it could actually be a bug with npm shrinkwrap, you have to debug it further but this is not related with git hooks. – Gabriel Avellaneda Sep 24 '17 at 14:20