Skip to content

Commit 58c791f

Browse files
authoredJun 15, 2023
fix: diff when detecting major change from prerelease (#566)
Fixes #561
1 parent 5c8efbc commit 58c791f

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed
 

‎functions/diff.js

+31-20
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,35 @@ const diff = (version1, version2) => {
1313
const highVersion = v1Higher ? v1 : v2
1414
const lowVersion = v1Higher ? v2 : v1
1515
const highHasPre = !!highVersion.prerelease.length
16+
const lowHasPre = !!lowVersion.prerelease.length
17+
18+
if (lowHasPre && !highHasPre) {
19+
// Going from prerelease -> no prerelease requires some special casing
20+
21+
// If the low version has only a major, then it will always be a major
22+
// Some examples:
23+
// 1.0.0-1 -> 1.0.0
24+
// 1.0.0-1 -> 1.1.1
25+
// 1.0.0-1 -> 2.0.0
26+
if (!lowVersion.patch && !lowVersion.minor) {
27+
return 'major'
28+
}
29+
30+
// Otherwise it can be determined by checking the high version
31+
32+
if (highVersion.patch) {
33+
// anything higher than a patch bump would result in the wrong version
34+
return 'patch'
35+
}
36+
37+
if (highVersion.minor) {
38+
// anything higher than a minor bump would result in the wrong version
39+
return 'minor'
40+
}
41+
42+
// bumping major/minor/patch all have same result
43+
return 'major'
44+
}
1645

1746
// add the `pre` prefix if we are going to a prerelease version
1847
const prefix = highHasPre ? 'pre' : ''
@@ -29,26 +58,8 @@ const diff = (version1, version2) => {
2958
return prefix + 'patch'
3059
}
3160

32-
// at this point we know stable versions match but overall versions are not equal,
33-
// so either they are both prereleases, or the lower version is a prerelease
34-
35-
if (highHasPre) {
36-
// high and low are preleases
37-
return 'prerelease'
38-
}
39-
40-
if (lowVersion.patch) {
41-
// anything higher than a patch bump would result in the wrong version
42-
return 'patch'
43-
}
44-
45-
if (lowVersion.minor) {
46-
// anything higher than a minor bump would result in the wrong version
47-
return 'minor'
48-
}
49-
50-
// bumping major/minor/patch all have same result
51-
return 'major'
61+
// high and low are preleases
62+
return 'prerelease'
5263
}
5364

5465
module.exports = diff

‎test/functions/diff.js

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ test('diff versions test', (t) => {
2626
['0.0.2-1', '1.0.0', 'major'],
2727
['0.1.0-1', '0.1.0', 'minor'],
2828
['1.0.0-1', '1.0.0', 'major'],
29+
['1.0.0-1', '1.1.1', 'major'],
30+
['1.0.0-1', '2.1.1', 'major'],
2931
['1.0.1-1', '1.0.1', 'patch'],
3032
['0.0.0-1', '0.0.0', 'major'],
3133
['1.0.0-1', '2.0.0', 'major'],

0 commit comments

Comments
 (0)
Please sign in to comment.