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

fix: ignore untracked file errors, attempt 2 #371

Merged
merged 2 commits into from
May 3, 2024
Merged
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
23 changes: 15 additions & 8 deletions docusaurus.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ import footer from "./config/footer.config";
import { env } from "process";
import { Config } from "@docusaurus/types";
import { Options } from "@docusaurus/plugin-content-docs";
import { getFileCommitHash } from "@docusaurus/utils/src/gitUtils";
import { AUTHOR_FALLBACK, AuthorData, commitCache, cacheAuthorData } from "./src/util/authorUtils";
import {
AUTHOR_FALLBACK,
AuthorData,
commitCache,
cacheAuthorData,
getFileCommitHashSafe,
} from "./src/util/authorUtils";

const preview = env.VERCEL_ENV === "preview";
cacheAuthorData(preview || process.env.NODE_ENV === "development");
Expand Down Expand Up @@ -89,12 +94,14 @@ const config: Config = {
...AUTHOR_FALLBACK,
};
if (process.env.NODE_ENV !== "development") {
const { commit } = await getFileCommitHash(params.filePath);
const username = commitCache.get(commit);
author = {
commit,
username: username ?? AUTHOR_FALLBACK.username,
};
const data = await getFileCommitHashSafe(params.filePath);
if (data) {
const username = commitCache.get(data.commit);
author = {
commit: data.commit,
username: username ?? AUTHOR_FALLBACK.username,
};
}
}

return {
Expand Down
26 changes: 14 additions & 12 deletions src/util/authorUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ async function cacheUsernameFromCommit(commit: string) {
}
}

export const getFileCommitHashSafe = async (file: string): Promise<{ commit: string } | null> => {
try {
return await getFileCommitHash(file);
} catch (e) {
if (e instanceof FileNotTrackedError) {
return null;
}

throw e; // different error, rethrow
}
};

export async function cacheAuthorData(isPreview: boolean) {
// Only Render in Production and not cache in every invocation of importing docusaurus.config.ts
if (isPreview || !new Error().stack.includes("async loadSite")) {
Expand All @@ -59,18 +71,8 @@ export async function cacheAuthorData(isPreview: boolean) {
}

const pagesFiles = await Globby("docs/**/*.md*");
const commits = await Promise.all(
pagesFiles.map((f) => {
return getFileCommitHash(f).catch((e) => {
if (e instanceof FileNotTrackedError) {
return null;
}

throw e; // different error, rethrow
});
})
);

const commits = await Promise.all(pagesFiles.map(getFileCommitHashSafe));
const commitsSet = new Set(commits.filter(Boolean).map((value) => value.commit));

await Promise.all(Array.from(commitsSet).map(cacheUsernameFromCommit));
}