-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgatsby-node.js
93 lines (85 loc) · 2.52 KB
/
gatsby-node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const fs = require("fs");
const path = require("path");
const fetch = require("node-fetch");
const inJapan = (username, users) => {
const user = users[username];
return (
user && (/(Japan|Tokyo|hyogo|日本)/i.test(user.location) || user.japan)
);
};
async function fetchJson(url) {
console.log(`Downloading ${url}`);
const response = await fetch(url);
return response.json();
}
async function fetchUsers() {
const url =
"https://raw.githubusercontent.com/aws-amplify-jp/aws-amplify-contributors/main/assets/users.json";
return await fetchJson(url);
}
async function fetchContributors() {
const url =
"https://raw.githubusercontent.com/aws-amplify-jp/aws-amplify-contributors/main/assets/contributors.json";
return await fetchJson(url);
}
async function readUsersInJapan() {
const usersInJapanJsonPath = path.join(
__dirname,
"./src/contents/contributors/users-in-japan.json"
);
return fs.existsSync(usersInJapanJsonPath)
? JSON.parse(
await fs.promise.readFile(usersInJapanJsonPath, { encoding: "utf8" })
)
: [];
}
async function fetchContributorsInJapan() {
const usersInJapan = await readUsersInJapan();
const repositories = await fetchContributors();
const users = await fetchUsers();
// locationだけで日本と判断できないユーザーを明示的に日本であると補完する
usersInJapan.forEach((userInJapan) => {
const user = users.find((user) => user.login === userInJapan);
if (user) {
user.japan = true;
}
});
const contributorList = [];
for (const { owner, repo, contributors } of repositories) {
contributorList.push(
...contributors
.filter(({ login }) => owner === "aws-amplify-jp" || inJapan(login, users))
.map((contributor) => ({
owner,
repo,
contributor,
}))
);
}
return contributorList;
}
exports.sourceNodes = async ({
actions,
createNodeId,
createContentDigest,
}) => {
//
// Create nodes for all of the contributors
//
const contributors = await fetchContributorsInJapan();
contributors.forEach(({ owner, repo, contributor }) => {
const { login, avatar_url, html_url } = contributor;
const repository = `${owner}/${repo}`;
actions.createNode({
contributeTo: repository,
name: login,
avatar: avatar_url,
url: html_url,
id: createNodeId(`Contributor-${repository}-${login}`),
internal: {
type: "Contributor",
contentDigest: createContentDigest(contributor),
},
});
});
};