-
Notifications
You must be signed in to change notification settings - Fork 14
/
gridsome.server.js
160 lines (147 loc) · 4.13 KB
/
gridsome.server.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Server API makes it possible to hook into various parts of Gridsome
// on server-side and add custom data to the GraphQL data layer.
// Learn more: https://gridsome.org/docs/server-api/
// Changes here require a server restart.
// To restart press CTRL + C in terminal and run `gridsome develop`
const fetch = require('node-fetch')
const GitHub = require('github-api')
const uuid = require('uuid').v4
const marked = require('marked')
module.exports = function (api) {
api.loadSource(
async ({
addCollection,
addMetadata,
store,
addReference,
getCollection,
}) => {
// Use the Data Store API here: https://gridsome.org/docs/data-store-api/
addMetadata('settings', require('./gridsome.config').settings)
const gh = new GitHub({
token: process.env.GITHUB_TOKEN,
})
const bridgeRepo = gh.getRepo('bridge-core', 'bridge.')
const pluginRepo = gh.getRepo('bridge-core', 'plugins')
const dataRepo = gh.getRepo('bridge-core', 'data')
const editorRepo = gh.getRepo('bridge-core', 'editor')
const contributorData = (await bridgeRepo.getContributors()).data
.concat(
(await pluginRepo.getContributors()).data.map((d) => ({
...d,
isPluginAuthor: true,
}))
)
.concat((await dataRepo.getContributors()).data)
.concat((await editorRepo.getContributors()).data)
const filteredContributors = []
contributorData.forEach((d) => {
if (
!filteredContributors.find(({ login }) => d.login === login)
)
filteredContributors.push(d)
})
// Release data
const releases = addCollection({
typeName: 'Release',
})
const releaseData = (await editorRepo.listReleases()).data
releaseData.forEach((release) => {
if (release.body) release.content = marked(release.body)
releases.addNode(release)
})
const contributors = addCollection({
typeName: 'Contributor',
})
filteredContributors.forEach(
({ id, html_url, avatar_url, login, type, isPluginAuthor }) =>
contributors.addNode({
id,
type,
isPluginAuthor,
title: login,
path: html_url,
image: avatar_url,
})
)
const plugins = addCollection({
typeName: 'Plugin',
})
const tagCollection = getCollection('Tag')
const authorCollection = getCollection('Author')
const ghPluginData = (
await pluginRepo.getContents('master', 'plugins.json', true)
).data.concat(
(
await pluginRepo.getContents(
'master',
'extensions.json',
true
)
).data
)
await Promise.all(
ghPluginData.map(
async ({
author,
tags = [],
target = 'v1',
contributeFiles,
...other
}) => {
let readmeLink = other.link.split(/\\|\//g)
readmeLink.pop()
readmeLink = readmeLink.concat(['README.md']).join('/')
const readme = (
await pluginRepo
.getContents('master', readmeLink, true)
.catch(() => ({}))
).data
tags.unshift(`v${other.version.replace(/\./g, '-')}`)
tags.unshift(
...(target === 'both'
? ['bridge-v1', 'bridge-v2']
: ['bridge-' + target])
)
tags.forEach(
(t) =>
tagCollection.findNode({
title: t.toLowerCase(),
}) ||
tagCollection.addNode({
title: t.toLowerCase(),
id: t.toLowerCase(),
})
)
const contributor =
authorCollection.findNode({
id: author.replace(/\s+/g, '').toLowerCase(),
}) ||
contributors.findNode({
title: author.replace(/\s+/g, ''),
}) ||
contributors.addNode({
title: author,
isPluginAuthor: true,
image: `https://robohash.org/${author}`,
})
if (plugins.getNodeById(other.id) === undefined)
plugins.addNode({
...other,
content: readme ? marked(readme) : '',
path: `/plugins/${other.id}/`,
author: store.createReference(contributor),
tags: store.createReference(
'Tag',
tags.map((t) => t.toLowerCase())
),
})
}
)
)
}
)
api.createPages(({ createPage }) => {
// Use the Pages API here: https://gridsome.org/docs/pages-api/
})
}