-
Notifications
You must be signed in to change notification settings - Fork 11
/
gatsby-node.js
351 lines (322 loc) · 8.71 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
const path = require(`path`)
const axios = require('axios');
const crypto = require('crypto');
const qs = require('qs');
const dotenv = require('dotenv')
const repos = [
'drupal-console',
'drupal-console-core',
'drupal-console-en',
'drupal-console-launcher',
];
dotenv.config({
path: `.env.${process.env.NODE_ENV}`,
});
exports.createSchemaCustomization = ({ actions }) => {
actions.createTypes(`
type Mdx implements Node {
fileInfo: File @link(from: "parent")
}
`)
}
exports.sourceNodes = async ({ actions }) => {
const {createNode} = actions;
for (repo of repos) {
const releases = await axios({
method: 'post',
url: `${process.env.CLOUD_FUNCTIONS_URL}/releases.json`,
data: qs.stringify({
owner: 'hechoendrupal',
repo: repo,
}),
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
for (const release of releases.data) {
const id = release.id.toString();
delete release.id;
await createNode({
children: [],
id: id,
repo: repo,
...release,
parent: null,
internal: {
type: 'Release',
contentDigest: crypto
.createHash(`md5`)
.update(JSON.stringify(release))
.digest(`hex`),
},
});
}
}
const contributors = await axios.get(`${process.env.CLOUD_FUNCTIONS_URL}/contributors.json`);
for (const contributor of contributors.data) {
const id = contributor.id.toString();
delete contributor.id;
await createNode({
children: [],
id: id,
...contributor,
parent: null,
internal: {
type: 'Contributor',
contentDigest: crypto
.createHash(`md5`)
.update(JSON.stringify(contributor))
.digest(`hex`),
},
});
}
const packagist = await axios.get(`https://packagist.org/packages/drupal/console.json`);
const latestRelease = await axios.get(`${process.env.CLOUD_FUNCTIONS_URL}/latestRelease.json`);
await createNode({
children: [],
id: latestRelease.data.version,
stars: packagist.data.package.github_stars,
watchers: packagist.data.package.github_watchers,
forks: packagist.data.package.github_forks,
open_issues: packagist.data.package.github_open_issues,
dependents: packagist.data.package.dependents,
downloads_phar: 895799, // calculated from downloads as phar, before using as local composer dependency
downloads_packagist: packagist.data.package.downloads.total,
downloads_total: 895799 + packagist.data.package.downloads.total,
favers: packagist.data.package.favers,
contributors: contributors.data.length,
latest_release: latestRelease.data.version,
parent: null,
internal: {
type: 'Statistic',
contentDigest: crypto
.createHash(`md5`)
.update(JSON.stringify(packagist.data))
.digest(`hex`),
},
});
};
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
// MDX content
if (node.internal.type === `Mdx`) {
const sourceInstanceName = getNode(node.parent).sourceInstanceName;
if (sourceInstanceName===`docs`) {
const slug = getNode(node.parent).relativePath.replace("/README.md", "").replace(".md", "").toLowerCase()
const title = node.frontmatter.title
// Add slug field
createNodeField({
name: `slug`,
node,
value: `/${sourceInstanceName}/${slug}`
})
// Add language field
createNodeField({
name: `language`,
node,
value: slug.indexOf('/') > 0 ? slug.substring(0, slug.indexOf('/')) : slug
})
// Add title field
createNodeField({
name: `title`,
node,
value: title
})
}
else {
// Add language field
createNodeField({
name: `language`,
node,
value: 'en'
})
}
if (sourceInstanceName===`articles` || sourceInstanceName===`changelog` || sourceInstanceName===`pages`) {
const slug = node.frontmatter.path
// Add slug field
createNodeField({
name: `slug`,
node,
value: slug
})
}
}
}
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions
return graphql(`
{
allDocs: allMdx(filter: {fileInfo: {sourceInstanceName: {eq: "docs"}}}) {
edges {
node {
fields {
slug
language
}
rawBody
excerpt
}
}
}
allArticles: allMdx(filter: {fileInfo: {sourceInstanceName: {eq: "articles"}}}) {
edges {
node {
fields {
slug
language
}
rawBody
}
}
}
allPages: allMdx(filter: {fileInfo: {sourceInstanceName: {eq: "pages"}}}) {
edges {
node {
fields {
slug
language
}
rawBody
}
}
}
allChangelog: allMdx(filter: {fileInfo: {sourceInstanceName: {eq: "changelog"}}}) {
edges {
node {
fields {
slug
language
}
}
}
}
allLanguages: allLanguagesYaml {
edges {
node {
id
slug
name
}
}
}
allCommands: allDataJson(filter: {type: {eq: "commands"}}) {
edges {
node {
language
commands {
name
description
dashed
aliases
options {
description
name
}
arguments {
description
name
}
examples {
description
execution
}
messages {
arguments
details
options
usage
examples
argument
option
}
}
}
}
}
}
`).then(result => {
if (result.errors) {
throw result.errors
}
// Create doc pages.
const docs = result.data.allDocs.edges
docs.forEach(doc => {
createPage({
path: doc.node.fields.slug,
component: path.resolve(`./src/templates/doc.js`),
context: {
slug: doc.node.fields.slug,
language: doc.node.fields.language
},
})
})
// Create article pages.
const articles = result.data.allArticles.edges
articles.forEach(article => {
createPage({
path: article.node.fields.slug,
component: path.resolve(`./src/templates/article.js`),
context: {
slug: article.node.fields.slug,
language: article.node.fields.language
},
})
})
// Create page pages.
const pages = result.data.allPages.edges
pages.forEach(page => {
createPage({
path: page.node.fields.slug,
component: path.resolve(`./src/templates/page.js`),
context: {
slug: page.node.fields.slug,
language: page.node.fields.language
},
})
})
// Create changelog pages.
const releases = result.data.allChangelog.edges
releases.forEach(changelog => {
createPage({
path: changelog.node.fields.slug,
component: path.resolve(`./src/templates/changelog.js`),
context: {
slug: changelog.node.fields.slug,
language: changelog.node.fields.language
},
})
})
// Create languag pages.
const languages = result.data.allLanguages.edges
languages.forEach(item => {
const language = item.node.id
const slug = `docs/${language}/commands/`
createPage({
path: slug,
component: path.resolve(`./src/templates/commands.js`),
context: {
slug: slug,
language: language
},
})
})
// Create command pages.
const commands = result.data.allCommands.edges
commands.forEach(item => {
const language = item.node.language
item.node.commands.forEach(command => {
const slug = `docs/${language}/commands/${command.dashed}`
createPage({
path: slug,
component: path.resolve(`./src/templates/command.js`),
context: {
slug: slug,
language: language,
command: command
},
})
})
})
return null
})
}