-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
184 lines (170 loc) · 4.39 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
const { withDefaults } = require(`./utils/default-options`)
const fs = require("fs")
const { createSearchIndex, searchQuery } = require(`./utils/search`)
const {
siteMetadataTypeDefs,
projectTypeDefs,
contactTypeDefs,
pageTypeDefs,
} = require(`./utils/types`)
exports.onPreBootstrap = ({ reporter }, themeOptions) => {
const { themeImageDirectory } = withDefaults(themeOptions)
const paths = [themeImageDirectory]
paths.forEach((path) => {
if (!fs.existsSync(path)) {
reporter.info(`creating the ${path} directory`)
fs.mkdirSync(path, { recursive: true })
}
})
}
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions
createTypes(siteMetadataTypeDefs)
createTypes(projectTypeDefs)
createTypes(contactTypeDefs)
createTypes(pageTypeDefs)
}
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions
const result = await graphql(`
query {
allProject {
nodes {
slug
}
}
allCardPage {
nodes {
slug
filterOn {
status
}
}
}
searchPages: allGeneralPage(
filter: { templateKey: { eq: "SearchPage" } }
) {
nodes {
slug
}
}
aboutPages: allGeneralPage(filter: { templateKey: { eq: "AboutPage" } }) {
nodes {
slug
}
}
contactPages: allGeneralPage(
filter: { templateKey: { eq: "ContactPage" } }
) {
nodes {
slug
}
}
}
`)
if (result.errors) {
reporter.panicOnBuild(result.errors)
}
// Create Posts and Post pages.
const { allProject } = result.data
const projects = allProject.nodes
// Create a page for each Post
projects.forEach((project) => {
const { slug } = project
createPage({
path: `project/${slug}`,
component: require.resolve(`./src/layouts/ProjectDetailPage.tsx`),
context: {
slug: slug,
},
})
})
const { allCardPage } = result.data
allCardPage.nodes.forEach((page) => {
const {
slug,
filterOn: { status },
} = page
createPage({
path: `/${slug}`,
component: require.resolve(`./src/layouts/CardPageLayout.tsx`),
context: {
slug: slug,
statusFilter: status,
},
})
})
const { searchPages } = result.data
searchPages.nodes.forEach((page) => {
const { slug } = page
createPage({
path: `/${slug}`,
component: require.resolve(`./src/layouts/SearchPageLayout.tsx`),
context: {
slug: slug,
},
})
})
const { aboutPages } = result.data
aboutPages.nodes.forEach((page) => {
const { slug } = page
createPage({
path: `/${slug}`,
component: require.resolve(`./src/layouts/AboutPageLayout.tsx`),
context: {
slug: slug,
},
})
})
const { contactPages } = result.data
contactPages.nodes.forEach((page) => {
const { slug } = page
const thankYouPagePath = `/${slug}/thank-you`
createPage({
path: `/${slug}`,
component: require.resolve(`./src/layouts/ContactPageLayout.tsx`),
context: {
slug: slug,
thankYouPagePath: thankYouPagePath,
},
})
createPage({
path: thankYouPagePath,
component: require.resolve("./src/layouts/ThankYouPageLayout.tsx"),
context: {
slug: slug,
},
})
})
}
exports.onPreBuild = async ({ reporter, basePath, pathPrefix, graphql }) => {
const result = await graphql(searchQuery)
const { allProject, allGeneralPage } = result.data
const [index, documents] = createSearchIndex({ allProject, allGeneralPage })
await fs.writeFile("static/lunr-index.json", JSON.stringify(index), (err) => {
if (err) console.error(err)
})
await fs.writeFile(
"static/documents.json",
JSON.stringify(documents),
(err) => {
if (err) console.error(err)
}
)
// this is a function which grabs the page from the original documents
// lunr tosses this info for SPEED
const reduceDocuments = documents.reduce(function (page, document) {
page[document.slug] = document
return page
}, {})
await fs.writeFile(
"static/documents-reduced.json",
JSON.stringify(reduceDocuments),
(err) => {
if (err) console.error(err)
}
)
reporter.info(
`Site was built with basePath: ${basePath} & pathPrefix: ${pathPrefix}`
)
}