Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move each provider to own file #177

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
11 changes: 10 additions & 1 deletion src/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { promisify } from "node:util";
import type { Agent } from "node:http";
import { relative, resolve } from "pathe";
import { fetch } from "node-fetch-native/proxy";
import type { GitInfo } from "./types";
import type { GitInfo, TemplateInfo } from "./types";

export async function download(
url: string,
Expand Down Expand Up @@ -136,3 +136,12 @@ export function startShell(cwd: string) {
stdio: "inherit",
});
}

export function defineProvider(
provider: (
input: string,
options: { auth?: string },
) => TemplateInfo | Promise<TemplateInfo> | null,
) {
return provider;
}
139 changes: 0 additions & 139 deletions src/providers.ts

This file was deleted.

15 changes: 15 additions & 0 deletions src/providers/bitbucket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { defineProvider, parseGitURI } from "../_utils";

export default defineProvider((input, options) => {
const parsed = parseGitURI(input);
return {
name: parsed.repo.replace("/", "-"),
version: parsed.ref,
subdir: parsed.subdir,
headers: {
authorization: options.auth ? `Bearer ${options.auth}` : undefined,
},
url: `https://bitbucket.com/${parsed.repo}/src/${parsed.ref}${parsed.subdir}`,
tar: `https://bitbucket.org/${parsed.repo}/get/${parsed.ref}.tar.gz`,
};
});
24 changes: 24 additions & 0 deletions src/providers/github.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { defineProvider, parseGitURI } from "../_utils";

export default defineProvider((input, options) => {
const parsed = parseGitURI(input);

// https://docs.github.com/en/rest/repos/contents#download-a-repository-archive-tar
// TODO: Verify solution for github enterprise
const githubAPIURL = process.env.GIGET_GITHUB_URL || "https://api.github.com";

return {
name: parsed.repo.replace("/", "-"),
version: parsed.ref,
subdir: parsed.subdir,
headers: {
Authorization: options.auth ? `Bearer ${options.auth}` : undefined,
Accept: "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
},
url: `${githubAPIURL.replace("api.github.com", "github.com")}/${
parsed.repo
}/tree/${parsed.ref}${parsed.subdir}`,
tar: `${githubAPIURL}/repos/${parsed.repo}/tarball/${parsed.ref}`,
};
});
18 changes: 18 additions & 0 deletions src/providers/gitlab.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { defineProvider, parseGitURI } from "../_utils";

export default defineProvider((input, options) => {
const parsed = parseGitURI(input);
const gitlab = process.env.GIGET_GITLAB_URL || "https://gitlab.com";
return {
name: parsed.repo.replace("/", "-"),
version: parsed.ref,
subdir: parsed.subdir,
headers: {
authorization: options.auth ? `Bearer ${options.auth}` : undefined,
// https://gitlab.com/gitlab-org/gitlab/-/commit/50c11f278d18fe1f3fb12eb595067216bb58ade2
"sec-fetch-mode": "same-origin",
},
url: `${gitlab}/${parsed.repo}/tree/${parsed.ref}${parsed.subdir}`,
tar: `${gitlab}/${parsed.repo}/-/archive/${parsed.ref}.tar.gz`,
};
});
61 changes: 61 additions & 0 deletions src/providers/http.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { basename } from "pathe";
import { debug, defineProvider, sendFetch } from "../_utils";
import { TemplateInfo } from "../types";

const _httpJSON = defineProvider(async (input, options) => {
const result = await sendFetch(input, {
validateStatus: true,
headers: {
authorization: options.auth ? `Bearer ${options.auth}` : undefined,
},
});
const info = (await result.json()) as TemplateInfo;
if (!info.tar || !info.name) {
throw new Error(
`Invalid template info from ${input}. name or tar fields are missing!`,
);
}
return info;
});

export default defineProvider(async (input, options) => {
if (input.endsWith(".json")) {
return (await _httpJSON(input, options)) as TemplateInfo;
}

const url = new URL(input);
let name: string = basename(url.pathname);

try {
const head = await sendFetch(url.href, {
method: "HEAD",
validateStatus: true,
headers: {
authorization: options.auth ? `Bearer ${options.auth}` : undefined,
},
});
const _contentType = head.headers.get("content-type") || "";
if (_contentType.includes("application/json")) {
return (await _httpJSON(input, options)) as TemplateInfo;
}
const filename = head.headers
.get("content-disposition")
?.match(/filename="?(.+)"?/)?.[1];
if (filename) {
name = filename.split(".")[0];
}
} catch (error) {
debug(`Failed to fetch HEAD for ${url.href}:`, error);
}

return {
name: `${name}-${url.href.slice(0, 8)}`,
version: "",
subdir: "",
tar: url.href,
defaultDir: name,
headers: {
Authorization: options.auth ? `Bearer ${options.auth}` : undefined,
},
};
});
16 changes: 16 additions & 0 deletions src/providers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TemplateProvider } from "../types";
import http from "./http";
import github from "./github";
import gitlab from "./gitlab";
import bitbucket from "./bitbucket";
import sourcehut from "./sourcehut";

export const providers: Record<string, TemplateProvider> = {
http,
https: http,
github,
gh: github,
gitlab,
bitbucket,
sourcehut,
};
15 changes: 15 additions & 0 deletions src/providers/sourcehut.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { defineProvider, parseGitURI } from "../_utils";

export default defineProvider((input, options) => {
const parsed = parseGitURI(input);
return {
name: parsed.repo.replace("/", "-"),
version: parsed.ref,
subdir: parsed.subdir,
headers: {
authorization: options.auth ? `Bearer ${options.auth}` : undefined,
},
url: `https://git.sr.ht/~${parsed.repo}/tree/${parsed.ref}/item${parsed.subdir}`,
tar: `https://git.sr.ht/~${parsed.repo}/archive/${parsed.ref}.tar.gz`,
};
});