summaryrefslogtreecommitdiff
path: root/source/libs/utils.ts
diff options
context:
space:
mode:
authorGravatar Federico Brigante <github@bfred.it> 2020-03-22 22:34:59 +0100
committerGravatar GitHub <noreply@github.com> 2020-03-22 22:34:59 +0100
commitfb220430297d4025b59efcdc80bae202fcd81488 (patch)
tree48db6bac2cf140059689d2a688335bc30153c86c /source/libs/utils.ts
parenta3b960fd9ebe3f98c1b4d0a60638952c6cb74ed6 (diff)
downloadrefined-github-fb220430297d4025b59efcdc80bae202fcd81488.tar.gz
refined-github-fb220430297d4025b59efcdc80bae202fcd81488.tar.zst
refined-github-fb220430297d4025b59efcdc80bae202fcd81488.zip
Exclude pre-releases from `latest-tag-button` (#2905)
Diffstat (limited to 'source/libs/utils.ts')
-rw-r--r--source/libs/utils.ts26
1 files changed, 26 insertions, 0 deletions
diff --git a/source/libs/utils.ts b/source/libs/utils.ts
index 64c76ccf..38d4b6b9 100644
--- a/source/libs/utils.ts
+++ b/source/libs/utils.ts
@@ -2,6 +2,7 @@ import select from 'select-dom';
import onetime from 'onetime';
import stripIndent from 'strip-indent';
import {isRepo, isPR, isIssue} from './page-detect';
+import compareVersions from 'tiny-version-compare';
export function logError(featureName: typeof __featureName__, error: Error | string, ...extras: unknown[]): void {
const message = typeof error === 'string' ? error : error.message;
@@ -177,3 +178,28 @@ export function getScopedSelector(selector: string): string {
export function looseParseInt(text: string): number {
return Number(text.replace(/\D+/g, ''));
}
+
+const validVersion = /^[vr]?\d+(?:\.\d+)+/;
+const isPrerelease = /^[vr]?\d+(?:\.\d+)+(-\d)/;
+export function getLatestVersionTag(tags: string[]): string {
+ // Some tags aren't valid versions; comparison is meaningless.
+ // Just use the latest tag returned by the API (reverse chronologically-sorted list)
+ if (!tags.every(tag => validVersion.test(tag))) {
+ return tags[0];
+ }
+
+ // Exclude pre-releases
+ let releases = tags.filter(tag => !isPrerelease.test(tag));
+ if (releases.length === 0) { // They were all pre-releases; undo.
+ releases = tags;
+ }
+
+ let latestVersion = releases[0];
+ for (const release of releases) {
+ if (compareVersions(latestVersion, release) < 0) {
+ latestVersion = release;
+ }
+ }
+
+ return latestVersion;
+}