diff --git a/console/src/components/detail/DetailReleaseItem.vue b/console/src/components/detail/DetailReleaseItem.vue index 96eebdb..3eebe1a 100644 --- a/console/src/components/detail/DetailReleaseItem.vue +++ b/console/src/components/detail/DetailReleaseItem.vue @@ -16,6 +16,7 @@ import type { AxiosError } from "axios"; import storeApiClient from "@/utils/store-api-client"; import PaymentCheckModal from "../PaymentCheckModal.vue"; import { usePaymentCheckModal } from "@/composables/use-payment-check-modal"; +import { satisfiesRequires } from "@/utils/version"; const props = withDefaults( defineProps<{ @@ -56,8 +57,7 @@ const hasInstalled = computed(() => { const isSatisfies = computed(() => { const { requires } = props.release.release.spec; - if (!haloVersion.value || !requires) return false; - return semver.satisfies(haloVersion.value, requires, { includePrerelease: true }); + return satisfiesRequires(haloVersion.value, requires); }); async function getDownloadUrl(asset: ApplicationReleaseAsset) { diff --git a/console/src/composables/use-app-compare.ts b/console/src/composables/use-app-compare.ts index aa84bb1..645c165 100644 --- a/console/src/composables/use-app-compare.ts +++ b/console/src/composables/use-app-compare.ts @@ -5,6 +5,7 @@ import semver from "semver"; import { useHaloVersion } from "./use-halo-version"; import { useFetchInstalledPlugins } from "./use-plugin"; import { useFetchInstalledThemes } from "./use-theme"; +import { satisfiesRequires } from "@/utils/version"; export function useAppCompare(app: Ref) { const { haloVersion } = useHaloVersion(); @@ -64,12 +65,8 @@ export function useAppCompare(app: Ref) { if (!app.value?.latestRelease) { return false; } - const { requires } = app.value.latestRelease.spec; - - if (!haloVersion.value || !requires) return false; - - return semver.satisfies(haloVersion.value, requires, { includePrerelease: true }); + return satisfiesRequires(haloVersion.value, requires); }); return { diff --git a/console/src/composables/use-plugin-version.ts b/console/src/composables/use-plugin-version.ts index 564dc6f..463d21c 100644 --- a/console/src/composables/use-plugin-version.ts +++ b/console/src/composables/use-plugin-version.ts @@ -7,6 +7,7 @@ import semver from "semver"; import { useHaloVersion } from "./use-halo-version"; import { STORE_APP_ID } from "@/constant"; import { useFetchInstalledPlugins } from "./use-plugin"; +import { satisfiesRequires } from "@/utils/version"; export function usePluginVersion(plugin: Ref) { const { haloVersion } = useHaloVersion(); @@ -62,12 +63,7 @@ export function usePluginVersion(plugin: Ref) { return false; } const { requires } = matchedApp.value.latestRelease.spec; - - if (!haloVersion.value || !requires) { - return false; - } - - return semver.satisfies(haloVersion.value, requires, { includePrerelease: true }); + return satisfiesRequires(haloVersion.value, requires); }); return { hasUpdate, isSatisfies, matchedApp }; diff --git a/console/src/composables/use-theme-version.ts b/console/src/composables/use-theme-version.ts index 0aba4f3..fb02717 100644 --- a/console/src/composables/use-theme-version.ts +++ b/console/src/composables/use-theme-version.ts @@ -7,6 +7,7 @@ import semver from "semver"; import { useHaloVersion } from "./use-halo-version"; import { STORE_APP_ID } from "@/constant"; import { useFetchInstalledThemes } from "./use-theme"; +import { satisfiesRequires } from "@/utils/version"; export function useThemeVersion(theme: Ref) { const { haloVersion } = useHaloVersion(); @@ -62,12 +63,7 @@ export function useThemeVersion(theme: Ref) { return false; } const { requires } = matchedApp.value.latestRelease.spec; - - if (!haloVersion.value || !requires) { - return false; - } - - return semver.satisfies(haloVersion.value, requires, { includePrerelease: true }); + return satisfiesRequires(haloVersion.value, requires); }); return { hasUpdate, isSatisfies, matchedApp }; diff --git a/console/src/utils/version.ts b/console/src/utils/version.ts new file mode 100644 index 0000000..5aaac2b --- /dev/null +++ b/console/src/utils/version.ts @@ -0,0 +1,25 @@ +import semver from "semver"; + +const VERSION_REGEX = /^\d+\.\d+\.\d+$/g; + +/** + * Checks if a given version satisfies a required version range. + * + * @see https://github.com/halo-dev/halo/blob/a5a69780a37410d2734043d6cae1b718b57c722b/application/src/main/java/run/halo/app/infra/utils/VersionUtils.java#L18 + * @param version - The version to check. + * @param requires - The required version range. + * @returns A boolean indicating whether the version satisfies the required range. + */ +export function satisfiesRequires(version?: string, requires?: string): boolean { + if (!version || !requires) { + return false; + } + + requires = requires.trim(); + + if (VERSION_REGEX.test(requires)) { + requires = `>=${requires}`; + } + + return semver.satisfies(version, requires, { includePrerelease: true }); +}