From dabed4e37684bb35d2c2b8fefb529b91e5810181 Mon Sep 17 00:00:00 2001 From: Vanita Barrett Date: Fri, 20 May 2022 11:31:52 +0100 Subject: [PATCH] Generate NPM tag within publish-release bash script If we want to release versions of GOV.UK Frontend older than the current major version, e.g: releasing a version 4.5.0. when we've already released version 5.0.0, then we need to be able to override NPM's default "tag" which tags the most recent release as "latest". Tagging v4.5.0 as "latest" would mean people doing `npm install govuk-frontend` would get v4.5.0 instead of v5.0.0. Unfortunately, the only way to NOT tag something as "latest" is to provide a different tag to tag it with. This script compares the tags on Github with the version being proposed. If the version number being proposed is smaller than the highest version tag on Github, the NPM tag will be `latest-[MAJOR-VERSION-NUMBER]` e.g: `latest-4` --- bin/generate-npm-tag.sh | 20 ++++++++++++++++++++ bin/publish-release.sh | 25 ++++++++++++++++++++----- 2 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 bin/generate-npm-tag.sh diff --git a/bin/generate-npm-tag.sh b/bin/generate-npm-tag.sh new file mode 100644 index 0000000000..5f5af9c908 --- /dev/null +++ b/bin/generate-npm-tag.sh @@ -0,0 +1,20 @@ +#!/bin/sh +set -e + +# Get highest tag published on Github +HIGHEST_PUBLISHED_TAG=$(git tag --list 2>/dev/null | sort -V | tail -n1 2>/dev/null | sed 's/v//g') + +# Extract tag version from ./package/package.json +CURRENT_TAG=$(node -p "require('./package/package.json').version") + +function version() { echo "$@" | awk -F. '{ printf("%d%03d%03d\n", $1,$2,$3); }'; } + +if [ $CURRENT_TAG == $HIGHEST_PUBLISHED_TAG ]; then + echo "⚠️ Git tag $TAG already exists. Check you have updated the version in package/package.json correctly." + exit 1 +elif [ $(version $CURRENT_TAG) -ge $(version $HIGHEST_PUBLISHED_TAG) ]; then + NPM_TAG="latest" +else + major_version_num="${CURRENT_TAG:0:1}" + NPM_TAG="latest-$major_version_num" +fi diff --git a/bin/publish-release.sh b/bin/publish-release.sh index f278f62de4..320c94f574 100755 --- a/bin/publish-release.sh +++ b/bin/publish-release.sh @@ -1,14 +1,29 @@ #!/bin/sh set -e +source ./bin/generate-npm-tag.sh + +# Check NPM tag looks as expected +# https://npm.github.io/publishing-pkgs-docs/updating/using-tags.html#publishing-with-tags +echo "This will publish the package with the following NPM tag:" +echo $NPM_TAG +echo " " + +read -r -p "Does this look correct? [y/N] " continue_prompt + +if [[ $continue_prompt != 'y' ]]; then + read -r -p "What should the NPM tag be: " NPM_TAG +fi + echo "Starting a release..." echo " " echo "This will:" echo "- check that you're logged in to npm as the correct user" -echo "- publish the package if it has not been published already" -echo "- check that there is not already a tag published" -echo "- create a new tag" -echo "- push the tag to remote origin" +echo "- publish the package to NPM if it has not been published already" +echo "- tag the package on NPM with '$NPM_TAG'" +echo "- check that there is not already a Github tag published" +echo "- create a new Github tag" +echo "- push the Github tag to remote origin" echo "- create a zip file of the 'dist/' directory locally" echo " " @@ -34,7 +49,7 @@ echo "📦 Publishing package..." # Try publishing cd package -npm publish +[ $NPM_TAG = "latest" ] && npm publish || npm publish --tag $NPM_TAG echo "🗒 Package published!" cd ..