Skip to content

Commit 0c6fa0a

Browse files
committed
implement suggestions from code review
- drop `axios` dependency - always throw if `TOKEN`s are unset - log errors to console - extract bot data logic to its own file - use bot's avatar as fallback for translators
1 parent 94390be commit 0c6fa0a

File tree

8 files changed

+53
-45
lines changed

8 files changed

+53
-45
lines changed

.vitepress/data/authors.data.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export default {
3232
const authorsArray = await Promise.all(
3333
Array.from(authors.values()).map(async (author) => {
3434
try {
35+
if (!GITHUB_TOKEN) throw new Error("GITHUB_TOKEN is unset")
3536
const { data } = await octokit.rest.users.getByUsername({
3637
username: author.login,
3738
});
@@ -43,26 +44,15 @@ export default {
4344
number: author.files,
4445
} as DefaultTheme.TeamMember & { number: number };
4546
} catch (error) {
46-
// Allows build without internet
47+
console.error("Error: ", error);
4748
return null;
4849
}
4950
})
5051
);
5152

52-
const { data } = await (async () => {
53-
try {
54-
return await octokit.rest.users.getByUsername({
55-
username: "FabricMCBot",
56-
});
57-
} catch (e) {
58-
// Allows build without internet
59-
return { data: { avatar_url: "" } };
60-
}
61-
})();
6253
const authorsNoGitHubArray = Array.from(authorsNoGitHub.values()).map(
6354
(author) =>
6455
({
65-
avatar: data.avatar_url,
6656
name: author.login,
6757
number: author.files,
6858
} as DefaultTheme.TeamMember & { number: number })

.vitepress/data/bot.data.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Octokit } from "octokit";
2+
import { DefaultTheme } from "vitepress";
3+
4+
const LOGIN = "FabricMCBot";
5+
const GITHUB_TOKEN = process.env.GITHUB_TOKEN || "";
6+
const octokit = new Octokit({ auth: GITHUB_TOKEN });
7+
8+
export default {
9+
async load() {
10+
const { data } = await (async () => {
11+
try {
12+
return await octokit.rest.users.getByUsername({
13+
username: LOGIN,
14+
});
15+
} catch (error) {
16+
console.error("Error: ", error);
17+
return { data: { avatar_url: `https://github.com/${LOGIN}.png` } };
18+
}
19+
})();
20+
21+
return {
22+
avatar: data.avatar_url,
23+
name: LOGIN,
24+
} as DefaultTheme.TeamMember;
25+
},
26+
};

.vitepress/data/committers.data.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,16 @@ export default {
88
async load() {
99
const contributors = await (async () => {
1010
try {
11+
if (!GITHUB_TOKEN) throw new Error("GITHUB_TOKEN is unset")
1112
return await octokit.paginate(octokit.rest.repos.listContributors, {
1213
owner: "FabricMC",
1314
repo: "fabric-docs",
1415
});
15-
} catch (e) {
16-
// Allows build without internet
16+
} catch (error) {
17+
console.error("Error: ", error);
1718
return [];
1819
}
1920
})();
20-
const { data } = await (async () => {
21-
try {
22-
return await octokit.rest.users.getByUsername({
23-
username: "FabricMCBot",
24-
});
25-
} catch (e) {
26-
// Allows build without internet
27-
return { data: { avatar_url: "" } };
28-
}
29-
})();
3021

3122
return contributors
3223
.filter(
@@ -36,7 +27,7 @@ export default {
3627
.map(
3728
(contributor) =>
3829
({
39-
avatar: contributor.avatar_url || data.avatar_url,
30+
avatar: contributor.avatar_url,
4031
links: [{ icon: "github", link: contributor.html_url }],
4132
name: contributor.login,
4233
number: contributor.contributions,

.vitepress/data/maintainers.data.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ export default {
88
async load() {
99
const members = await (async () => {
1010
try {
11+
if (!GITHUB_TOKEN) throw new Error("GITHUB_TOKEN is unset")
1112
return await octokit.paginate(octokit.rest.teams.listMembersInOrg, {
1213
org: "FabricMC",
1314
team_slug: "documentation",
1415
});
15-
} catch (e) {
16-
// Allows build without a GITHUB_TOKEN or without internet
16+
} catch (error) {
17+
console.error("Error: ", error);
1718
return [];
1819
}
1920
})();
@@ -23,11 +24,12 @@ export default {
2324
members.map(async (member) => ({
2425
val: await (async () => {
2526
try {
27+
if (!GITHUB_TOKEN) throw new Error("GITHUB_TOKEN is unset")
2628
return await octokit.paginate(octokit.rest.orgs.listForUser, {
2729
username: member.login,
2830
});
29-
} catch (e) {
30-
// Allows build without internet
31+
} catch (error) {
32+
console.error("Error: ", error);
3133
return [{ login: "FabricMC" }];
3234
}
3335
})(),

.vitepress/data/translators.data.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import crowdin from "@crowdin/crowdin-api-client";
2-
import axios from "axios";
32
import { DefaultTheme } from "vitepress";
43

54
const CROWDIN_TOKEN = process.env.CROWDIN_TOKEN || "";
@@ -8,16 +7,14 @@ const { sourceFilesApi, reportsApi } = (() => {
87
try {
98
//@ts-expect-error https://github.com/crowdin/crowdin-api-client-js/issues/98
109
return new crowdin.default({ token: CROWDIN_TOKEN });
11-
} catch (e) {
12-
// Allows build without a CROWDIN_TOKEN or without internet
10+
} catch (error) {
1311
return { sourceFilesApi: {}, reportsApi: {} };
1412
}
1513
})();
1614

1715
export default {
1816
async load() {
1917
if (!CROWDIN_TOKEN) return [];
20-
2118
const { data: directories } = await sourceFilesApi.listProjectDirectories(
2219
PROJECT_ID,
2320
{
@@ -53,13 +50,15 @@ export default {
5350
}
5451

5552
report = (
56-
await axios.get(
57-
(
58-
await reportsApi.downloadReport(PROJECT_ID, identifier)
59-
).data.url
60-
)
61-
).data.data;
62-
} catch (e) {}
53+
await (
54+
await fetch(
55+
(
56+
await reportsApi.downloadReport(PROJECT_ID, identifier)
57+
).data.url
58+
)
59+
).json()
60+
).data;
61+
} catch (error) {}
6362

6463
const translators = new Map<
6564
string,
@@ -89,9 +88,7 @@ export default {
8988
.map(
9089
(contributor) =>
9190
({
92-
avatar:
93-
contributor.avatar ||
94-
"https://i2.wp.com/crowdin.com/images/user-picture.png?ssl=1",
91+
avatar: contributor.avatar,
9592
links: [
9693
{
9794
icon: "crowdin",

credits.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
import { computed } from "vue";
2020

2121
import { data as _authors } from "/.vitepress/data/authors.data";
22+
import { data as _bot } from "/.vitepress/data/bot.data";
2223
import { data as _committers } from "/.vitepress/data/committers.data";
2324
import { data as _maintainers } from "/.vitepress/data/maintainers.data";
2425
import { data as _translators } from "/.vitepress/data/translators.data";
@@ -31,6 +32,7 @@ const options = computed(
3132

3233
const authors = _authors.map((author) => ({
3334
...author,
35+
avatar: author.avatar || _bot.avatar;
3436
title:
3537
author.number === 1
3638
? options.value.authors.description.singular
@@ -42,6 +44,7 @@ const authors = _authors.map((author) => ({
4244

4345
const committers = _committers.map((committer) => ({
4446
...committer,
47+
avatar: committer.avatar || _bot.avatar;
4548
title:
4649
committer.number === 1
4750
? options.value.committers.description.singular
@@ -55,6 +58,7 @@ const maintainers = _maintainers;
5558

5659
const translators = _translators.map((translator) => ({
5760
...translator,
61+
avatar: translator.avatar || _bot.avatar;
5862
title:
5963
translator.number === 1
6064
? options.value.translators.description.singular

package-lock.json

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
"dependencies": {
2525
"@crowdin/crowdin-api-client": "^1.41.1",
2626
"@vueuse/core": "^12.4.0",
27-
"axios": "^1.7.9",
2827
"markdown-it-mathjax3": "^4.3.2",
2928
"markdown-it-vuepress-code-snippet-enhanced": "github:IMB11/md-it-enhanced-snippets#dfb9fa2",
3029
"medium-zoom": "^1.1.0",

0 commit comments

Comments
 (0)