Skip to content

Commit

Permalink
Switch to "semver-compare" lib for semantic versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
OrigamingWasTaken committed Oct 8, 2024
1 parent f16ace5 commit 513ac75
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 31 deletions.
Binary file modified bun.lockb
Binary file not shown.
10 changes: 5 additions & 5 deletions frontend/src/windows/main/components/Updater.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
import { toast } from 'svelte-sonner';
import { version } from '../../../../../package.json';
import { loadSettings, saveSettings } from './settings';
import { compareVersions, curlGet } from '../ts/utils';
import { curlGet } from '../ts/utils';
import Link from './Link.svelte';
import { shell } from '../ts/tools/shell';
import compare from 'semver-compare';
let showUpdatePopup = false;
let updateVersion = version;
let body = '';
async function checkForUpdate() {
const checkWifi = await shell(`if [[ "$(networksetup -getairportnetwork en0 | grep -o 'Current Wi-Fi Network:')" != "" ]]; then echo "true"; else echo "false"; fi`,[],{completeCommand: true});
const checkWifi = await shell(`if ping -c 1 -W 1 8.8.8.8 &> /dev/null; then echo "true"; else echo "false"; fi`,[],{completeCommand: true});
if (!checkWifi.stdOut.includes('true')) {
toast.error('Could not connect to internet');
return;
Expand All @@ -26,14 +27,13 @@
});
if (releases.message) return;
for (const re of releases) {
if (compareVersions(re.tag_name, updateVersion) > 0) {
if (compare(re.tag_name, updateVersion) === 1) {
updateVersion = re.tag_name;
body = re.body;
}
}
if (updateVersion === version) return;
const compare = compareVersions(updateVersion, version);
if (compare > 0) {
if (compare(updateVersion, version) === 1) {
console.info(`[Updater] A new release is available: ${updateVersion}`);
const settings = await loadSettings('updating');
if (settings) {
Expand Down
26 changes: 0 additions & 26 deletions frontend/src/windows/main/ts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,6 @@ export async function curlGet(url: string): Promise<any> {
return res;
}

/**
* Compare two semantic version strings.
*
* @param {string} v1 - The first version string.
* @param {string} v2 - The second version string.
* @returns {number} -1 if v1 < v2, 1 if v1 > v2, 0 if they are equal.
*/
export function compareVersions(v1: string, v2: string): number {
const v1Parts: number[] = v1.split('.').map(Number);
const v2Parts: number[] = v2.split('.').map(Number);

for (let i = 0; i < Math.max(v1Parts.length, v2Parts.length); i++) {
const v1Part: number = v1Parts[i] || 0;
const v2Part: number = v2Parts[i] || 0;

if (v1Part > v2Part) {
return 1;
}
if (v1Part < v2Part) {
return -1;
}
}

return 0;
}

export function sleep(ms = 0) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@sveltejs/vite-plugin-svelte": "^3.0.2",
"@tsconfig/svelte": "^5.0.2",
"@types/path-browserify": "^1.0.2",
"@types/semver-compare": "^1.0.3",
"@types/signale": "^1.4.7",
"@types/tar": "^6.1.11",
"autoprefixer": "^10.4.19",
Expand Down Expand Up @@ -55,6 +56,7 @@
"lucide-svelte": "^0.446.0",
"mode-watcher": "^0.3.1",
"path-browserify": "^1.0.1",
"semver-compare": "^1.0.0",
"svelte-markdown": "^0.4.1",
"svelte-sonner": "^0.3.21",
"tailwind-merge": "^2.5.2",
Expand Down

0 comments on commit 513ac75

Please sign in to comment.