11import axios from 'axios' ;
22
3- // 一个辅助函数,用于比较两个版本号,考虑非数字部分
4- function isVersionGreater ( onlineVersion : string , currentVersion : string ) {
5- // 正则表达式用于分离数字部分和后缀
6- const versionRegex = / ( \d + \. \d + \. \d + ) ( .* ) / ;
7- const [ , onlineMain , onlineSuffix ] = onlineVersion . match ( versionRegex ) || [ ] ;
8- const [ , currentMain , currentSuffix ] =
3+ export const isVersionGreater = (
4+ onlineVersion : string ,
5+ currentVersion : string ,
6+ ) => {
7+ // 更新正则表达式,更好地分离数字和后缀部分
8+ const versionRegex = / ( \d + \. \d + \. \d + ) ( - ? [ ^ . ] * ) ( \. \d + ) ? / ;
9+ const [ , onlineMain , onlinePre , onlinePreNum ] =
10+ onlineVersion . match ( versionRegex ) || [ ] ;
11+ const [ , currentMain , currentPre , currentPreNum ] =
912 currentVersion . match ( versionRegex ) || [ ] ;
1013
11- // 先比较主版本号
14+ // 比较主版本号
1215 const onlineParts = onlineMain . split ( '.' ) . map ( Number ) ;
1316 const currentParts = currentMain . split ( '.' ) . map ( Number ) ;
1417
15- // eslint-disable-next-line no-plusplus
1618 for ( let i = 0 ; i < Math . max ( onlineParts . length , currentParts . length ) ; i ++ ) {
1719 const onlinePart = onlineParts [ i ] || 0 ;
1820 const currentPart = currentParts [ i ] || 0 ;
@@ -24,19 +26,25 @@ function isVersionGreater(onlineVersion: string, currentVersion: string) {
2426 }
2527 }
2628
27- // 如果主版本号相同,比较后缀,视 "beta" 等作为较小
28- if ( onlineSuffix && currentSuffix ) {
29- return onlineSuffix > currentSuffix ;
30- }
31- if ( onlineSuffix ) {
32- return false ; // online 有后缀但 current 无后缀,online 为较小版本
33- }
34- if ( currentSuffix ) {
35- return true ; // current 有后缀但 online 无后缀,online 为较大版本
29+ // 如果主版本号相同,先比较后缀是否存在(如果存在数字后缀的话)
30+ if ( onlinePre || currentPre ) {
31+ if ( ! onlinePre && currentPre ) return true ; // online 没有后缀,current 有后缀,online 更大
32+ if ( onlinePre && ! currentPre ) return false ; // online 有后缀,current 没有后缀,online 更小
33+ if ( onlinePre !== currentPre ) {
34+ return onlinePre > currentPre ; // 直接字符串比较后缀
35+ }
36+ // 如果后缀相同,比较数字后缀
37+ const onlinePreNumValue = onlinePreNum
38+ ? parseInt ( onlinePreNum . substring ( 1 ) , 10 )
39+ : 0 ;
40+ const currentPreNumValue = currentPreNum
41+ ? parseInt ( currentPreNum . substring ( 1 ) , 10 )
42+ : 0 ;
43+ return onlinePreNumValue > currentPreNumValue ;
3644 }
3745
38- return false ;
39- }
46+ return false ; // 如果主版本号和后缀完全相同,返回 false
47+ } ;
4048
4149export const getVersionInfo = async ( currentVersion : string ) => {
4250 let updates : { version : string ; url : string ; description : string } [ ] = [ ] ;
0 commit comments