@@ -2,15 +2,14 @@ import https from "https";
2
2
import { rootPath } from "#common/utils/file/getRootPath.js" ;
3
3
import fs from "fs" ;
4
4
import path from "path" ;
5
- import { log } from "console" ;
6
-
7
5
const npmUrl = `https://registry.npmjs.org/leetcode-practice` ;
8
- const githubUrl = `https://api.github .com/repos/ wh131462/leetcode-practice/commits?per_page=1 ` ;
6
+ const githubUrl = `https://raw.githubusercontent .com/wh131462/leetcode-practice/master/package.json ` ;
9
7
/**
10
8
* 获取远端npm库中的版本号
11
9
*/
12
- export const getRemoteVersion = ( ) => {
10
+ export const getNpmVersion = ( ) => {
13
11
return new Promise ( ( resolve , reject ) => {
12
+ console . log ( "开始获取npm仓库中的版本号..." )
14
13
https . get ( npmUrl , ( res ) => {
15
14
let data = '' ;
16
15
res . on ( 'data' , ( chunk ) => {
@@ -20,12 +19,15 @@ export const getRemoteVersion = ()=>{
20
19
try {
21
20
const packageInfo = JSON . parse ( data ) ;
22
21
const latestVersion = packageInfo [ 'dist-tags' ] . latest ;
22
+ console . log ( "npm仓库中的版本号获取成功!" )
23
23
resolve ( latestVersion ) ;
24
24
} catch ( error ) {
25
+ console . log ( "npm仓库中的版本号获取失败!" )
25
26
reject ( error ) ;
26
27
}
27
28
} ) ;
28
29
} ) . on ( 'error' , ( error ) => {
30
+ console . log ( "npm仓库中的版本号获取失败!" )
29
31
reject ( error ) ;
30
32
} ) ;
31
33
} ) ;
@@ -36,47 +38,53 @@ export const getRemoteVersion = ()=>{
36
38
*/
37
39
export const getGithubVersion = ( ) => {
38
40
return new Promise ( ( resolve , reject ) => {
39
- https . get ( githubUrl , {
40
- headers : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"
41
- } , ( res ) => {
41
+ console . log ( "开始获取github仓库的版本号..." )
42
+ https . get ( githubUrl , ( res ) => {
42
43
let data = '' ;
43
44
res . on ( 'data' , ( chunk ) => {
44
45
data += chunk ;
45
46
} ) ;
46
47
res . on ( 'end' , ( ) => {
47
48
try {
48
- console . log ( data ) ;
49
49
const jsonData = JSON . parse ( data ) ;
50
- const latestCommitSha = jsonData [ 0 ] . sha ;
51
- console . log ( latestCommitSha )
52
- resolve ( latestCommitSha ) ;
50
+ console . log ( "github仓库的版本号获取成功!" )
51
+ resolve ( jsonData . version ) ;
53
52
} catch ( error ) {
53
+ console . log ( "github仓库的版本号获取失败!" )
54
54
reject ( error ) ;
55
55
}
56
56
} ) ;
57
57
} ) . on ( 'error' , ( error ) => {
58
+ console . log ( "github仓库的版本号获取失败!" )
58
59
reject ( error ) ;
59
60
} ) ;
60
61
} ) ;
61
62
}
62
63
export const getLocalVersion = ( ) => {
64
+ console . log ( "开始获取本地版本号..." )
63
65
try {
64
66
const { version} = JSON . parse ( fs . readFileSync ( path . resolve ( rootPath , 'package.json' ) , "utf-8" ) )
67
+ console . log ( "本地版本号获取成功!" )
65
68
return version ;
66
69
}
67
70
catch ( e ) {
71
+ console . log ( "本地版本号获取失败!" )
68
72
return false ;
69
73
}
70
74
}
71
-
75
+ /**
76
+ * 检测整体的更新状况
77
+ * @returns {Promise<{localVersion: (any|boolean), githubVersion: *, isCliUpdate: boolean, remoteVersion: unknown, isGithubUpdate: boolean}> }
78
+ */
72
79
export const checkUpdate = async ( ) => {
73
- const remote = await getRemoteVersion ( ) ;
74
- const local = getLocalVersion ( ) ;
80
+ const remote = await getNpmVersion ( ) ;
75
81
const github = await getGithubVersion ( )
76
- console . log ( github )
82
+ const local = getLocalVersion ( ) ;
77
83
return {
78
84
localVersion : local ,
79
- remoteVersion : remote ,
80
- isUpdate : remote !== local
85
+ npmVersion : remote ,
86
+ githubVersion : github ,
87
+ isCliUpdate : remote !== local ,
88
+ isGithubUpdate : github !== local
81
89
} ;
82
90
}
0 commit comments