Skip to content

Commit

Permalink
formatVersion to support chrome extension version format (`1.2.3.4444…
Browse files Browse the repository at this point in the history
…` -> `1.2.3-4444`)
  • Loading branch information
eric2788 committed Oct 27, 2024
1 parent af722d2 commit e711eb9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/background/update-listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { type MV2Settings } from '~migrations/schema'
import semver from 'semver'
import migrateFromMV2 from '~migrations'
import { getLatestRelease } from '~api/github'
import { formatVersion } from "~utils/misc"

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

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

const lastVersion = (await localStorage.get('last_version')) ?? '0.12.4'
const mv2 = await storage.get<MV2Settings>('settings')
if (mv2 && semver.lt(lastVersion, '2.0.0')) {
if (mv2 && semver.lt(formatVersion(lastVersion), '2.0.0')) {
try {
await migrateFromMV2()
await sendInternal('notify', {
Expand All @@ -67,7 +68,7 @@ getSettingStorage('settings.version').then(async (settings) => {
if (!settings.autoCheckUpdate) return
const currentVersion = chrome.runtime.getManifest().version
const latest = await getLatestRelease()
if (semver.lt(currentVersion, latest.tag_name)) {
if (semver.lt(formatVersion(currentVersion), formatVersion(latest.tag_name))) {
await sendInternal('notify', {
type: 'list',
title: 'bilibili-vup-stream-enhancer 已推出新版本',
Expand All @@ -84,5 +85,4 @@ getSettingStorage('settings.version').then(async (settings) => {
]
})
}
})

})
3 changes: 2 additions & 1 deletion src/options/fragments/version.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { StateProxy } from "~hooks/binding"
import SwitchListItem from "~options/components/SwitchListItem"
import type { ReleaseInfo } from "~types/github"
import semver from 'semver';
import { formatVersion } from "~utils/misc"


export type SettingSchema = {
Expand Down Expand Up @@ -65,7 +66,7 @@ function VersionSettings({ state, useHandler }: StateProxy<SettingSchema>): JSX.
{([current, last]: ReleaseInfo[]) => (
<div className="md:flex justify-between text-center md:text-left space-y-3 md:space-y-0">
<div>
{semver.lt(current.tag_name, last.tag_name) ? (
{semver.lt(formatVersion(current.tag_name), formatVersion(last.tag_name)) ? (
<Typography color="red" className="font-bold mb-4">
你有可用的更新
</Typography>
Expand Down
25 changes: 25 additions & 0 deletions src/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,29 @@ export function setNestedValue<T extends object, K extends Leaves<T>>(obj: T, pa
const lastKey = keys.pop()
const lastObj = keys.reduce((o, p) => o[p], obj)
lastObj[lastKey] = value
}



/**
* Formats a version string by ensuring it follows the major.minor.patch format.
* If there are additional segments beyond the patch version, they are appended
* as a hyphenated suffix.
*
* @param version - The version string to format.
* @returns The formatted version string.
*
* @example
* ```typescript
* formatVersion("1.2.3"); // "1.2.3"
* formatVersion("1.2.3.4"); // "1.2.3-4"
* formatVersion("1.2.3.4.5"); // "1.2.3-45"
* formatVersion("1.2"); // "1.2"
* ```
*/
export function formatVersion(version: string): string {
const digits = version.split('.')
if (digits.length < 3) return version
const [majar, minor, patch, ...noise] = digits
return `${majar}.${minor}.${patch}${noise.length ? `-${noise.join('')}` : ''}`
}

0 comments on commit e711eb9

Please sign in to comment.