Skip to content

Instantly share code, notes, and snippets.

@andris-silis
Forked from 8bitDesigner/1.md
Last active January 2, 2016 02:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andris-silis/8238597 to your computer and use it in GitHub Desktop.
Save andris-silis/8238597 to your computer and use it in GitHub Desktop.
Update git project's bower and npm packages on git pull, merge and checkout

Make bower and npm package install easy

This repo has some handy handy hooks to run bower update or npm install whenever you:

  • git checkout a new branch with a different package.json or bower.json
  • git pull a change to package.json or bower.json
#! /bin/bash
# post-checkout hook - looks for changes to bower.json or package.json,
# when you change branches, and if found, reinstalls the given packages every time you `git merge` or `git pull`
#
# To install:
# copy to your project's .git/hooks folder
# chmod +x .git/hooks/post-checkout
#
# Exit early if this was only a file checkout, not a branch change ($3 == 1)
[[ $3 == 0 ]] && exit 0
oldRef=$1
newRef=$2
function changed {
git diff --name-only $oldRef $newRef | grep "^$1" > /dev/null 2>&1
}
if changed 'bower.json'; then
echo "bower.json changed, installing"
bower update
fi
if changed 'package.json'; then
echo "Package.json changed, installing"
npm install
fi
#!/bin/sh
# post-checkout hook - looks for changes to bower.json or package.json, and if
# found, reinstalls the given packages every time you `git merge` or `git pull`
#
# To install:
# copy to your project's .git/hooks folder
# chmod +x .git/hooks/post-merge
#
function changed {
git diff --name-only HEAD@{2} HEAD | grep "^$1" > /dev/null 2>&1
}
if changed 'bower.json'; then
echo "bower.json changed, installing"
bower update
fi
if changed 'package.json'; then
echo "Package.json changed, installing"
npm install
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment