Skip to content

Commit

Permalink
fix: find last stable release tag using semver (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyoban committed Apr 15, 2024
1 parent 84afa2f commit a3b46b4
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,27 @@ export async function getGitTags() {
.reverse()
}

function getTagWithoutPrefix(tag: string) {
return tag.replace(/^v/, '')
}

export async function getLastMatchingTag(inputTag: string) {
const isVersion = semver.valid(semver.coerce(inputTag))
const inputTagWithoutPrefix = getTagWithoutPrefix(inputTag)
const isVersion = semver.valid(inputTagWithoutPrefix) !== null
const isPrerelease = semver.prerelease(inputTag) !== null
const tags = await getGitTags()

let tag: string | undefined
// Doing a stable release, find the last stable release to compare with
if (!isPrerelease && isVersion)
tag = tags.find(tag => tag !== inputTag && tag[0] === 'v' && !tag.includes('-'))
if (!isPrerelease && isVersion) {
tag = tags.find((tag) => {
const tagWithoutPrefix = getTagWithoutPrefix(tag)

return tagWithoutPrefix !== inputTagWithoutPrefix
&& semver.valid(tagWithoutPrefix) !== null
&& semver.prerelease(tagWithoutPrefix) === null
})
}

// Fallback to the last tag, that are not the input tag
tag ||= tags.find(tag => tag !== inputTag)
Expand Down

0 comments on commit a3b46b4

Please sign in to comment.