repos / neovimcraft

website that makes it easy to find neovim plugins
git clone https://github.com/neurosnap/neovimcraft.git

commit
a3a3e11
parent
622fb2a
author
Eric Bower
date
2021-12-13 17:06:22 +0000 UTC
use github api to fetch readme url fixes #19
1 files changed,  +19, -30
M scripts/process.ts
+19, -30
 1@@ -50,35 +50,6 @@ async function processMissingResources() {
 2   return { plugins, markdown };
 3 }
 4 
 5-async function fetchReadme({
 6-  username,
 7-  repo,
 8-  branch,
 9-}: Props & { branch: string }): Promise<Resp<string>> {
10-  const url = `https://raw.githubusercontent.com/${username}/${repo}/${branch}/README.md`;
11-  console.log(`Fetching ${url}`);
12-  const res = await fetch(url);
13-  if (res.ok) {
14-    const data = await res.text();
15-    return { ok: true, data };
16-  }
17-
18-  const lowerUrl = `https://raw.githubusercontent.com/${username}/${repo}/${branch}/readme.md`;
19-  const lowerRes = await fetch(lowerUrl);
20-  if (lowerRes.ok) {
21-    const data = await lowerRes.text();
22-    return { ok: true, data };
23-  }
24-
25-  return {
26-    ok: false,
27-    data: {
28-      status: res.status,
29-      error: new Error(`Could not load ${url}`),
30-    },
31-  };
32-}
33-
34 async function delay(ms: number): Promise<void> {
35   return new Promise((resolve) => setTimeout(() => resolve(), ms));
36 }
37@@ -119,6 +90,25 @@ async function githubApi(endpoint: string): Promise<Resp<{ [key: string]: any }>
38   };
39 }
40 
41+async function fetchReadme({ username, repo }: Props): Promise<Resp<string>> {
42+  const result = await githubApi(`/repos/${username}/${repo}/readme`);
43+  if (!result.ok) {
44+    return {
45+      ok: false,
46+      data: result.data as any,
47+    };
48+  }
49+
50+  const url = result.data.download_url;
51+  console.log(`Fetching ${url}`);
52+  const readme = await fetch(url);
53+  const data = await readme.text();
54+  return {
55+    ok: true,
56+    data,
57+  };
58+}
59+
60 async function fetchRepo({ username, repo }: Props): Promise<Resp<{ [key: string]: any }>> {
61   const result = await githubApi(`/repos/${username}/${repo}`);
62   return result;
63@@ -160,7 +150,6 @@ async function fetchGithubData(props: Props): Promise<Resp<any>> {
64   const readme = await fetchReadme({
65     username: props.username,
66     repo: props.repo,
67-    branch: repo.data.default_branch,
68   });
69   if (readme.ok === false) {
70     console.log(`${readme.data.status}: ${readme.data.error.message}`);