Created
May 20, 2022 01:49
-
-
Save ryanflorence/3de6bf069efa2c56427e770f0d35da02 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fsp from "fs/promises"; | |
import invariant from "tiny-invariant"; | |
import path from "path"; | |
/** | |
* Fetches the contents of a file in a repository or from your local disk. | |
* | |
* @param ref The GitHub ref, use `"local"` for local docs development | |
* @param filepath The filepath inside the repo (including "docs/") | |
* @returns The text of the file | |
*/ | |
export async function getRepoContent( | |
repoPair: string, | |
ref: string, | |
filepath: string | |
): Promise<string | null> { | |
if (ref === "local") return getLocalContent(filepath); | |
let [owner, repo] = repoPair.split("/"); | |
let pathname = `/${owner}/${repo}/${ref}/${filepath}`; | |
let response = await fetch( | |
new URL(pathname, "https://raw.githubusercontent.com/").href, | |
{ headers: { "User-Agent": `docs:${owner}/${repo}` } } | |
); | |
if (!response.ok) return null; | |
return response.text(); | |
} | |
/** | |
* Reads a single file from your local source repository | |
*/ | |
async function getLocalContent(filepath: string): Promise<string> { | |
invariant( | |
process.env.LOCAL_REPO_RELATIVE_PATH, | |
"Expected LOCAL_REPO_RELATIVE_PATH" | |
); | |
let localFilePath = path.join(process.env.LOCAL_REPO_RELATIVE_PATH, filepath); | |
let content = await fsp.readFile(localFilePath); | |
return content.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment