Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

gitCafeHosts.ts

39 lines1.3 KB

1export const GITCAFE_HOSTS = ["git.cafe", "staging.git.cafe"] as const;
2export type GitCafeHost = (typeof GITCAFE_HOSTS)[number];
3​
4export function gitCafeHost(host: string): GitCafeHost | null {
5 const normalized = host.trim().toLowerCase();
6 return GITCAFE_HOSTS.find((candidate) => candidate === normalized) ?? null;
7}
8​
9export function isGitCafeHost(host: string): boolean {
10 return gitCafeHost(host) !== null;
12​
13const REPOSITORY = /^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/u;
14const REMOTE_PATTERNS = [
15 /^https?:\/\/(?:[^@/]+@)?([^/:]+)(?::\d+)?\/(.+)$/iu,
16 /^ssh:\/\/[^@/]+@([^/:]+)(?::\d+)?\/(.+)$/iu,
17 /^(?:[^@/:]+@)?([^/:]+):(.+)$/u,
18];
19​
20export function gitCafeRepositoryPath(path: string): string | null {
21 const repository = path
22 .trim()
23 .replace(/\/+$/u, "")
24 .replace(/\.git$/u, "");
25 return REPOSITORY.test(repository) ? repository : null;
27​
28export function parseGitCafeRemote(
29 url: string,
30): { readonly host: GitCafeHost; readonly repository: string } | null {
31 for (const pattern of REMOTE_PATTERNS) {
32 const match = pattern.exec(url.trim());
33 if (!match?.[1] || !match[2]) continue;
34 const host = gitCafeHost(match[1]);
35 const repository = gitCafeRepositoryPath(match[2]);
36 return host && repository ? { host, repository } : null;
37 }
38 return null;