From 080fd629b0863fadf7f5f8a59b04e3c78976c3d2 Mon Sep 17 00:00:00 2001 From: Carbon Date: Sun, 29 Nov 2026 22:38:00 +0800 Subject: [PATCH] fix(cli): handle #ref fragments in GitHub template URLs for vp create --- .../cli/src/create/__tests__/discovery.spec.ts | 10 ++++++++++ packages/cli/src/create/discovery.ts | 14 +++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/packages/cli/src/create/__tests__/discovery.spec.ts b/packages/cli/src/create/__tests__/discovery.spec.ts index 3350465228..7cbdddc62e 100644 --- a/packages/cli/src/create/__tests__/discovery.spec.ts +++ b/packages/cli/src/create/__tests__/discovery.spec.ts @@ -287,11 +287,21 @@ describe('GitHub template helpers', () => { it('should parse GitHub https URLs', () => { expect(parseGitHubUrl('https://github.com/user/repo')).toBe('user/repo'); expect(parseGitHubUrl('https://github.com/user/repo.git')).toBe('user/repo'); + expect(parseGitHubUrl('https://github.com/user/repo#v2.0.0')).toBe('user/repo#v2.0.0'); + expect(parseGitHubUrl('https://github.com/user/repo.git#feat/auth')).toBe( + 'user/repo#feat/auth', + ); }); it('should infer the repository name from GitHub templates', () => { expect(inferGitHubRepoName('github:nkzw-tech/fate-template')).toBe('fate-template'); expect(inferGitHubRepoName('https://github.com/nkzw-tech/fate-template')).toBe('fate-template'); + expect(inferGitHubRepoName('github:nkzw-tech/fate-template#v2.0.0')).toBe('fate-template'); + expect(inferGitHubRepoName('github:nkzw-tech/fate-template#feat/auth')).toBe('fate-template'); + expect(inferGitHubRepoName('github:sxzz/tsdown-templates/vite-plus#main')).toBe('vite-plus'); + expect(inferGitHubRepoName('https://github.com/nkzw-tech/fate-template.git#feat/auth')).toBe( + 'fate-template', + ); }); it('should resolve GitHub templates to degit without reusing the original URL as destination', () => { diff --git a/packages/cli/src/create/discovery.ts b/packages/cli/src/create/discovery.ts index 69705d4e9f..ee2e5e9aa1 100644 --- a/packages/cli/src/create/discovery.ts +++ b/packages/cli/src/create/discovery.ts @@ -25,10 +25,14 @@ export function parseGitHubUrl(url: string): string | null { return url.slice(7); } + const hashIndex = url.indexOf('#'); + const ref = hashIndex === -1 || hashIndex === url.length - 1 ? '' : url.slice(hashIndex); + const urlWithoutRef = hashIndex === -1 ? url : url.slice(0, hashIndex); + // https://github.com/user/repo → user/repo - const match = url.match(/github\.com\/([^/]+\/[^/]+)/); + const match = urlWithoutRef.match(/github\.com\/([^/]+\/[^/]+)/); if (match) { - return match[1].replace(/\.git$/, ''); + return `${match[1].replace(/\.git$/, '')}${ref}`; } return null; @@ -40,7 +44,11 @@ export function inferGitHubRepoName(templateName: string): string | null { return null; } - const repoName = degitPath.split('/').pop(); + const pathWithoutRef = degitPath + .split('#')[0] + .replace(/\/+$/, '') + .replace(/\.git$/, ''); + const repoName = pathWithoutRef.split('/').pop(); return repoName || null; }