-
Notifications
You must be signed in to change notification settings - Fork 6
/
gatsby-node.js
282 lines (266 loc) · 10.1 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
const path = require('path')
const fs = require('fs')
const appRootDir = require('app-root-dir').get()
const { createFilePath } = require('gatsby-source-filesystem')
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
if (node.internal.type === 'Mdx' && node.frontmatter.title) {
const slug = createFilePath({ node, getNode, basePath: 'pages' })
createNodeField({
node,
name: 'slug',
value: slug,
})
}
}
exports.createPages = ({ graphql, reporter, actions }) => {
const { createRedirect } = actions
return Promise.all([
createRedirects({ graphql, reporter, createRedirect }),
createComponentsCache({ graphql, reporter }),
createYearsCache({ graphql, reporter })
])
}
exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
node: {
fs: 'empty'
}
})
}
// Create redirects from Redirect.mdx frontmatter
const createRedirects = ({ graphql, reporter, createRedirect }) => {
console.info('creating redirects')
return new Promise((resolve, reject) => {
resolve(
graphql(`
query redirects {
allMdx(filter: {fileAbsolutePath: {regex: "/content-partials/Redirects/"}}) {
nodes {
frontmatter {
redirects {
newUrl
oldUrl
}
stateRedirects
}
}
}
}
`)
)
}).then(result => {
if (result.errors) {
reporter.panicOnBuild('🚨 ERROR: Loading "createPages" query')
}
else {
const redirects = result.data.allMdx.nodes[0].frontmatter.redirects
const stateRedirects = result.data.allMdx.nodes[0].frontmatter.stateRedirects
// standard redirects fromPath -> toPath
redirects.forEach(element => {
createRedirect({
fromPath: element.oldUrl,
toPath: element.newUrl,
redirectInBrowser: true,
isPermanent: true
})
})
// explore state redirects
stateRedirects.forEach(state => {
createRedirect({
fromPath: `/explore/${ state }/`,
toPath: `/explore/?location=${ state.toUpperCase() }`,
redirectInBrowser: true,
isPermanent: true
})
})
}
})
}
const createYearsCache = ({ graphql, reporter }) => {
console.info('creating years cache index')
const PERIOD_FISCAL_YEAR = 'Fiscal Year'
const PERIOD_CALENDAR_YEAR = 'Calendar Year'
const REVENUE = 'Revenue'
const DISBURSEMENT = 'Disbursements'
const PRODUCTION = 'Production'
const REVENUE_BY_COMPANY = 'Federal revenue by company'
const FEDERAL_SALES = 'Federal Sales'
return new Promise((resolve, reject) => {
resolve(
graphql(`
{
onrr {
revenue_fiscal_years: period(distinct_on: fiscal_year, where: {revenues: {revenue: {_is_null: false}, period: {period: {_eq: "Fiscal Year"}}}}, order_by: {fiscal_year: asc}) {
fiscal_year
}
revenue_calendar_years: period(distinct_on: calendar_year, where: {revenues: {revenue: {_is_null: false}, period: {period: {_eq: "Calendar Year"}}}}, order_by: {calendar_year: asc}) {
calendar_year
}
production_fiscal_years: period(distinct_on: fiscal_year, where: {productions: {volume: {_is_null: false}, period: {period: {_eq: "Fiscal Year"}}}}, order_by: {fiscal_year: asc}) {
fiscal_year
}
production_calendar_years: period(distinct_on: calendar_year, where: {productions: {volume: {_is_null: false}, period: {period: {_eq: "Monthly"}, month: {_eq: 12}} }}, order_by: {calendar_year: asc}) {
calendar_year
}
disbursement_fiscal_years: period(distinct_on: fiscal_year, where: {disbursements: {disbursement: {_is_null: false}, period: {period: {_eq: "Fiscal Year"}}}}, order_by: {fiscal_year: asc}) {
fiscal_year
}
disbursement_calendar_years: period(distinct_on: calendar_year, where: {disbursements: {disbursement: {_is_null: false}, period: {period: {_eq: "Monthly"}, month: {_eq: 12}}}}, order_by: {calendar_year: asc}) {
calendar_year
}
federal_revenue_by_company_calendar_years: federal_revenue_by_company(distinct_on: calendar_year, order_by: {calendar_year: asc}) {
calendar_year
},
sales_calendar_years: federal_sales_v(distinct_on: calendar_year, order_by: {calendar_year: asc}) {
calendar_year
}
}
}
`).then(result => {
if (result.errors) {
reporter.panicOnBuild('🚨 ERROR: Loading "Create Years Cache" query', result.errors)
}
else {
result.data.onrr.revenue_fiscal_years = result.data.onrr.revenue_fiscal_years.map(y => y[Object.keys(y)[0]])
result.data.onrr.revenue_calendar_years = result.data.onrr.revenue_calendar_years.map(y => y[Object.keys(y)[0]])
result.data.onrr.production_fiscal_years = result.data.onrr.production_fiscal_years.map(y => y[Object.keys(y)[0]])
result.data.onrr.production_calendar_years = result.data.onrr.production_calendar_years.map(y => y[Object.keys(y)[0]])
result.data.onrr.disbursement_fiscal_years = result.data.onrr.disbursement_fiscal_years.map(y => y[Object.keys(y)[0]])
result.data.onrr.disbursement_calendar_years = result.data.onrr.disbursement_calendar_years.map(y => y[Object.keys(y)[0]])
result.data.onrr.federal_revenue_by_company_calendar_years = result.data.onrr.federal_revenue_by_company_calendar_years.map(y => y[Object.keys(y)[0]])
result.data.onrr.sales_calendar_years = result.data.onrr.sales_calendar_years.map(y => y[Object.keys(y)[0]])
const allYears = {
[REVENUE]: {
[PERIOD_CALENDAR_YEAR]: result.data.onrr.revenue_calendar_years,
[PERIOD_FISCAL_YEAR]: result.data.onrr.revenue_fiscal_years
},
[PRODUCTION]: {
[PERIOD_CALENDAR_YEAR]: result.data.onrr.production_calendar_years,
[PERIOD_FISCAL_YEAR]: result.data.onrr.production_fiscal_years
},
[DISBURSEMENT]: {
[PERIOD_CALENDAR_YEAR]: result.data.onrr.disbursement_calendar_years,
[PERIOD_FISCAL_YEAR]: result.data.onrr.disbursement_fiscal_years
},
[REVENUE_BY_COMPANY]: {
[PERIOD_CALENDAR_YEAR]: result.data.onrr.federal_revenue_by_company_calendar_years
},
[FEDERAL_SALES]: {
[PERIOD_CALENDAR_YEAR]: result.data.onrr.sales_calendar_years
}
}
fs.writeFileSync(
path.join(appRootDir, '.cache/all-years.js'),
`const ALL_YEARS = ${ JSON.stringify(allYears) }\nexport default ALL_YEARS\n`
)
}
})
)
})
}
/**
* Creates a index file of all the components during build time so they can be easily imported for the mdx provider to use.
* This aids in creating mdx pages since we dont have to import components in a mdx page. This is also used in creating documentation for
* the pattern library.
* @param {*} graphql
*/
const createComponentsCache = ({ graphql, reporter }) => {
console.info('creating components cache index')
return new Promise((resolve, reject) => {
resolve(
graphql(`
{
allMdx(filter: {fileAbsolutePath: {regex: "/content-partials/"}}) {
edges {
node {
parent {
... on File {
name
absolutePath
}
}
}
}
}
allComponentMetadata {
nodes {
id
displayName
description {
text
}
parent {
... on File {
absolutePath
name
}
}
props {
name
type {
name
raw
value
}
description {
text
}
required
}
}
}
}
`).then(result => {
if (result.errors) {
reporter.panicOnBuild('🚨 ERROR: Loading "Create Components Cache" query', result.errors)
}
else {
const allComponents = result.data.allComponentMetadata.nodes.filter(node => (!node.displayName.includes('Demos'))).map(
(node, i) =>
Object.assign({}, node, {
componentName: node.parent.name,
filePath: node.parent.absolutePath,
})
)
const allMdx = result.data.allMdx.edges.map(
(node, i) => Object.assign({}, node.node, {
displayName: node.node.parent.name,
filePath: node.node.parent.absolutePath,
})
)
let exportFileContents =
allComponents
.reduce((accumulator, { displayName, filePath, componentName }) => {
if (filePath.search('components/images/index.js') >= 0) {
accumulator.push(
`export { ${ displayName } } from "${ filePath }"`
)
}
else if (displayName === componentName) {
accumulator.push(
`export { default as ${ displayName } } from "${ filePath }"`
)
}
return accumulator
}, [])
.join('\n') + '\n'
exportFileContents = exportFileContents.concat(
allMdx
.reduce((accumulator, { displayName, filePath }) => {
accumulator.push(
`export { default as ${ displayName } } from "${ filePath }"`
)
return accumulator
}, [])
.join('\n') + '\n'
)
fs.writeFileSync(
path.join(appRootDir, '.cache/components.js'),
exportFileContents
)
}
})
)
})
}