Skip to content

Commit

Permalink
refactor: app version requires check
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Wang <[email protected]>
  • Loading branch information
ruibaby committed Sep 26, 2023
1 parent 309e554 commit 41b6428
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 19 deletions.
4 changes: 2 additions & 2 deletions console/src/components/detail/DetailReleaseItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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<{
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 2 additions & 5 deletions console/src/composables/use-app-compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ApplicationSearchResult | undefined>) {
const { haloVersion } = useHaloVersion();
Expand Down Expand Up @@ -64,12 +65,8 @@ export function useAppCompare(app: Ref<ApplicationSearchResult | undefined>) {
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 {
Expand Down
8 changes: 2 additions & 6 deletions console/src/composables/use-plugin-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Plugin | undefined>) {
const { haloVersion } = useHaloVersion();
Expand Down Expand Up @@ -62,12 +63,7 @@ export function usePluginVersion(plugin: Ref<Plugin | undefined>) {
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 };
Expand Down
8 changes: 2 additions & 6 deletions console/src/composables/use-theme-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Theme | undefined>) {
const { haloVersion } = useHaloVersion();
Expand Down Expand Up @@ -62,12 +63,7 @@ export function useThemeVersion(theme: Ref<Theme | undefined>) {
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 };
Expand Down
25 changes: 25 additions & 0 deletions console/src/utils/version.ts
Original file line number Diff line number Diff line change
@@ -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 });
}

0 comments on commit 41b6428

Please sign in to comment.