Skip to content

Commit 21d913f

Browse files
committed
Automatically set calver version in changelog and package.json
1 parent be96113 commit 21d913f

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

.github/workflows/deploy.yml

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ jobs:
2929
with:
3030
title: Version Packages (${{ github.ref_name }})
3131
publish: yarn run deploy --tag ${{ github.ref_name }}
32+
version: yarn calver
3233
env:
3334
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
3435
GITHUB_TOKEN: ${{ secrets.SHOPIFY_GH_ACCESS_TOKEN }}

package.json

+2-1
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

+70
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)