Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/cli/src/create/__tests__/discovery.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
14 changes: 11 additions & 3 deletions packages/cli/src/create/discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down