Skip to content

Commit

Permalink
修正从老版本升级上来后出现的报错 (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
riophae authored and LitoMore committed Sep 12, 2019
1 parent f690a19 commit 251a786
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/background/environment/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import storage from './storage'
import features from '@features'
import isFanfouWebUrl from '@libs/isFanfouWebUrl'
import migrate from '@libs/migrate'
import isExtensionUpgraded from '@libs/isExtensionUpgraded'
import { readJSONFromLocalStorage } from '@libs/localStorageWrappers'
import getExtensionVersion from '@libs/getExtensionVersion'
import omitBy from '@libs/omitBy'
Expand Down Expand Up @@ -189,10 +190,6 @@ async function checkIfExtensionUpgraded() {
PREVIOUS_EXTENSION_VERSION_STORAGE_AREA_NAME,
)
const currentVersion = getExtensionVersion()
const isExtensionUpgraded = (
!!previousVersion &&
semver.gt(currentVersion, previousVersion)
)

await storage.write(
PREVIOUS_EXTENSION_VERSION_STORAGE_KEY,
Expand All @@ -201,7 +198,7 @@ async function checkIfExtensionUpgraded() {
)
await storage.write(
STORAGE_KEY_IS_EXTENSION_UPGRADED,
isExtensionUpgraded,
isExtensionUpgraded(previousVersion, currentVersion),
STORAGE_AREA_NAME_IS_EXTENSION_UPGRADED,
)
}
Expand Down
10 changes: 10 additions & 0 deletions src/libs/isExtensionUpgraded.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import semver from 'semver'
import isLegacyVersion from '@libs/isLegacyVersion'

export default (previousVersion, currentVersion) => {
// 新安装,没有检查到旧版本号
if (!previousVersion) return false
// 检查到老的 '0.x.x.x' 版本号,但是这个不能直接传给 semver,会报错
if (isLegacyVersion(previousVersion)) return true
return semver.gt(currentVersion, previousVersion)
}
8 changes: 8 additions & 0 deletions src/libs/isExtensionUpgraded.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import isExtensionUpgraded from './isExtensionUpgraded'

test('isExtensionUpgraded', () => {
expect(isExtensionUpgraded(null, '1.0.0')).toBe(false)
expect(isExtensionUpgraded('0.9.8.9', '1.0.0')).toBe(true)
expect(isExtensionUpgraded('1.0.0', '1.0.0')).toBe(false)
expect(isExtensionUpgraded('1.0.0', '1.0.1')).toBe(true)
})
5 changes: 5 additions & 0 deletions src/libs/isLegacyVersion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// 判断版本号是否为 '0.x.x.x' 的格式
export default version => (
typeof version === 'string' &&
/^0\.\d\.\d\.\d$/.test(version)
)
6 changes: 6 additions & 0 deletions src/libs/isLegacyVersion.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import isLegacyVersion from './isLegacyVersion'

test('isLegacyVersion', () => {
expect(isLegacyVersion('0.9.8.9')).toBe(true)
expect(isLegacyVersion('1.0.0')).toBe(false)
})

0 comments on commit 251a786

Please sign in to comment.