-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathupdate.js
92 lines (90 loc) · 2.3 KB
/
update.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import fs from 'node:fs'
import path from 'node:path'
import { rootPath } from '#common/utils/file/getRootPath.js'
import {
GITEE_URL,
GITHUB_HOST,
GITHUB_RAW,
NPM_URL,
PackageName,
} from '#common/constants/question.const.js'
import { url_join } from '#common/utils/http/urlJoin.js'
import { fetch_ } from '#common/utils/http/fetch_.js'
// npm 中的 包地址
const npmUrl = url_join(NPM_URL, PackageName)
const githubUrl = url_join(
GITHUB_RAW,
GITHUB_HOST,
PackageName,
'master/package.json',
)
const giteeUrl = url_join(
GITEE_URL,
GITHUB_HOST,
PackageName,
'raw',
'master/package.json',
)
/**
* 获取远端npm库中的版本号
*/
export async function getNpmVersion() {
try {
const res = await fetch_(npmUrl, { method: 'GET' })
console.log('获取NPM版本成功!======', res['dist-tags']?.latest)
return res['dist-tags']?.latest
}
catch (e) {
console.log('获取NPM版本失败!')
throw new Error(e)
}
}
/**
* 获取github的最新提交sha
* @returns {Promise<unknown>}
*/
export async function getGithubVersion() {
try {
const [{ reason: _1, value: github }, { reason: _2, value: gitee }]
= await Promise.allSettled([
fetch_(githubUrl, { method: 'GET' }),
fetch_(giteeUrl, { method: 'GET' }),
])
const ver = github?.version ?? gitee?.version
console.log('获取Github版本成功!======', ver)
return ver
}
catch (e) {
console.log('获取Github版本失败!', e)
throw new Error(e)
}
}
export function getLocalVersion() {
try {
const { version } = JSON.parse(
fs.readFileSync(path.resolve(rootPath, 'package.json'), 'utf-8'),
)
console.log('本地版本号获取成功!======', version)
return version
}
catch (e) {
console.log('本地版本号获取失败!')
return false
}
}
/**
* 检测整体的更新状况
* @returns {Promise<{localVersion: (any|boolean), githubVersion: *, isCliUpdate: boolean, remoteVersion: unknown, isGithubUpdate: boolean}>}
*/
export async function checkUpdate() {
const remote = await getNpmVersion()
const github = await getGithubVersion()
const local = getLocalVersion()
return {
localVersion: local,
npmVersion: remote,
githubVersion: github,
isCliUpdate: remote !== local,
isGithubUpdate: github !== local,
}
}