release.sh 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env bash
  2. # ensure git is latest clean branch
  3. # require npm user
  4. # bump package version
  5. # commit
  6. # create tag
  7. # push commit & tag
  8. # publish
  9. usage() {
  10. echo ""
  11. echo " Usage: bash $0 <major|minor|patch>"
  12. }
  13. print() {
  14. echo "NPM RELEASE: $1"
  15. }
  16. run() {
  17. local version=$1
  18. # ensure git is ready, fetch before making comparisons
  19. git fetch
  20. local local_sha=$(git rev-parse @)
  21. local remote_sha=$(git rev-parse @{u})
  22. local base_sha=$(git merge-base @ @{u})
  23. if [[ -n $(git status --porcelain) ]]; then
  24. print "Commit or stash you changes before releasing"
  25. exit 1
  26. else
  27. print "Working directory is clean"
  28. fi
  29. if [ $local_sha = $remote_sha ]; then
  30. print "Local branch is up-to-date."
  31. elif [ $local_sha = $base_sha ]; then
  32. print "You need to pull changes before you can release."
  33. exit 1
  34. elif [ $remote_sha = $base_sha ]; then
  35. print "You need to push changes before you can release."
  36. exit 1
  37. else
  38. print "Your branch has diverged from the remote, you cannot release."
  39. exit 1
  40. fi
  41. # ensure npm is ready
  42. local npm_user=$(npm whoami)
  43. local is_collaborator=$(npm access ls-collaborators | grep ".*$npm_user.*:.*write.*")
  44. local is_owner=$(npm owner ls | grep ".*$npm_user <.*")
  45. if ! [[ "$npm_user" ]]; then
  46. print "You must be logged in to NPM to publish, run \"npm login\" first."
  47. exit 1
  48. fi
  49. if [[ -z "$is_collaborator" ]] && [[ -z "$is_owner" ]]; then
  50. print "$npm_user is not an NPM owner or collaborator. Request access from:"
  51. npm owner ls
  52. exit 1
  53. fi
  54. # all checks out, publish
  55. print "Publishing new $version version as $npm_user."
  56. print "...npm version $version"
  57. npm version ${version}
  58. print "...git push"
  59. git push
  60. print "...git push --follow-tags"
  61. git push --follow-tags
  62. print "...npm publish"
  63. npm publish
  64. }
  65. case $1 in
  66. "major" | "minor" | "patch")
  67. run $1
  68. ;;
  69. *)
  70. usage
  71. exit 1
  72. ;;
  73. esac