/
gatsby-node.js
135 lines (116 loc) · 3.8 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
const Promise = require(`bluebird`)
const startersRedirects = require(`./starter-redirects.json`)
const { loadYaml } = require(`./src/utils/load-yaml`)
const redirects = loadYaml(`./redirects.yaml`)
// Split the logic into files based on the section of the website.
// The eventual goal is to split www into different themes per section.
const docs = require(`./src/utils/node/docs.js`)
const blog = require(`./src/utils/node/blog.js`)
const showcase = require(`./src/utils/node/showcase.js`)
const starters = require(`./src/utils/node/starters.js`)
const creators = require(`./src/utils/node/creators.js`)
const packages = require(`./src/utils/node/packages.js`)
const features = require(`./src/utils/node/features.js`)
const apiCalls = require(`./src/utils/node/api-calls.js`)
const sections = [
docs,
blog,
showcase,
starters,
creators,
packages,
features,
apiCalls,
]
// Run the provided API on all defined sections of the site
async function runApiForSections(api, helpers) {
await Promise.all(
sections.map(section => section[api] && section[api](helpers))
)
}
exports.onCreateWebpackConfig = ({ actions, plugins }) => {
const currentCommitSHA = require(`child_process`)
.execSync(`git rev-parse HEAD`, {
encoding: `utf-8`,
})
.trim()
actions.setWebpackConfig({
plugins: [
plugins.define({
"process.env.COMMIT_SHA": JSON.stringify(currentCommitSHA),
}),
],
})
}
exports.createSchemaCustomization = async helpers => {
await runApiForSections(`createSchemaCustomization`, helpers)
const {
actions: { createTypes },
} = helpers
// Explicitly define Airtable types so that queries still work
// when there are no events.
// TODO make upstream change to gatsby-source-airtable
createTypes(/* GraphQL */ `
type Airtable implements Node {
id: ID!
data: AirtableData
}
type AirtableData @dontInfer {
name: String @proxy(from: "Name_of_Event")
organizerFirstName: String @proxy(from: "Organizer_Name")
organizerLastName: String @proxy(from: "Organizer's_Last_Name")
date: Date @dateformat @proxy(from: "Date_of_Event")
location: String @proxy(from: "Location_of_Event")
url: String @proxy(from: "Event_URL_(if_applicable)")
type: String @proxy(from: "What_type_of_event_is_this?")
hasGatsbyTeamSpeaker: Boolean @proxy(from: "Gatsby_Speaker_Approved")
approved: Boolean @proxy(from: "Approved_for_posting_on_event_page")
}
`)
}
exports.createResolvers = async helpers => {
await runApiForSections(`createResolvers`, helpers)
const { createResolvers } = helpers
// Patch `DocumentationJs` type to handle custom `@availableIn` jsdoc tag
createResolvers({
DocumentationJs: {
availableIn: {
type: `[String]`,
resolve(source) {
const { tags } = source
if (!tags || !tags.length) {
return []
}
const availableIn = tags.find(tag => tag.title === `availableIn`)
if (availableIn) {
return availableIn.description
.split(`\n`)[0]
.replace(/[[\]]/g, ``)
.split(`,`)
.map(api => api.trim())
}
return []
},
},
},
})
}
exports.onCreateNode = async helpers => {
await runApiForSections(`onCreateNode`, helpers)
}
exports.createPages = async helpers => {
await runApiForSections(`createPages`, helpers)
const { actions } = helpers
const { createRedirect } = actions
redirects.forEach(redirect => {
createRedirect({ isPermanent: true, ...redirect, force: true })
})
Object.entries(startersRedirects).forEach(([fromSlug, toSlug]) => {
createRedirect({
fromPath: `/starters${fromSlug}`,
toPath: `/starters${toSlug}`,
isPermanent: true,
force: true,
})
})
}