-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmarkdown-from-repos.js
53 lines (46 loc) · 1.54 KB
/
markdown-from-repos.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
import {readFileSync, writeFileSync} from 'node:fs';
import fetch from 'node-fetch';
const data = JSON.parse(readFileSync('./data.json', 'utf8'));
const plopHeader = readFileSync('./plop-header.md', 'utf8');
const cfg = { headers: { 'user-agent': 'Mozilla/5.0' } };
Promise.all(
data.map(async (repoData) => {
const res = await fetch(`https://api.github.com/repos/${repoData.repoUrl}`, cfg);
const repoJSON = await res.json();
const { description, html_url } = repoJSON;
return {
description,
html_url,
...repoData,
};
})
).then((repoDataArr) => {
const reposObj = repoDataArr.reduce((prev, repoData) => {
const cat = repoData.category;
if (prev[cat]) {
prev[cat].push(repoData);
} else {
prev[cat] = [repoData];
}
return prev;
}, {});
const headers = Object.keys(reposObj);
const awesomeListData = headers
.map((header) => {
const repoArr = reposObj[header];
const headerItems = repoArr
.map((repoData) => {
let str = `- [${repoData.name}](${repoData.html_url}) - ${repoData.description}`
if (repoData.image && !repoData.imageLink) {
str += `\n\n![${repoData.imageAlt || ""}](${repoData.image})\n\n`
} else if (repoData.image && repoData.imageLink) {
str += `\n\n[![${repoData.imageAlt || ""}](${repoData.image})](${repoData.imageLink})\n\n`
}
return str;
})
.join('\n');
return `## ${header}\n\n${headerItems}`;
})
.join('\n\n');
writeFileSync('./README.md', [plopHeader, awesomeListData].join('\n'));
});