#!/bin/bash
REPO_NAME=$(basename `git rev-parse --show-toplevel`)

if [[ $(pwd) =~ .+$REPO_NAME ]]; then
  # cd to application route directory this allows the commiting/pushing from any part of the application
  cd ${BASH_REMATCH}

  DEFAULT=$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')

  # can the amount of lines changed
  amount_of_additions=$(git diff origin/$DEFAULT -- package-lock.json | grep "+  " | wc -l)

  # assume a single addition is a version bump
  if [ $amount_of_additions \> 1 ]; then
    echo "Your package-lock.json differs to origin/$DEFAULT with" \
      $amount_of_additions \
      "additions"
    echo "Now running an 'npm install'"
    npm install
  fi
fi
