Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save LinusBorg/85136396ddcd1f4f4cfda20d8624991b to your computer and use it in GitHub Desktop.
Save LinusBorg/85136396ddcd1f4f4cfda20d8624991b to your computer and use it in GitHub Desktop.
git hook to run `npm install` after git pull, merge, rebase or checkout if package.json was changed
# The name for this gist (starts with exclamation mark, because the name of the gist is the name of the first file in ASCIIbetical order)

Git hooks for NPM Projects

1. Copy theses files into your hooks folder

$ mkdir hooks && curl -L -o hooks/post-checkout -O https://gist.github.com/taurus227/28960de89e6c43bb3d492125368f1224/raw/post-checkout && curl -L -o hooks/post-merge -O https://gist.github.com/taurus227/28960de89e6c43bb3d492125368f1224/raw/post-merge && curl -L -o hooks/post-rewrite -O https://gist.github.com/taurus227/28960de89e6c43bb3d492125368f1224/raw/post-rewrite

2. Add an installation script into your package.json

...
  "scripts": {                                                       
    "install": "cp ./hooks/* .git/hooks/ && chmod -R a+x .git/hooks/"
  },                                                                 
...

3. Commit and push to remote repository

At this point it will work for the whole team as soon as they all run npm install

Thanks to these guys:

#!/usr/bin/env bash
# from https://gist.github.com/taurus227/28960de89e6c43bb3d492125368f1224
changed_files="$(git diff-tree -r --name-only --no-commit-id $1 $2)"
check_run() {
echo "$changed_files" | grep --quiet "$1" && echo "$0: Running $2..." && eval "$2"
}
check_run package.json "npm prune && npm install"
#!/usr/bin/env bash
# from https://gist.github.com/taurus227/28960de89e6c43bb3d492125368f1224
changed_files="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)"
check_run() {
echo "$changed_files" | grep --quiet "$1" && echo "$0: Running $2..." && eval "$2"
}
check_run package.json "npm prune && npm install"
#!/bin/bash
# from https://gist.github.com/taurus227/28960de89e6c43bb3d492125368f1224
if [[ "$1" == "rebase" ]]
then
$(dirname "$0")/post-merge
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment