forked from npmx-dev/npmx.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseRepositoryUrl.ts
More file actions
37 lines (28 loc) · 855 Bytes
/
useRepositoryUrl.ts
File metadata and controls
37 lines (28 loc) · 855 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { joinURL, withoutTrailingSlash } from 'ufo'
type RequestedVersion = SlimPackument['requestedVersion'] | null
type UseRepositoryUrlReturn = {
repositoryUrl: ComputedRef<string | null>
}
export function useRepositoryUrl(
requestedVersion: MaybeRefOrGetter<RequestedVersion>,
): UseRepositoryUrlReturn {
const repositoryUrl = computed<string | null>(() => {
const repo = toValue(requestedVersion)?.repository
if (!repo?.url) {
return null
}
let url = normalizeGitUrl(repo.url)
if (!url) {
return null
}
// Fix: Strip trailing .git (and any leftovers) before constructing the directory link
url = url.replace(/\.git\/?$/, '')
if (repo.directory) {
url = joinURL(`${withoutTrailingSlash(url)}/tree/HEAD`, repo.directory)
}
return url
})
return {
repositoryUrl,
}
}