11import type { Models } from '@appwrite.io/console' ;
22
33/**
4- * Build VCS repo URL from the template response model.
5- * Example (GitHub): https://github.com/appwrite/templates-for-sites
4+ * build VCS repo URL from the template response model.
5+ * supports GitHub, GitLab, and Bitbucket.
6+ *
7+ * important: We use 'master' as the branch name because GitHub (and other providers)
8+ * redirect 'master' to the repository's default branch, regardless of whether
9+ * its actually named 'main', 'master', or something else. This ensures the
10+ * link works across all repositories without needing to know their default branch.
11+ *
12+ * Example (GitHub): https://github.com/appwrite/templates-for-sites/tree/master/sveltekit/starter
613 */
714export function getTemplateSourceUrl (
815 t : Models . TemplateSite | Models . TemplateFunction
@@ -20,7 +27,41 @@ export function getTemplateSourceUrl(
2027 bitbucket : 'bitbucket.org'
2128 } ;
2229
23- const host = hostMap [ provider . toLowerCase ( ) ] ?? provider ; // fallback
30+ const host = hostMap [ provider . toLowerCase ( ) ] ;
31+ if ( ! host ) return null ;
2432
25- return `https://${ host } /${ owner } /${ repo } ` ;
33+ let folderPath : string | undefined ;
34+ if (
35+ 'providerRootDirectory' in t &&
36+ t . providerRootDirectory &&
37+ typeof t . providerRootDirectory === 'string'
38+ ) {
39+ folderPath = t . providerRootDirectory ;
40+ } else if (
41+ 'frameworks' in t &&
42+ t . frameworks ?. length > 0 &&
43+ t . frameworks [ 0 ] ?. providerRootDirectory &&
44+ typeof t . frameworks [ 0 ] . providerRootDirectory === 'string'
45+ ) {
46+ folderPath = t . frameworks [ 0 ] . providerRootDirectory ;
47+ }
48+
49+ let url = `https://${ host } /${ owner } /${ repo } ` ;
50+
51+ if ( folderPath ) {
52+ const normalizedPath = folderPath . replace ( / ^ \/ + | \/ + $ / g, '' ) ;
53+ if ( normalizedPath ) {
54+ const providerLower = provider . toLowerCase ( ) ;
55+ // Use 'master' as branch name - GitHub/GitLab/Bitbucket redirect it to default branch
56+ if ( providerLower === 'github' ) {
57+ url = `${ url } /tree/master/${ normalizedPath } ` ;
58+ } else if ( providerLower === 'gitlab' ) {
59+ url = `${ url } /-/tree/master/${ normalizedPath } ` ;
60+ } else if ( providerLower === 'bitbucket' ) {
61+ url = `${ url } /src/master/${ normalizedPath } ` ;
62+ }
63+ }
64+ }
65+
66+ return url ;
2667}
0 commit comments