Skip to content

Commit b3f08da

Browse files
authored
fix: version comparison logic (#50)
* fix: version comparison logic * use existing method
1 parent 235c654 commit b3f08da

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

src/releases.ts

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { window } from "vscode";
2+
import semver from "semver";
23
import { logger } from "./logger";
34
import { getConfig } from "./config";
45
import { state } from "./state";
@@ -80,24 +81,25 @@ export class Releases {
8081
return false;
8182
}
8283

83-
const prereleaseEnabled = this.prereleaseEnabled();
84+
const filteredReleases = this.all();
8485

85-
const idx = this.releases
86-
.filter((r) => prereleaseEnabled || !r.prerelease)
87-
.findIndex((r) => r.tag_name === tag_name);
88-
89-
if (idx === -1) {
90-
return true;
86+
if (filteredReleases.length === 0) {
87+
return false;
9188
}
9289

93-
if (idx >= 3) {
94-
return true;
95-
}
90+
// Get the latest version (first in the sorted array)
91+
const latestVersion = filteredReleases[0].tag_name;
92+
93+
// Use semantic version comparison to check if current version is outdated
94+
// A version is considered outdated if it's less than the latest version
95+
return semver.lt(tag_name, latestVersion);
9696
}
9797

9898
latestVersion(): string | null {
99-
if (this.releases.length > 0) {
100-
return this.releases[0].tag_name;
99+
const filteredReleases = this.all();
100+
101+
if (filteredReleases.length > 0) {
102+
return filteredReleases[0].tag_name;
101103
} else {
102104
return null;
103105
}
@@ -165,12 +167,17 @@ async function fromGithub(): Promise<Release[]> {
165167

166168
const filteredAndSorted = releases
167169
.filter((r) => !r.draft)
168-
.sort(
169-
(a, b) =>
170-
new Date(b.published_at).getTime() - new Date(a.published_at).getTime()
171-
);
170+
.sort((a, b) => {
171+
// Sort by semantic version (highest first), then by publication date as fallback
172+
const versionComparison = semver.rcompare(a.tag_name, b.tag_name);
173+
if (versionComparison !== 0) {
174+
return versionComparison;
175+
}
176+
// If versions are equal, sort by publication date (newest first)
177+
return new Date(b.published_at).getTime() - new Date(a.published_at).getTime();
178+
});
172179

173180
await setCache(filteredAndSorted);
174181

175182
return filteredAndSorted;
176-
}
183+
}

src/session.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export const createSession = async (
8787
Date.now() - new Date(lastNotifiedOfUpdate).getTime() > daysToMs(3)
8888
) {
8989
window.showInformationMessage(
90-
`PostgresTools ${version} is outdated, consider updating to ${state.releases.latestVersion()}.`
90+
`A new version of PostgresTools is available! your version ${version} is outdated, consider updating to latest version ${state.releases.latestVersion()}.`
9191
);
9292
await state.context.globalState.update(
9393
"lastNotifiedOfUpdate",
@@ -100,7 +100,7 @@ export const createSession = async (
100100
!semver.gte(version, "0.8.0")
101101
) {
102102
window.showInformationMessage(
103-
`PostgresTools ${version} does not support multi root workspaces. Consider upgrading to >= 0.8.0.`
103+
`Your current PostgresTools version ${version} does not support multi-root workspaces. Consider upgrading to version >= 0.8.0.`
104104
);
105105
}
106106

0 commit comments

Comments
 (0)