From 2ff7eaaecb2e30dd00c512b1867de02f1ae809eb Mon Sep 17 00:00:00 2001 From: Soybean Date: Tue, 23 Apr 2024 23:38:12 +0800 Subject: [PATCH] fix(projects): fix tags order --- src/constant.ts | 2 ++ src/git.ts | 31 +++++++++++++++++++++++++++++-- src/index.ts | 2 ++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/constant.ts b/src/constant.ts index 55fb0f2..d3699f9 100644 --- a/src/constant.ts +++ b/src/constant.ts @@ -1,5 +1,7 @@ export const VERSION_REG = /v\d+\.\d+\.\d+(-(beta|alpha)\.\d+)?/; +export const RELEASE_VERSION_REG = /v\d+\.\d+\.\d+/; + export const VERSION_REG_OF_MARKDOWN = /## \[v\d+\.\d+\.\d+(-(beta|alpha)\.\d+)?]/g; export const VERSION_WITH_RELEASE = /release\sv\d+\.\d+\.\d+(-(beta|alpha)\.\d+)?/; diff --git a/src/git.ts b/src/git.ts index 8c04d7c..1029533 100644 --- a/src/git.ts +++ b/src/git.ts @@ -2,7 +2,7 @@ import { ofetch } from 'ofetch'; import dayjs from 'dayjs'; import { consola } from 'consola'; import { execCommand, notNullish } from './shared'; -import { VERSION_REG } from './constant'; +import { RELEASE_VERSION_REG, VERSION_REG } from './constant'; import type { GitCommit, GitCommitAuthor, GithubConfig, RawGitCommit, Reference, ResolvedAuthor } from './types'; /** Get the total git tags */ @@ -11,7 +11,34 @@ export async function getTotalGitTags() { const tags = tagStr.split('\n'); - return tags.filter(tag => VERSION_REG.test(tag)); + const filtered = tags.filter(tag => VERSION_REG.test(tag)); + + return sortTags(filtered); +} + +function sortTags(tags: string[]) { + tags.sort((a, b) => { + const versionA = a.match(RELEASE_VERSION_REG)?.[0] || ''; + const versionB = b.match(RELEASE_VERSION_REG)?.[0] || ''; + + if (versionA < versionB) { + return -1; + } else if (versionA > versionB) { + return 1; + } + + const isBetaA = isPrerelease(a); + const isBetaB = isPrerelease(b); + + if (isBetaA && !isBetaB) { + return -1; + } else if (!isBetaA && isBetaB) { + return 1; + } + return 0; + }); + + return tags; } /** Get map of the git tag and date */ diff --git a/src/index.ts b/src/index.ts index 2b6bf3a..cc90357 100644 --- a/src/index.ts +++ b/src/index.ts @@ -107,4 +107,6 @@ export async function generateTotalChangelog(options?: Partial, await writeMarkdown(markdown, opts.output, true); } +generateTotalChangelog(); + export type { ChangelogOption };