generated from halo-dev/plugin-starter
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: app version requires check (#39)
基于 semver 封装新的版本范围检查方法,兼容 [Halo 的规则](https://github.com/halo-dev/halo/blob/a5a69780a37410d2734043d6cae1b718b57c722b/application/src/main/java/run/halo/app/infra/utils/VersionUtils.java#L18)。 Fixes #38 <img width="1216" alt="image" src="https://github.com/halo-dev/plugin-app-store/assets/21301288/ac79588d-d6e5-42a0-9505-22d53431267f"> /kind improvement ```release-note 修复部分应用因为版本范围规则检测方法导致不兼容的问题。 ```
- Loading branch information
Showing
6 changed files
with
66 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { satisfiesRequires } from "../version"; | ||
|
||
describe("satisfiesRequires", () => { | ||
it("should return true when version satisfies required range", () => { | ||
expect(satisfiesRequires("2.0.0", "*")).toBe(true); | ||
expect(satisfiesRequires("2.0.0", "")).toBe(true); | ||
expect(satisfiesRequires("2.0.0", ">=2.0.0")).toBe(true); | ||
expect(satisfiesRequires("2.1.0", "2.0.0")).toBe(true); | ||
}); | ||
|
||
it("should return false when version does not satisfy required range", () => { | ||
expect(satisfiesRequires("0.0.0", ">=2.2.0")).toBe(false); | ||
expect(satisfiesRequires("2.0.0", ">2.0.0")).toBe(false); | ||
expect(satisfiesRequires("2.0.0", ">=2.1.0")).toBe(false); | ||
}); | ||
|
||
it("should return true when version or required range is missing", () => { | ||
expect(satisfiesRequires("2.0.0", undefined)).toBe(true); | ||
expect(satisfiesRequires(undefined, undefined)).toBe(true); | ||
}); | ||
|
||
it("should return true when version satisfies required range with prerelease", () => { | ||
expect(satisfiesRequires("2.0.0-beta.1", ">=2.0.0-beta.1")).toBe(true); | ||
expect(satisfiesRequires("2.0.0-beta.1", ">=2.0.0-beta.0")).toBe(true); | ||
expect(satisfiesRequires("2.0.0-beta.1", ">=2.0.0-alpha.0")).toBe(true); | ||
expect(satisfiesRequires("2.0.0", ">=2.0.0-alpha.0")).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
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) { | ||
version = "0.0.0"; | ||
} | ||
|
||
if (!requires) { | ||
return true; | ||
} | ||
|
||
requires = requires.trim(); | ||
|
||
if (VERSION_REGEX.test(requires)) { | ||
requires = `>=${requires}`; | ||
} | ||
|
||
return semver.satisfies(version, requires, { includePrerelease: true }); | ||
} |