Skip to content

Commit d2ce61f

Browse files
committed
Fix release version comparison.
1 parent 9ca32bf commit d2ce61f

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

__tests__/main.test.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,26 @@ test('should bump version if same tag is available', () => {
1414
expect(nextVersion).toBe('0.0.1-preview.1');
1515
});
1616

17-
test('should bump version if same tag is available but prefixed', () => {
17+
test('should bump pre-release version if same tag is available but prefixed', () => {
1818
const nextVersion = getNextVersion('v0.0.1-preview.0', ['0.0.1-preview.0']);
1919
expect(nextVersion).toBe('0.0.1-preview.1');
2020
});
2121

22-
test('should bump version if same tag is available but not prefixed', () => {
22+
test('should bump pre-release version if same tag is available but not prefixed', () => {
2323
const nextVersion = getNextVersion('0.0.1-preview.0', ['v0.0.1-preview.0']);
2424
expect(nextVersion).toBe('0.0.1-preview.1');
2525
});
2626

27+
test('should bump release version if same tag is available but prefixed', () => {
28+
const nextVersion = getNextVersion('v0.1.0', ['0.1.0']);
29+
expect(nextVersion).toBe('0.1.1');
30+
});
31+
32+
test('should bump release version if same tag is available but not prefixed', () => {
33+
const nextVersion = getNextVersion('0.1.0', ['v0.1.0']);
34+
expect(nextVersion).toBe('0.1.1');
35+
});
36+
2737
test('should use bump latest pre-release tag if multiple tags are available', async () => {
2838
const nextVersion = getNextVersion('0.0.1-preview.0', ['0.0.1-preview.1', '0.0.1-preview.0']);
2939
expect(nextVersion).toBe('0.0.1-preview.2');

src/main.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ export function getNextVersion(version: string, tags: string[]): string {
2727

2828
let nextVer: SemVer | null = null;
2929

30-
if (semVer.prerelease.length === 0) {
31-
if (tags.includes(semVer.version)) {
30+
if (!isPreRelease(semVer)) {
31+
if (tagsInclude(tags, semVer)) {
3232
if (semVer.patch > 0) {
3333
throw new Error(
3434
`Version in package.json is ${version} but tag already exists. Set patch to 0 to enable auto-increment.`,
@@ -58,6 +58,20 @@ export function getNextVersion(version: string, tags: string[]): string {
5858
return nextVer.version;
5959
}
6060

61+
function tagsInclude(tags: string[], v: SemVer): boolean {
62+
for (const tag of tags) {
63+
const vt = new SemVer(tag);
64+
if (vt.compare(v) === 0) {
65+
return true;
66+
}
67+
}
68+
return false;
69+
}
70+
71+
function isPreRelease(semVer: SemVer): boolean {
72+
return semVer.prerelease.length > 0;
73+
}
74+
6175
function isSameRelease(v1: SemVer, v2: SemVer): boolean {
6276
return v1.major === v2.major && v1.minor === v2.minor && v1.patch === v2.patch;
6377
}

0 commit comments

Comments
 (0)