forked from hackclub/v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
111 lines (103 loc) · 3.21 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
const path = require('path')
const fs = require('fs')
const _ = require('lodash')
const GeoPattern = require('geopattern')
const { colors } = require('@hackclub/design-system')
const writeFile = require('fs').writeFile
const axios = require('axios')
exports.onPreBootstrap = () =>
axios
.get('https://api.hackclub.com/v1/challenges')
.then(res => res.data)
.then(challenges => {
challenges.forEach(data => {
data.id = data.id.toString() // for Gatsby
})
writeFile('./public/challenges.json', JSON.stringify(challenges), err => {
if (err) throw err
})
})
.catch(e => {
console.error(e)
})
const pattern = (text = 'Hack Club', color = colors.primary) =>
GeoPattern.generate(text, { baseColor: color }).toString()
const writePattern = (path, name) =>
fs.writeFile(path, pattern(_.camelCase(name), colors.blue[6]), (err, a) => {
if (err) console.error(err)
})
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions
if (node.internal.type === 'MarkdownRemark') {
const fileNode = getNode(node.parent)
const parsedFilePath = path.parse(fileNode.relativePath)
if (!!parsedFilePath.dir && _.includes(fileNode.relativePath, 'README')) {
const value = `/workshops/${parsedFilePath.dir}`
createNodeField({ node, name: 'slug', value })
createNodeField({ node, name: 'bg', value: `${value}.svg` })
const dir = './public/workshops'
if (!fs.existsSync(dir)) fs.mkdirSync(dir)
const path = `./public${_.replace(value, '/lib', '')}.svg`
writePattern(path, node.frontmatter.name)
}
}
}
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions
return Promise.all([
new Promise((resolve, reject) => {
segmentData = require(path.resolve('src/data.json')).userSegmentPages
resolve(
_.forEach(Object.keys(segmentData), segmentPath => {
createPage({
path: `/${segmentPath}`,
component: path.resolve('src/templates/segmenter.js'),
context: {
userType: segmentData[segmentPath]
}
})
})
)
}),
new Promise((resolve, reject) => {
const component = path.resolve('src/templates/workshop.js')
resolve(
graphql(
`
{
allMarkdownRemark(filter: { frontmatter: { name: { ne: null } } }) {
edges {
node {
frontmatter {
name
description
group
order
}
fields {
slug
}
}
}
}
}
`
).then(result => {
if (result.errors) {
console.log(result.errors)
reject(result.errors)
}
_.forEach(result.data.allMarkdownRemark.edges, edge => {
createPage({
path: edge.node.fields.slug,
component,
context: {
slug: edge.node.fields.slug
}
})
})
})
)
})
])
}