[refactor] replace manual semver operations with semver package

Replace custom compareVersions and isSemVer functions with the robust semver package to handle version comparisons more reliably. This addresses edge cases and follows industry standards for semantic version handling.
This commit is contained in:
bymyself
2025-08-06 13:43:42 -07:00
committed by Christian Byrne
parent 85aa89da45
commit 9da2a5b6de
8 changed files with 99 additions and 104 deletions

View File

@@ -1,5 +1,6 @@
import _ from 'lodash'
import { defineStore } from 'pinia'
import * as semver from 'semver'
import { ref } from 'vue'
import type { Settings } from '@/schemas/apiSchema'
@@ -7,7 +8,6 @@ import { api } from '@/scripts/api'
import { app } from '@/scripts/app'
import type { SettingParams } from '@/types/settingTypes'
import type { TreeNode } from '@/types/treeExplorerTypes'
import { compareVersions, isSemVer } from '@/utils/formatUtil'
export const getSettingInfo = (setting: SettingParams) => {
const parts = setting.category || setting.id.split('.')
@@ -132,17 +132,30 @@ export const useSettingStore = defineStore('setting', () => {
if (installedVersion) {
const sortedVersions = Object.keys(defaultsByInstallVersion).sort(
(a, b) => compareVersions(b, a)
(a, b) => {
const versionA = semver.coerce(a)
const versionB = semver.coerce(b)
if (!versionA || !versionB) return 0
return semver.rcompare(versionA, versionB)
}
)
for (const version of sortedVersions) {
// Ensure the version is in a valid format before comparing
if (!isSemVer(version)) {
if (!semver.valid(version)) {
continue
}
if (compareVersions(installedVersion, version) >= 0) {
const versionedDefault = defaultsByInstallVersion[version]
const installedSemver = semver.coerce(installedVersion)
const targetSemver = semver.coerce(version)
if (
installedSemver &&
targetSemver &&
semver.gte(installedSemver, targetSemver)
) {
const versionedDefault =
defaultsByInstallVersion[
version as `${number}.${number}.${number}`
]
return typeof versionedDefault === 'function'
? versionedDefault()
: versionedDefault