Skip to content

Commit aee662c

Browse files
committed
add semver and automate package version bumps
1 parent e768f28 commit aee662c

File tree

2 files changed

+183
-2
lines changed

2 files changed

+183
-2
lines changed

.github/workflows/update-dependencies.yml

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ jobs:
2626
with:
2727
node-version: '22.14.0'
2828

29-
- name: 📦 Enable Corepack
30-
run: corepack enable
29+
- name: 📦 Enable Corepack and install semver
30+
run: |
31+
corepack enable
32+
npm install -g [email protected]
3133
3234
- name: 🔍 Get current dependency versions
3335
id: current-versions
@@ -285,11 +287,32 @@ jobs:
285287
git add android/build.gradle
286288
fi
287289
290+
# Determine version bump type using semver script
291+
echo "📈 Analyzing version bump requirements..."
292+
VERSION_BUMP_TYPE=$(node semver.js analyze \
293+
"${{ steps.current-versions.outputs.ios_version }}" \
294+
"${{ steps.update-analysis.outputs.ios_new_version }}" \
295+
"${{ steps.current-versions.outputs.android_version }}" \
296+
"${{ steps.update-analysis.outputs.android_new_version }}")
297+
298+
# Get current version and increment using semver script
299+
echo "📈 Applying $VERSION_BUMP_TYPE version bump..."
300+
CURRENT_VERSION=$(node semver.js current)
301+
NEW_VERSION=$(node semver.js inc "$CURRENT_VERSION" "$VERSION_BUMP_TYPE")
302+
303+
# Update package.json version using semver script
304+
node semver.js update "$NEW_VERSION"
305+
306+
echo "📈 Version bumped ($VERSION_BUMP_TYPE): $CURRENT_VERSION → $NEW_VERSION"
307+
git add package.json semver.js
308+
288309
# Update yarn.lock
289310
echo "📦 Installing dependencies to update lockfile..."
290311
yarn install --immutable
291312
git add yarn.lock
292313
314+
CHANGES="$CHANGES\\n- Bumped version from $CURRENT_VERSION to $NEW_VERSION ($VERSION_BUMP_TYPE)"
315+
293316
# Store for next steps
294317
echo "changes<<EOF" >> $GITHUB_OUTPUT
295318
echo -e "$CHANGES" >> $GITHUB_OUTPUT

semver.js

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
#!/usr/bin/env node
2+
3+
const semver = require('semver');
4+
5+
// Parse command line arguments
6+
const args = process.argv.slice(2);
7+
const command = args[0];
8+
9+
function showHelp() {
10+
console.log(`
11+
Usage: node semver.js <command> [arguments]
12+
13+
Commands:
14+
diff <version1> <version2> - Compare two versions and return difference type
15+
inc <version> <type> - Increment version by type (major|minor|patch)
16+
current - Get current version from package.json
17+
18+
Examples:
19+
node semver.js diff 19.1.2 19.2.0 # Returns: minor
20+
node semver.js inc 1.0.0 major # Returns: 2.0.0
21+
node semver.js current # Returns: 1.0.0
22+
`);
23+
}
24+
25+
function getCurrentVersion() {
26+
try {
27+
const pkg = require('./package.json');
28+
return pkg.version;
29+
} catch (error) {
30+
console.error('Error reading package.json:', error.message);
31+
process.exit(1);
32+
}
33+
}
34+
35+
function updatePackageVersion(newVersion) {
36+
try {
37+
const fs = require('fs');
38+
const pkg = require('./package.json');
39+
pkg.version = newVersion;
40+
fs.writeFileSync('./package.json', JSON.stringify(pkg, null, 2) + '\n');
41+
return newVersion;
42+
} catch (error) {
43+
console.error('Error updating package.json:', error.message);
44+
process.exit(1);
45+
}
46+
}
47+
48+
switch (command) {
49+
case 'diff': {
50+
const [version1, version2] = args.slice(1);
51+
if (!version1 || !version2) {
52+
console.error('Error: diff requires two versions');
53+
process.exit(1);
54+
}
55+
56+
// Clean versions (remove 'v' prefix if present)
57+
const clean1 = version1.replace(/^v/, '');
58+
const clean2 = version2.replace(/^v/, '');
59+
60+
try {
61+
const diff = semver.diff(clean1, clean2);
62+
console.log(diff || 'patch'); // Default to patch if no diff
63+
} catch (error) {
64+
console.error('Error comparing versions:', error.message);
65+
process.exit(1);
66+
}
67+
break;
68+
}
69+
70+
case 'inc': {
71+
const [version, type] = args.slice(1);
72+
if (!version || !type) {
73+
console.error('Error: inc requires version and type (major|minor|patch)');
74+
process.exit(1);
75+
}
76+
77+
try {
78+
const newVersion = semver.inc(version, type);
79+
console.log(newVersion);
80+
} catch (error) {
81+
console.error('Error incrementing version:', error.message);
82+
process.exit(1);
83+
}
84+
break;
85+
}
86+
87+
case 'current': {
88+
const currentVersion = getCurrentVersion();
89+
console.log(currentVersion);
90+
break;
91+
}
92+
93+
case 'update': {
94+
const [newVersion] = args.slice(1);
95+
if (!newVersion) {
96+
console.error('Error: update requires new version');
97+
process.exit(1);
98+
}
99+
100+
const result = updatePackageVersion(newVersion);
101+
console.log(result);
102+
break;
103+
}
104+
105+
case 'analyze': {
106+
// Special command for workflow: analyze multiple version changes and determine bump type
107+
const iosOld = args[1];
108+
const iosNew = args[2];
109+
const androidOld = args[3];
110+
const androidNew = args[4];
111+
112+
let bumpType = 'patch'; // Default
113+
114+
if (iosOld && iosNew && iosOld !== 'null' && iosNew !== 'null') {
115+
const iosDiff = semver.diff(
116+
iosOld.replace(/^v/, ''),
117+
iosNew.replace(/^v/, '')
118+
);
119+
console.error(`📱 iOS: ${iosOld}${iosNew} (${iosDiff})`);
120+
121+
if (iosDiff === 'major') {
122+
bumpType = 'major';
123+
} else if (iosDiff === 'minor' && bumpType !== 'major') {
124+
bumpType = 'minor';
125+
}
126+
}
127+
128+
if (
129+
androidOld &&
130+
androidNew &&
131+
androidOld !== 'null' &&
132+
androidNew !== 'null'
133+
) {
134+
const androidDiff = semver.diff(
135+
androidOld.replace(/^v/, ''),
136+
androidNew.replace(/^v/, '')
137+
);
138+
console.error(
139+
`🤖 Android: ${androidOld}${androidNew} (${androidDiff})`
140+
);
141+
142+
if (androidDiff === 'major') {
143+
bumpType = 'major';
144+
} else if (androidDiff === 'minor' && bumpType !== 'major') {
145+
bumpType = 'minor';
146+
}
147+
}
148+
149+
console.error(`📈 Determined bump type: ${bumpType}`);
150+
console.log(bumpType);
151+
break;
152+
}
153+
154+
default:
155+
console.error(`Unknown command: ${command}`);
156+
showHelp();
157+
process.exit(1);
158+
}

0 commit comments

Comments
 (0)