-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-catalog.mjs
More file actions
97 lines (87 loc) · 2.67 KB
/
update-catalog.mjs
File metadata and controls
97 lines (87 loc) · 2.67 KB
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
94
95
96
97
(() => {
if (process.env.LEADYOU_TOKEN === undefined) {
const error = new Error("Environment variable LEADYOU_TOKEN does not exist.");
console.error("⚠️", error);
process.exit(0);
}
})();
import fetch from "node-fetch";
import { promises as fs } from "fs";
const apiToken = process.env.LEADYOU_TOKEN;
// Tap GitHub API
const checkStatus = (response) => {
if (response.ok) {
return response.json();
} else {
throw new Error(`HTTP Error Response: ${response.status} ${response.statusText}`);
}
};
const getJSON = (requestURL) => {
const options = {
method: "GET",
headers: {
Authorization: `token ${apiToken}`,
Accept: "application/vnd.github+json",
},
};
return fetch(requestURL, options).then((res) => checkStatus(res));
};
// Fetch READMEs
const searchCodes = async () => {
const leadyouTag = "CREATED_BY_LEADYOU_README_GENERATOR";
let requestURL = "https://api.github.com/search/code";
requestURL += `?q=${leadyouTag}+in:file+language:md+filename:README+path:/`;
const json = await getJSON(requestURL);
return json.items.map((item) => ({
ownerRepo: item.repository.full_name,
sha: item.html_url.split("/blob/").pop().split("/").shift(),
}));
};
const getDate = async (ownerRepo, sha) => {
const requestURL = `https://api.github.com/repos/${ownerRepo}/commits/${sha}`;
const props = { ownerRepo, sha };
try {
const json = await getJSON(requestURL);
return { ...props, date: Date.parse(json.commit.committer.date) };
} catch (error) {
console.log(ownerRepo, sha);
console.error(error);
return { ...props, date: null };
}
};
const fetchReadmes = async () => {
const codes = await searchCodes();
const promises = codes.map(({ ownerRepo, sha }) => {
return getDate(ownerRepo, sha);
});
let commits = await Promise.all(promises);
const repositories = commits
.filter((commit) => commit.date !== null)
.sort((a, b) => b.date - a.date)
.map((props) => {
const rawURL = `https://raw.githubusercontent.com/${props.ownerRepo}/${props.sha}/README.md`;
return { ...props, rawURL };
})
.slice(0, 12);
return { totalCount: codes.length, repositories };
};
// Overwrite Catalog.json
const overwriteCatalog = async (catalog) => {
const text = JSON.stringify(catalog, null, 2);
try {
await fs.writeFile("src/json/catalog.json", text);
} catch (error) {
throw error;
}
};
(async () => {
try {
const catalog = await fetchReadmes();
console.dir(catalog, { depth: null });
console.log("count", catalog.repositories.length);
await overwriteCatalog(catalog);
} catch (error) {
console.error("⚠️", error);
process.exit(1);
}
})();