-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-readme.js
89 lines (79 loc) · 2.54 KB
/
build-readme.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
const fs = require('fs')
const {GraphQLClient} = require('graphql-request')
function sortReposByReleaseDate(a, b) {
if (a.releases.nodes[0].publishedAt > b.releases.nodes[0].publishedAt) {
return -1
} else if (
a.releases.nodes[0].publishedAt < b.releases.nodes[0].publishedAt
) {
return 1
}
return 0
}
function createReleaseHTML(repo) {
const release = repo.releases.nodes[0]
const date = release.publishedAt.substring(0, 10)
const description = repo.descriptionHTML
.replace(/^<div>/, '<div><em>')
.replace(/<\/div>$/, '</em></div>')
return `<li>
<strong>${date}</strong> – <a href="${repo.url}">${repo.name}</a> <a href="${release.url}">${release.tagName}</a>
${description}
<details><summary>${release.tagName} release notes</summary>${release.descriptionHTML}</details>
</li>`
}
function replaceSectionContent(readme, sectionCode, content) {
const sectionRegExp = new RegExp(
`(<!-- ${sectionCode} -->)\\r?\\n.*\\r?\\n(<!-- /${sectionCode} -->)`,
's'
)
const match = sectionRegExp.exec(readme)
if (!match) {
return readme
}
const before = readme.slice(0, match.index)
const after = readme.slice(match.index + match[0].length)
return `${before}${match[1]}\n${content}\n${match[2]}${after}`
}
async function main() {
const graphQLClient = new GraphQLClient('https://api.github.com/graphql', {
headers: {
authorization: `Bearer ${process.env.INSIN_TOKEN}`,
},
})
const response = await graphQLClient.request(`{
viewer {
repositories(ownerAffiliations:[OWNER], isFork: false, isLocked:false, privacy: PUBLIC, orderBy: {field: PUSHED_AT, direction: DESC}, first: 100) {
nodes {
descriptionHTML
name
releases(orderBy: {field: CREATED_AT, direction: DESC}, first: 1) {
nodes {
descriptionHTML
publishedAt
tagName
url
}
}
url
}
}
}
}`)
const releases = response.viewer.repositories.nodes
.filter((repo) => repo.releases.nodes.length > 0 && repo.releases.nodes[0].publishedAt != null)
.sort(sortReposByReleaseDate)
.slice(0, 5)
.map(createReleaseHTML)
const readme = fs.readFileSync('README.md', 'utf-8')
fs.writeFileSync(
'README.md',
replaceSectionContent(
readme,
'RECENT_RELEASES',
`<ul>\n${releases.join('\n')}\n</ul>`
),
'utf-8'
)
}
main().catch((error) => console.error(error))