Skip to content

Commit

Permalink
Add publish script (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
christopher-stripe authored Apr 16, 2020
1 parent 2a28dec commit 8073811
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"build": "yarn clean && yarn rollup -c",
"clean": "rimraf dist",
"ci": "yarn lint && yarn test && yarn test:versions && yarn test:types && yarn typecheck && yarn build",
"prepublishOnly": "yarn ci",
"prepublishOnly": "echo \"\nPlease use ./scripts/publish instead\n\" && exit 1",
"prettier": "prettier './**/*.{js,ts,md,html,css}' --write",
"prettier-list-different": "prettier './**/*.{js,ts,md,html,css}' --list-different"
},
Expand Down
140 changes: 140 additions & 0 deletions scripts/publish
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#!/bin/bash

set -euo pipefail
IFS=$'\n\t'

RELEASE_TYPE=${1:-}

echo_help() {
cat << EOF
USAGE:
./scripts/publish <release_type>
ARGS:
<release_type>
A Semantic Versioning release type used to bump the version number. Either "patch", "minor", or "major".
EOF
}

create_github_release() {
if which hub | grep -q "not found"; then
create_github_release_fallback
return
fi

# Get the last two releases. For example, `("v1.3.1" "v1.3.2")`
local versions=($(git tag --sort version:refname | grep '^v' | tail -n 2))

# If we didn't find exactly two previous version versions, give up
if [ ${#versions[@]} -ne 2 ]; then
create_github_release_fallback
return
fi

local previous_version="${versions[0]}"
local current_version="${versions[1]}"
local commit_titles=$(git log --pretty=format:"- %s" "$previous_version".."$current_version"^)
local release_notes="$(cat << EOF
$current_version
<!-- Please group the following commits into one of the sections below. -->
<!-- Remove empty sections when done. -->
$commit_titles
### New features
### Fixes
### Changed
EOF
)"

local release_url=$(hub release create -em "$release_notes" "$current_version")

cat << EOF
Created GitHub release:
$release_url
EOF

}

create_github_release_fallback() {
cat << EOF
Remember to create a release on GitHub with a changelog notes:
https://github.com/stripe/stripe-js/releases/new
EOF
}

# Show help if no arguments passed
if [ $# -eq 0 ]; then
echo "Error! Missing release type argument"
echo ""
echo_help
exit 1
fi

# Show help message if -h, --help, or help passed
case $1 in
-h | --help | help)
echo_help
exit 0
;;
esac

# Validate passed release type
case $RELEASE_TYPE in
patch | minor | major)
;;

*)
echo "Error! Invalid release type supplied"
echo ""
echo_help
exit 1
;;
esac

# Make sure our working dir is the repo root directory
cd "$(git rev-parse --show-toplevel)"

echo "Fetching git remotes"
git fetch

GIT_STATUS=$(git status)

if ! grep -q 'On branch master' <<< "$GIT_STATUS"; then
echo "Error! Must be on master branch to publish"
exit 1
fi

if ! grep -q "Your branch is up to date with 'origin/master'." <<< "$GIT_STATUS"; then
echo "Error! Must be up to date with origin/master to publish"
exit 1
fi

if ! grep -q 'working tree clean' <<< "$GIT_STATUS"; then
echo "Error! Cannot publish with dirty working tree"
exit 1
fi

echo "Installing dependencies according to lockfile"
yarn -s install --frozen-lockfile

echo "Linting, testing, and building"
yarn -s run ci

echo "Tagging and publishing $RELEASE_TYPE release"
yarn -s --ignore-scripts publish --$RELEASE_TYPE --access=public

echo "Pushing git commit and tag"
git push --follow-tags

echo "Publish successful!"
echo ""

create_github_release

0 comments on commit 8073811

Please sign in to comment.