Skip to content

Commit e711eb9

Browse files
committed
formatVersion to support chrome extension version format (1.2.3.4444 -> 1.2.3-4444)
1 parent af722d2 commit e711eb9

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

src/background/update-listener.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { type MV2Settings } from '~migrations/schema'
55
import semver from 'semver'
66
import migrateFromMV2 from '~migrations'
77
import { getLatestRelease } from '~api/github'
8+
import { formatVersion } from "~utils/misc"
89

910
chrome.runtime.onInstalled.addListener(async (data: chrome.runtime.InstalledDetails) => {
1011

@@ -42,7 +43,7 @@ chrome.runtime.onInstalled.addListener(async (data: chrome.runtime.InstalledDeta
4243

4344
const lastVersion = (await localStorage.get('last_version')) ?? '0.12.4'
4445
const mv2 = await storage.get<MV2Settings>('settings')
45-
if (mv2 && semver.lt(lastVersion, '2.0.0')) {
46+
if (mv2 && semver.lt(formatVersion(lastVersion), '2.0.0')) {
4647
try {
4748
await migrateFromMV2()
4849
await sendInternal('notify', {
@@ -67,7 +68,7 @@ getSettingStorage('settings.version').then(async (settings) => {
6768
if (!settings.autoCheckUpdate) return
6869
const currentVersion = chrome.runtime.getManifest().version
6970
const latest = await getLatestRelease()
70-
if (semver.lt(currentVersion, latest.tag_name)) {
71+
if (semver.lt(formatVersion(currentVersion), formatVersion(latest.tag_name))) {
7172
await sendInternal('notify', {
7273
type: 'list',
7374
title: 'bilibili-vup-stream-enhancer 已推出新版本',
@@ -84,5 +85,4 @@ getSettingStorage('settings.version').then(async (settings) => {
8485
]
8586
})
8687
}
87-
})
88-
88+
})

src/options/fragments/version.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { StateProxy } from "~hooks/binding"
66
import SwitchListItem from "~options/components/SwitchListItem"
77
import type { ReleaseInfo } from "~types/github"
88
import semver from 'semver';
9+
import { formatVersion } from "~utils/misc"
910

1011

1112
export type SettingSchema = {
@@ -65,7 +66,7 @@ function VersionSettings({ state, useHandler }: StateProxy<SettingSchema>): JSX.
6566
{([current, last]: ReleaseInfo[]) => (
6667
<div className="md:flex justify-between text-center md:text-left space-y-3 md:space-y-0">
6768
<div>
68-
{semver.lt(current.tag_name, last.tag_name) ? (
69+
{semver.lt(formatVersion(current.tag_name), formatVersion(last.tag_name)) ? (
6970
<Typography color="red" className="font-bold mb-4">
7071
你有可用的更新
7172
</Typography>

src/utils/misc.ts

+25
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,29 @@ export function setNestedValue<T extends object, K extends Leaves<T>>(obj: T, pa
201201
const lastKey = keys.pop()
202202
const lastObj = keys.reduce((o, p) => o[p], obj)
203203
lastObj[lastKey] = value
204+
}
205+
206+
207+
208+
/**
209+
* Formats a version string by ensuring it follows the major.minor.patch format.
210+
* If there are additional segments beyond the patch version, they are appended
211+
* as a hyphenated suffix.
212+
*
213+
* @param version - The version string to format.
214+
* @returns The formatted version string.
215+
*
216+
* @example
217+
* ```typescript
218+
* formatVersion("1.2.3"); // "1.2.3"
219+
* formatVersion("1.2.3.4"); // "1.2.3-4"
220+
* formatVersion("1.2.3.4.5"); // "1.2.3-45"
221+
* formatVersion("1.2"); // "1.2"
222+
* ```
223+
*/
224+
export function formatVersion(version: string): string {
225+
const digits = version.split('.')
226+
if (digits.length < 3) return version
227+
const [majar, minor, patch, ...noise] = digits
228+
return `${majar}.${minor}.${patch}${noise.length ? `-${noise.join('')}` : ''}`
204229
}

0 commit comments

Comments
 (0)