|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# ensure git is latest clean branch |
| 4 | +# require npm user |
| 5 | +# bump package version |
| 6 | +# commit |
| 7 | +# create tag |
| 8 | +# push commit & tag |
| 9 | +# publish |
| 10 | + |
| 11 | +usage() { |
| 12 | + set -e |
| 13 | + |
| 14 | + echo "" |
| 15 | + echo " Usage: bash $0 <comand>" |
| 16 | + echo "" |
| 17 | + echo " Commands:" |
| 18 | + echo " <major|minor|patch> # run checks and publish" |
| 19 | + echo " check # run checks only" |
| 20 | + echo " publish <major|minor|patch> # publish without checking" |
| 21 | +} |
| 22 | + |
| 23 | +print() { |
| 24 | + set -e |
| 25 | + |
| 26 | + echo "RELEASE: $1" |
| 27 | +} |
| 28 | + |
| 29 | +check() { |
| 30 | + set -e |
| 31 | + |
| 32 | + local failed="false" |
| 33 | + |
| 34 | + # ensure git is ready, fetch before making comparisons |
| 35 | + git fetch |
| 36 | + local local_sha=$(git rev-parse @) |
| 37 | + local remote_sha=$(git rev-parse @{u}) |
| 38 | + local base_sha=$(git merge-base @ @{u}) |
| 39 | + |
| 40 | + if [[ -n $(git status --porcelain) ]]; then |
| 41 | + print "[check: FAIL] Commit or stash you changes before releasing." |
| 42 | + exit 1 |
| 43 | + else |
| 44 | + print "[check: PASSED] Working directory is clean." |
| 45 | + fi |
| 46 | + |
| 47 | + if [ $local_sha = $remote_sha ]; then |
| 48 | + print "[check: PASSED] Local branch is up-to-date." |
| 49 | + elif [ $local_sha = $base_sha ]; then |
| 50 | + print "[check: FAIL] You need to pull changes before you can release." |
| 51 | + exit 1 |
| 52 | + elif [ $remote_sha = $base_sha ]; then |
| 53 | + print "[check: FAIL] You need to push changes before you can release." |
| 54 | + exit 1 |
| 55 | + else |
| 56 | + print "[check: FAIL] Your branch has diverged from the remote, you cannot release." |
| 57 | + exit 1 |
| 58 | + fi |
| 59 | + |
| 60 | + # ensure npm is ready |
| 61 | + local npm_user=$(npm whoami) |
| 62 | + |
| 63 | + # check perms only if package exists, otherwise it will be created |
| 64 | + if [ $(npm view > /dev/null 2>&1) ]; then |
| 65 | + local is_collaborator=$(npm access ls-collaborators | grep ".*$npm_user.*:.*write.*") |
| 66 | + local is_owner=$(npm owner ls | grep ".*$npm_user <.*") |
| 67 | + |
| 68 | + if ! [[ "$npm_user" ]]; then |
| 69 | + print "[check: FAIL] You must be logged in to NPM to publish, run \"npm login\" first." |
| 70 | + exit 1 |
| 71 | + fi |
| 72 | + |
| 73 | + if [[ -z "$is_collaborator" ]] && [[ -z "$is_owner" ]]; then |
| 74 | + print "[check: FAILED] $npm_user is not an NPM owner or collaborator. Request access from:" |
| 75 | + npm owner ls |
| 76 | + exit 1 |
| 77 | + fi |
| 78 | + fi |
| 79 | + |
| 80 | + print "[check: DONE] Ready to publish!" |
| 81 | +} |
| 82 | + |
| 83 | +publish() { |
| 84 | + set -e |
| 85 | + |
| 86 | + local version=$1 |
| 87 | + |
| 88 | + if [[ -z ${version} ]]; then |
| 89 | + usage |
| 90 | + exit 1 |
| 91 | + fi |
| 92 | + |
| 93 | + # all checks out, publish |
| 94 | + print "[publish] Publishing new $version version." |
| 95 | + |
| 96 | + print "[publish] ...npm version $version" |
| 97 | + npm version ${version} |
| 98 | + |
| 99 | + print "[publish] ...git push" |
| 100 | + git push |
| 101 | + |
| 102 | + print "[publish] ...git push --follow-tags" |
| 103 | + git push --follow-tags |
| 104 | + |
| 105 | + print "[publish] ...npm publish" |
| 106 | + npm publish |
| 107 | + |
| 108 | + print "[publish] ...done!" |
| 109 | +} |
| 110 | + |
| 111 | +run() { |
| 112 | + check |
| 113 | + publish |
| 114 | +} |
| 115 | + |
| 116 | +case $1 in |
| 117 | + "major" | "minor" | "patch") |
| 118 | + run |
| 119 | + ;; |
| 120 | + |
| 121 | + "check") |
| 122 | + check |
| 123 | + ;; |
| 124 | + |
| 125 | + "publish") |
| 126 | + publish $2 |
| 127 | + ;; |
| 128 | + |
| 129 | + *) |
| 130 | + usage |
| 131 | + exit 1 |
| 132 | + ;; |
| 133 | +esac |
0 commit comments