|
|
@@ -413,31 +413,20 @@ async function getVSCodeVersions(): Promise<VersionResult> {
|
|
|
// 当前使用的 Git 镜像
|
|
|
let currentGitMirror: GitMirrorType = DEFAULT_GIT_MIRROR
|
|
|
|
|
|
-// 自定义镜像 URL(用户输入)
|
|
|
-let customGitMirrorUrl: string | null = null
|
|
|
-
|
|
|
/**
|
|
|
* 设置 Git 镜像
|
|
|
*/
|
|
|
export function setGitMirror(mirror: GitMirrorType): void {
|
|
|
currentGitMirror = mirror
|
|
|
- customGitMirrorUrl = null
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * 设置自定义 Git 镜像 URL
|
|
|
- * @param url 自定义镜像 URL,格式应包含 {version} 和 {arch} 占位符
|
|
|
- * 例如: https://example.com/git/{version}/Git-{version}-{arch}-bit.exe
|
|
|
- */
|
|
|
-export function setCustomGitMirrorUrl(url: string | null): void {
|
|
|
- customGitMirrorUrl = url
|
|
|
+ // 清除版本缓存,以便重新获取
|
|
|
+ clearCache()
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取当前 Git 镜像配置
|
|
|
*/
|
|
|
-export function getGitMirrorConfig(): { mirror: GitMirrorType; customUrl: string | null } {
|
|
|
- return { mirror: currentGitMirror, customUrl: customGitMirrorUrl }
|
|
|
+export function getGitMirrorConfig(): { mirror: GitMirrorType } {
|
|
|
+ return { mirror: currentGitMirror }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -445,20 +434,11 @@ export function getGitMirrorConfig(): { mirror: GitMirrorType; customUrl: string
|
|
|
*/
|
|
|
export function getGitDownloadUrl(version: string): string {
|
|
|
const arch = os.arch() === 'x64' ? '64' : '32'
|
|
|
-
|
|
|
- // 如果有自定义 URL,使用自定义 URL
|
|
|
- if (customGitMirrorUrl) {
|
|
|
- return customGitMirrorUrl
|
|
|
- .replace('{version}', version)
|
|
|
- .replace('{arch}', arch)
|
|
|
- }
|
|
|
-
|
|
|
- // 使用预设镜像
|
|
|
const mirror = GIT_MIRRORS[currentGitMirror]
|
|
|
return mirror.getDownloadUrl(version, arch)
|
|
|
}
|
|
|
|
|
|
-// 备用版本列表(当无法从 API 获取时使用)
|
|
|
+// 备用版本列表(当无法从任何源获取时使用)
|
|
|
const FALLBACK_GIT_VERSIONS = [
|
|
|
'2.47.1', '2.47.0',
|
|
|
'2.46.2', '2.46.1', '2.46.0',
|
|
|
@@ -469,8 +449,7 @@ const FALLBACK_GIT_VERSIONS = [
|
|
|
]
|
|
|
|
|
|
/**
|
|
|
- * 从 Git for Windows 官方 GitHub API 获取版本列表
|
|
|
- * API: https://api.github.com/repos/git-for-windows/git/releases
|
|
|
+ * 从 GitHub API 获取版本列表
|
|
|
*/
|
|
|
async function getGitVersionsFromGitHub(): Promise<string[]> {
|
|
|
try {
|
|
|
@@ -479,7 +458,7 @@ async function getGitVersionsFromGitHub(): Promise<string[]> {
|
|
|
'Accept': 'application/vnd.github.v3+json',
|
|
|
'User-Agent': 'ApqInstaller'
|
|
|
},
|
|
|
- signal: AbortSignal.timeout(10000) // 10秒超时
|
|
|
+ signal: AbortSignal.timeout(10000)
|
|
|
})
|
|
|
|
|
|
if (!response.ok) {
|
|
|
@@ -490,17 +469,10 @@ async function getGitVersionsFromGitHub(): Promise<string[]> {
|
|
|
const versions: string[] = []
|
|
|
|
|
|
for (const release of releases) {
|
|
|
- // 跳过预发布版本
|
|
|
if (release.prerelease) continue
|
|
|
-
|
|
|
- // tag_name 格式: v2.47.1.windows.1
|
|
|
const match = release.tag_name.match(/^v?(\d+\.\d+\.\d+)/)
|
|
|
- if (match) {
|
|
|
- const version = match[1]
|
|
|
- // 避免重复版本
|
|
|
- if (!versions.includes(version)) {
|
|
|
- versions.push(version)
|
|
|
- }
|
|
|
+ if (match && !versions.includes(match[1])) {
|
|
|
+ versions.push(match[1])
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -511,11 +483,66 @@ async function getGitVersionsFromGitHub(): Promise<string[]> {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * 从华为云镜像获取版本列表
|
|
|
+ * 通过解析目录页面获取可用版本
|
|
|
+ */
|
|
|
+async function getGitVersionsFromHuaweicloud(): Promise<string[]> {
|
|
|
+ try {
|
|
|
+ const response = await fetch('https://mirrors.huaweicloud.com/git-for-windows/', {
|
|
|
+ headers: { 'User-Agent': 'ApqInstaller' },
|
|
|
+ signal: AbortSignal.timeout(10000)
|
|
|
+ })
|
|
|
+
|
|
|
+ if (!response.ok) {
|
|
|
+ throw new Error(`华为云镜像请求失败: ${response.status}`)
|
|
|
+ }
|
|
|
+
|
|
|
+ const html = await response.text()
|
|
|
+ const versions: string[] = []
|
|
|
+
|
|
|
+ // 解析 HTML 页面,查找版本目录链接
|
|
|
+ // 格式: <a href="v2.47.1.windows.1/">v2.47.1.windows.1/</a>
|
|
|
+ const regex = /href="v(\d+\.\d+\.\d+)\.windows\.\d+\/"/g
|
|
|
+ let match
|
|
|
+ while ((match = regex.exec(html)) !== null) {
|
|
|
+ const version = match[1]
|
|
|
+ if (!versions.includes(version)) {
|
|
|
+ versions.push(version)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 按版本号降序排序
|
|
|
+ versions.sort((a, b) => {
|
|
|
+ const partsA = a.split('.').map(Number)
|
|
|
+ const partsB = b.split('.').map(Number)
|
|
|
+ for (let i = 0; i < 3; i++) {
|
|
|
+ if (partsA[i] !== partsB[i]) {
|
|
|
+ return partsB[i] - partsA[i]
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return 0
|
|
|
+ })
|
|
|
+
|
|
|
+ return versions
|
|
|
+ } catch (error) {
|
|
|
+ console.error('从华为云镜像获取 Git 版本失败:', error)
|
|
|
+ return []
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
async function getGitVersionsWindows(): Promise<VersionResult> {
|
|
|
const versionsByMajor = new Map<string, VersionItem[]>()
|
|
|
|
|
|
- // 尝试从 GitHub API 获取版本列表
|
|
|
- let versions = await getGitVersionsFromGitHub()
|
|
|
+ // 根据当前镜像源获取版本列表
|
|
|
+ let versions: string[] = []
|
|
|
+
|
|
|
+ if (currentGitMirror === 'github') {
|
|
|
+ versions = await getGitVersionsFromGitHub()
|
|
|
+ } else {
|
|
|
+ // 华为云镜像
|
|
|
+ versions = await getGitVersionsFromHuaweicloud()
|
|
|
+ }
|
|
|
|
|
|
// 如果获取失败,使用备用版本列表
|
|
|
if (versions.length === 0) {
|
|
|
@@ -533,8 +560,7 @@ async function getGitVersionsWindows(): Promise<VersionResult> {
|
|
|
throw new Error(ERROR_MESSAGES.VERSION_FETCH_ERROR)
|
|
|
}
|
|
|
|
|
|
- // 获取当前镜像名称
|
|
|
- const mirrorName = customGitMirrorUrl ? '自定义镜像' : GIT_MIRRORS[currentGitMirror].name
|
|
|
+ const mirrorName = GIT_MIRRORS[currentGitMirror].name
|
|
|
|
|
|
return {
|
|
|
versions: buildVersionList(versionsByMajor, {
|