Skip to content

Commit 24dcd8e

Browse files
committed
Automatically set calver version in changelog and package.json
1 parent f879be3 commit 24dcd8e

File tree

4 files changed

+74
-3
lines changed

4 files changed

+74
-3
lines changed

.github/workflows/deploy.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ jobs:
3030
with:
3131
title: Version Packages (${{ github.ref_name }})
3232
publish: yarn run deploy --tag ${{ github.ref_name }}
33+
version: yarn calver
3334
env:
3435
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
3536
GITHUB_TOKEN: ${{ secrets.SHOPIFY_GH_ACCESS_TOKEN }}

documentation/versions-and-deploys.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ Like the `unstable` branch, the `internal` branch also publishes snapshot releas
4646
To create a new stable version, you will need to follow these steps:
4747

4848
1. Run the "create stable version" action.
49-
1. Pull down the branch that that was created by the GitHub action (it should have the name `changeset-release/{{BRANCH_NAME}}`). Instead of the patch version changes that were made by the action, update the version of all packages manually to be the first patch release of a new version range. For example, if you are creating a `2025-01` API version, you will set the package versions of all packages to `2025.1.0`. Apply this change to `packages/ui-extensions/package.json`, `packages/ui-extensions/CHANGELOG.md`, `packages/ui-extensions-react/package.json`, and `packages/ui-extensions-react/CHANGELOG.md`.
50-
> Note: do not update the root-level `package.json`.
49+
1. Verify that the versions in the package.json and changelog.md files in packages/ were updated correctly. Make sure you get the PR reviewed by one other member of the [UI Extension Stewards GitHub team](https://github.com/orgs/Shopify/teams/ui-extension-stewards).
5150
1. Push your new changes, and make sure you get the PR reviewed by one other member of the [UI Extension Stewards GitHub team](https://github.com/orgs/Shopify/teams/ui-extension-stewards).
5251
1. Merge the PR, and let robots release the new versions to NPM and tag it appropriately.
5352
1. For any changes from `unstable` that have been incorporated into the new version, delete their changeset files on the `unstable` branch and replace the existing `CHANGELOG.md` files in `unstable` with what was just shipped.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"run:ts:watch": "nodemon --ext .ts,.tsx,.mjs,.json,.graphql node_modules/.bin/babel-node --extensions .ts,.tsx,.mjs,.js,.json",
3232
"scaffold-docs:admin": "./packages/ui-extensions/docs/surfaces/admin/create-doc-files.sh \"admin\"",
3333
"test": "loom test",
34-
"type-check": "loom type-check"
34+
"type-check": "loom type-check",
35+
"calver": "changeset version && ./scripts/calver.sh"
3536
},
3637
"devDependencies": {
3738
"@babel/node": "^7.8.7",

scripts/calver.sh

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/bin/bash
2+
3+
# Get the current version from a package.json file
4+
get_version() {
5+
grep -m1 '"version":' "$1" | awk -F'"' '{print $4}'
6+
}
7+
8+
ui_version=$(get_version "packages/ui-extensions/package.json")
9+
react_version=$(get_version "packages/ui-extensions-react/package.json")
10+
11+
create_new_version() {
12+
branch_name=$(git rev-parse --abbrev-ref HEAD)
13+
# Split the branch name into year and month
14+
IFS='-' read -r year month <<<"$branch_name"
15+
16+
# Remove leading zero from month
17+
month=${month#0}
18+
19+
echo "$year.$month.0"
20+
}
21+
22+
increment_patch() {
23+
echo "$1" | awk -F. '{$NF = $NF + 1;}1' OFS=.
24+
}
25+
26+
# Determine the new versions
27+
if [ "$ui_version" = "0.0.0" ]; then
28+
new_version=$(create_new_version)
29+
else
30+
new_version=$(increment_patch "$ui_version")
31+
fi
32+
if [ "$react_version" = "0.0.0" ]; then
33+
new_version_react=$(create_new_version)
34+
else
35+
new_version_react=$(increment_patch "$react_version")
36+
fi
37+
38+
update_version() {
39+
local package_file="$1"
40+
local changelog_file="$2"
41+
local version="$3"
42+
43+
# Update package.json
44+
awk -v version="$version" '
45+
/"version":/ { gsub(/"version": "[^"]*"/, "\"version\": \"" version "\"") }
46+
/"peerDependencies"/, /}/ {
47+
if (/"@shopify\/ui-extensions":/) {
48+
gsub(/"@shopify\/ui-extensions": "[^"]*"/, "\"@shopify/ui-extensions\": \"" version "\"")
49+
}
50+
}
51+
/"devDependencies"/, /}/ {
52+
if (/"@shopify\/ui-extensions":/) {
53+
gsub(/"@shopify\/ui-extensions": "[^"]*"/, "\"@shopify/ui-extensions\": \"" version "\"")
54+
}
55+
}
56+
{ print }
57+
' "$package_file" >"${package_file}.tmp" && mv "${package_file}.tmp" "$package_file"
58+
59+
# Update CHANGELOG.md
60+
awk -v version="$version" '
61+
/^## 0\.0\.0/ { sub(/^## 0\.0\.0/, "## " version); }
62+
{ print }
63+
' "$changelog_file" >"${changelog_file}.tmp" && mv "${changelog_file}.tmp" "$changelog_file"
64+
}
65+
66+
# Update versions in all relevant files
67+
update_version "packages/ui-extensions/package.json" "packages/ui-extensions/CHANGELOG.md" "$new_version"
68+
update_version "packages/ui-extensions-react/package.json" "packages/ui-extensions-react/CHANGELOG.md" "$new_version_react"
69+
70+
echo "Version updated to $new_version"

0 commit comments

Comments
 (0)