Skip to content

Commit

Permalink
Generate NPM tag within publish-release bash script
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
Vanita Barrett committed May 20, 2022
1 parent 137b806 commit dabed4e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
20 changes: 20 additions & 0 deletions bin/generate-npm-tag.sh
Original file line number Diff line number Diff line change
@@ -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
25 changes: 20 additions & 5 deletions bin/publish-release.sh
Original file line number Diff line number Diff line change
@@ -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 " "

Expand All @@ -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 ..

Expand Down

0 comments on commit dabed4e

Please sign in to comment.