|
| 1 | +import https from "https"; |
| 2 | +import {rootPath} from "#common/utils/file/getRootPath.js"; |
| 3 | +import fs from "fs"; |
| 4 | +import path from "path"; |
| 5 | +import {GITHUB_HOST, GITHUB_RAW, NPM_URL, PackageName} from "#common/constants/question.const.js"; |
| 6 | +import {url_join} from "#common/utils/http/urlJoin.js"; |
| 7 | +import {fetch_} from "#common/utils/http/fetch_.js"; |
| 8 | +// npm 中的 包地址 |
| 9 | +const npmUrl = url_join(NPM_URL,PackageName); |
| 10 | +const githubUrl = url_join(GITHUB_RAW,GITHUB_HOST,PackageName,"master/package.json"); |
| 11 | +/** |
| 12 | + * 获取远端npm库中的版本号 |
| 13 | + */ |
| 14 | +export const getNpmVersion = async ()=>{ |
| 15 | + try{ |
| 16 | + const res = await fetch_(npmUrl,{method:"GET"}); |
| 17 | + console.log("获取NPM版本成功!======",res['dist-tags']?.latest) |
| 18 | + return res['dist-tags']?.latest; |
| 19 | + } |
| 20 | + catch(e){ |
| 21 | + console.log("获取NPM版本失败!") |
| 22 | + throw new Error(e); |
| 23 | + } |
| 24 | +} |
| 25 | +/** |
| 26 | + * 获取github的最新提交sha |
| 27 | + * @returns {Promise<unknown>} |
| 28 | + */ |
| 29 | +export const getGithubVersion = async ()=>{ |
| 30 | + try{ |
| 31 | + const res = await fetch_(githubUrl,{method:"GET"}); |
| 32 | + console.log("获取Github版本成功!======",res?.version) |
| 33 | + return res?.version; |
| 34 | + } |
| 35 | + catch(e){ |
| 36 | + console.log("获取Github版本失败!",e) |
| 37 | + throw new Error(e); |
| 38 | + } |
| 39 | +} |
| 40 | +export const getLocalVersion = ()=>{ |
| 41 | + console.log("开始获取本地版本号...") |
| 42 | + try{ |
| 43 | + const {version} = JSON.parse(fs.readFileSync(path.resolve(rootPath,'package.json'),"utf-8")) |
| 44 | + console.log("本地版本号获取成功!") |
| 45 | + return version; |
| 46 | + } |
| 47 | + catch (e){ |
| 48 | + console.log("本地版本号获取失败!") |
| 49 | + return false; |
| 50 | + } |
| 51 | +} |
| 52 | +/** |
| 53 | + * 检测整体的更新状况 |
| 54 | + * @returns {Promise<{localVersion: (any|boolean), githubVersion: *, isCliUpdate: boolean, remoteVersion: unknown, isGithubUpdate: boolean}>} |
| 55 | + */ |
| 56 | +export const checkUpdate = async ()=>{ |
| 57 | + const remote = await getNpmVersion(); |
| 58 | + const github = await getGithubVersion() |
| 59 | + const local = getLocalVersion(); |
| 60 | + return { |
| 61 | + localVersion: local, |
| 62 | + npmVersion: remote, |
| 63 | + githubVersion: github, |
| 64 | + isCliUpdate : remote !== local, |
| 65 | + isGithubUpdate: github !== local |
| 66 | + }; |
| 67 | +} |
0 commit comments