forked from tomrndom/fnEvent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgatsby-node.js
More file actions
519 lines (439 loc) · 16.9 KB
/
gatsby-node.js
File metadata and controls
519 lines (439 loc) · 16.9 KB
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
const axios = require('axios');
const path = require('path');
const fs = require("fs");
const webpack = require('webpack');
const {createFilePath} = require('gatsby-source-filesystem');
const SentryWebpackPlugin = require("@sentry/webpack-plugin");
const {ClientCredentials} = require('simple-oauth2');
const URI = require('urijs');
const sizeOf = require('image-size');
const colorsFilePath = 'src/content/colors.json';
const disqusFilePath = 'src/content/disqus-settings.json';
const marketingFilePath = 'src/content/marketing-site.json';
const homeFilePath = 'src/content/home-settings.json';
const settingsFilePath = 'src/content/settings.json';
const eventsFilePath = 'src/content/events.json';
const eventsIdxFilePath = 'src/content/events.idx.json';
const speakersFilePath = 'src/content/speakers.json';
const speakersIdxFilePath = 'src/content/speakers.idx.json';
const voteablePresentationFilePath = 'src/content/voteable_presentations.json';
const summitFilePath = 'src/content/summit.json';
const maintenanceFilePath = 'src/content/maintenance.json';
const fileBuildTimes = [];
const myEnv = require("dotenv").config({
path: `.env.${process.env.NODE_ENV}`,
});
const getAccessToken = async (config, scope) => {
const client = new ClientCredentials(config);
try {
return await client.getToken({ scope });
} catch (error) {
console.log('Access Token error', error);
}
};
const SSR_getMarketingSettings = async (baseUrl, summitId) => {
const params = {
per_page: 100,
};
return await axios.get(
`${baseUrl}/api/public/v1/config-values/all/shows/${summitId}`,
{ params }
)
.then(response => {
return response.data.data
})
.catch(e => console.log('ERROR: ', e));
};
const SSR_GetRemainingPages = async (endpoint, params, lastPage) => {
// create an array with remaining pages to perform Promise.All
const pages = [];
for (let i = 2; i <= lastPage; i++) {
pages.push(i);
}
let remainingPages = await Promise.all(pages.map(pageIdx => {
return axios.get(endpoint ,
{ params : {
...params,
page: pageIdx
}
}).then(({ data }) => data);
}));
return remainingPages.sort((a, b,) => a.current_page - b.current_page ).map(p => p.data).flat();
}
const SSR_getEvents = async (baseUrl, summitId, accessToken) => {
const endpoint = `${baseUrl}/api/v1/summits/${summitId}/events/published`;
const params = {
access_token: accessToken,
per_page: 50,
page: 1,
expand: 'slides, links, videos, media_uploads, type, track, track.allowed_access_levels, location, location.venue, location.floor, speakers, moderator, sponsors, current_attendance, groups, rsvp_template, tags',
}
return await axios.get(endpoint, { params }).then(async ({data}) => {
console.log(`SSR_getEvents then data.current_page ${data.current_page} data.last_page ${data.last_page} total ${data.total}`)
let remainingPages = await SSR_GetRemainingPages(endpoint, params, data.last_page);
return [...data.data, ...remainingPages];
}).catch(e => console.log('ERROR: ', e));
};
const SSR_getSpeakers = async (baseUrl, summitId, accessToken, filter = null) => {
const params = {
access_token: accessToken,
per_page: 30,
page: 1,
};
const endpoint = `${baseUrl}/api/v1/summits/${summitId}/speakers/on-schedule`;
if (filter) {
params['filter[]'] = filter;
}
return await axios.get(
endpoint,
{ params }
)
.then(async ({data}) => {
console.log(`SSR_getSpeakers then data.current_page ${data.current_page} data.last_page ${data.last_page} total ${data.total}`)
let remainingPages = await SSR_GetRemainingPages(endpoint, params, data.last_page);
return [ ...data.data, ...remainingPages];
})
.catch(e => console.log('ERROR: ', e));
};
const SSR_getSummit = async (baseUrl, summitId) => {
const params = {
expand: 'event_types,tracks,track_groups,presentation_levels,locations.rooms,locations.floors,order_extra_questions.values,schedule_settings,schedule_settings.filters,schedule_settings.pre_filters',
t: Date.now()
};
return await axios.get(
`${baseUrl}/api/public/v1/summits/${summitId}`,
{ params }
)
.then(({ data }) => data)
.catch(e => console.log('ERROR: ', e));
};
const SSR_getVoteablePresentations = async (baseUrl, summitId, accessToken) => {
const endpoint = `${baseUrl}/api/v1/summits/${summitId}/presentations/voteable`;
const params = {
access_token: accessToken,
per_page: 50,
page: 1,
filter: 'published==1',
expand: 'slides, links, videos, media_uploads, type, track, track.allowed_access_levels, location, location.venue, location.floor, speakers, moderator, sponsors, current_attendance, groups, rsvp_template, tags',
};
return await axios.get(endpoint,
{ params }).then(async ({data}) => {
console.log(`SSR_getVoteablePresentations then data.current_page ${data.current_page} data.last_page ${data.last_page} total ${data.total}`)
let remainingPages = await SSR_GetRemainingPages(endpoint, params, data.last_page);
return [...data.data, ...remainingPages];
})
.catch(e => console.log('ERROR: ', e));
};
exports.onPreBootstrap = async () => {
console.log('onPreBootstrap');
const summitId = process.env.GATSBY_SUMMIT_ID;
const summitApiBaseUrl = process.env.GATSBY_SUMMIT_API_BASE_URL;
const marketingData = await SSR_getMarketingSettings(process.env.GATSBY_MARKETING_API_BASE_URL, process.env.GATSBY_SUMMIT_ID);
const colorSettings = fs.existsSync(colorsFilePath) ? JSON.parse(fs.readFileSync(colorsFilePath)) : {};
const disqusSettings = fs.existsSync(disqusFilePath) ? JSON.parse(fs.readFileSync(disqusFilePath)) : {};
const marketingSite = fs.existsSync(marketingFilePath) ? JSON.parse(fs.readFileSync(marketingFilePath)) : {};
const homeSettings = fs.existsSync(homeFilePath) ? JSON.parse(fs.readFileSync(homeFilePath)) : {};
const globalSettings = fs.existsSync(settingsFilePath) ? JSON.parse(fs.readFileSync(settingsFilePath)) : {};
const config = {
client: {
id: process.env.GATSBY_OAUTH2_CLIENT_ID_BUILD,
secret: process.env.GATSBY_OAUTH2_CLIENT_SECRET_BUILD
},
auth: {
tokenHost: process.env.GATSBY_IDP_BASE_URL,
tokenPath: process.env.GATSBY_OAUTH_TOKEN_PATH
},
options: {
authorizationMethod: 'header'
}
};
const accessToken = await getAccessToken(config, process.env.GATSBY_BUILD_SCOPES).then(({ token }) => token.access_token);
// Marketing Settings
marketingData.map(({ key, value }) => {
if (key.startsWith('color_')) colorSettings[key] = value;
if (key.startsWith('disqus_')) disqusSettings[key] = value;
if (key.startsWith('summit_')) marketingSite[key] = value;
if (key === 'REG_LITE_COMPANY_INPUT_PLACEHOLDER') marketingSite[key] = value;
if (key === 'REG_LITE_COMPANY_DDL_PLACEHOLDER') marketingSite[key] = value;
if (key === 'REG_LITE_ALLOW_PROMO_CODES') marketingSite[key] = !!Number(value);
if (key === 'schedule_default_image') homeSettings.schedule_default_image = value;
if (key === 'registration_in_person_disclaimer') marketingSite[key] = value;
if (key === 'ACTIVITY_CTA_TEXT') marketingSite[key] = value;
});
// Set the size property on marketing settings masonry if it's needed
const migrateMasonry = (masonry) => {
const sizeRequired = masonry.some(i => !i.hasOwnProperty("size"));
if (sizeRequired) {
return masonry.map((i) => {
isSingle = masonry.some(img => sizeOf(`./static${img.images[0].image}`).height > sizeOf(`./static${i.images[0].image}`).height);
return { ...i, size: isSingle ? 1: 2 }
})
}
return masonry;
}
Object.keys(marketingSite).map((key) => {
if (key === 'sponsors') marketingSite[key] = migrateMasonry(marketingSite[key]);
});
fs.writeFileSync(colorsFilePath, JSON.stringify(colorSettings), 'utf8');
fs.writeFileSync(disqusFilePath, JSON.stringify(disqusSettings), 'utf8');
fs.writeFileSync(marketingFilePath, JSON.stringify(marketingSite), 'utf8');
fs.writeFileSync(homeFilePath, JSON.stringify(homeSettings), 'utf8');
let sassColors = '';
Object.entries(colorSettings).forEach(([key, value]) => sassColors += `$${key} : ${value};\n`);
fs.writeFileSync('src/styles/colors.scss', sassColors, 'utf8');
// summit
const summit = await SSR_getSummit(summitApiBaseUrl, summitId);
fileBuildTimes.push(
{
'file' : summitFilePath,
'build_time': Date.now()
});
fs.writeFileSync(summitFilePath, JSON.stringify(summit), 'utf8');
// Show Events
const allEvents = await SSR_getEvents(summitApiBaseUrl, summitId, accessToken);
fileBuildTimes.push(
{
'file': eventsFilePath,
'build_time': Date.now()
});
console.log(`allEvents ${allEvents.length}`);
fs.writeFileSync(eventsFilePath, JSON.stringify(allEvents), 'utf8');
const allEventsIDX = {};
allEvents.forEach((e, index) => allEventsIDX[e.id] = index);
fileBuildTimes.push(
{
'file': eventsIdxFilePath,
'build_time': Date.now()
});
fs.writeFileSync(eventsIdxFilePath, JSON.stringify(allEventsIDX), 'utf8');
// Show Speakers
const allSpeakers = await SSR_getSpeakers(summitApiBaseUrl, summitId, accessToken);
console.log(`allSpeakers ${allSpeakers.length}`);
fileBuildTimes.push(
{
'file': speakersFilePath,
'build_time': Date.now()
});
fs.writeFileSync(speakersFilePath, JSON.stringify(allSpeakers), 'utf8');
const allSpeakersIDX = {};
allSpeakers.forEach((e, index) => allSpeakersIDX[e.id] = index);
fileBuildTimes.push(
{
'file': speakersIdxFilePath,
'build_time': Date.now()
});
fs.writeFileSync(speakersIdxFilePath, JSON.stringify(allSpeakersIDX), 'utf8');
// Voteable Presentations
const allVoteablePresentations = await SSR_getVoteablePresentations(summitApiBaseUrl, summitId, accessToken);
console.log(`allVoteablePresentations ${allVoteablePresentations.length}`);
fileBuildTimes.push(
{
'file':voteablePresentationFilePath,
'build_time': Date.now()
});
fs.writeFileSync(voteablePresentationFilePath, JSON.stringify(allVoteablePresentations), 'utf8');
// setting build times
globalSettings.staticJsonFilesBuildTime = fileBuildTimes;
globalSettings.lastBuild = Date.now();
fs.writeFileSync(settingsFilePath, JSON.stringify(globalSettings), 'utf8');
};
// makes Summit logo optional for graphql queries
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;
const typeDefs = `
type Summit implements Node {
logo: String
}
`;
createTypes(typeDefs)
};
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions;
/**
* Gatsby v4 Upgrade NOTE: This is no longer needed in `gatsby-remark-relative-images` v2.
* @see https://www.npmjs.com/package/gatsby-remark-relative-images#v2-breaking-changes
*/
// fmImagesToRelative(node); // convert image paths for gatsby images
if (node.internal.type === `MarkdownRemark`) {
const value = createFilePath({ node, getNode });
createNodeField({
name: `slug`,
node,
value,
})
}
};
exports.sourceNodes = async ({
actions,
createNodeId,
createContentDigest
}) => {
console.log('sourceNodes');
const { createNode } = actions;
const summit = fs.existsSync(summitFilePath) ? JSON.parse(fs.readFileSync(summitFilePath)) : {};
const nodeContent = JSON.stringify(summit);
const nodeMeta = {
...summit,
id: createNodeId(`summit-${summit.id}`),
summit_id: summit.id,
parent: null,
children: [],
internal: {
type: `Summit`,
mediaType: `application/json`,
content: nodeContent,
contentDigest: createContentDigest(summit)
}
};
const node = Object.assign({}, summit, nodeMeta);
createNode(node);
};
exports.createPages = ({ actions, graphql }) => {
const { createPage, createRedirect } = actions;
const maintenanceMode = fs.existsSync(maintenanceFilePath) ?
JSON.parse(fs.readFileSync(maintenanceFilePath)) : { enabled: false };
// create a catch all redirect
if (maintenanceMode.enabled) {
createRedirect({
fromPath: '/*',
toPath: '/maintenance/'
});
}
return graphql(`
{
allMarkdownRemark(limit: 1000) {
edges {
node {
id
fields {
slug
}
frontmatter {
templateKey
}
}
}
}
}
`).then((result) => {
const {
errors,
data: {
allMarkdownRemark: {
edges
}
}
} = result;
if (errors) {
errors.forEach((e) => console.error(e.toString()));
return Promise.reject(errors);
}
edges.forEach((edge) => {
const { id, fields, frontmatter: { templateKey } } = edge.node;
var slug = fields.slug;
if (slug.match(/custom-pages/)) {
slug = slug.replace('/custom-pages/', '/');
}
const page = {
path: slug,
component: path.resolve(
`src/templates/${String(templateKey)}.js`
),
context: {
id,
},
};
// dont create pages if maintenance mode enabled
// gatsby disregards redirect if pages created for path
if (maintenanceMode.enabled && !page.path.match(/maintenance/)) return;
createPage(page);
});
});
};
exports.onCreateWebpackConfig = ({ actions, plugins, loaders }) => {
actions.setWebpackConfig({
resolve: {
/**
* Webpack removed automatic polyfills for these node APIs in v5,
* so we need to patch them in the browser.
* @see https://www.gatsbyjs.com/docs/reference/release-notes/migrating-from-v2-to-v3/#webpack-5-node-configuration-changed-nodefs-nodepath-
* @see https://viglucci.io/how-to-polyfill-buffer-with-webpack-5
*/
fallback: {
path: require.resolve('path-browserify'),
stream: require.resolve('stream-browserify'),
buffer: require.resolve('buffer/')
}
},
// canvas is a jsdom external dependency
externals: ['canvas'],
experiments: {
topLevelAwait: true,
},
// devtool: 'source-map',
plugins: [
plugins.define({
'global.GENTLY': false,
'global.BLOB': false
}),
new webpack.ProvidePlugin({
Buffer: ['buffer', 'Buffer'],
}),
// upload source maps only if we have an sentry auth token and we are at production
...('GATSBY_SENTRY_AUTH_TOKEN' in process.env && process.env.NODE_ENV === 'production') ?[
new SentryWebpackPlugin({
org: process.env.GATSBY_SENTRY_ORG,
project: process.env.GATSBY_SENTRY_PROJECT,
ignore: ["app-*", "polyfill-*", "framework-*", "webpack-runtime-*","~partytown"],
// Specify the directory containing build artifacts
include: [
{
paths: ['src','public','.cache'],
urlPrefix: '~/',
},
{
paths: ['node_modules/upcoming-events-widget/dist'],
urlPrefix: '~/node_modules/upcoming-events-widget/dist',
},
{
paths: ['node_modules/summit-registration-lite/dist'],
urlPrefix: '~/node_modules/summit-registration-lite/dist',
},
{
paths: ['node_modules/full-schedule-widget/dist'],
urlPrefix: '~/node_modules/full-schedule-widget//dist',
},
{
paths: ['node_modules/schedule-filter-widget/dist'],
urlPrefix: '~/node_modules/schedule-filter-widget/dist',
},
{
paths: ['node_modules/lite-schedule-widget/dist'],
urlPrefix: '~/node_modules/lite-schedule-widget/dist',
},
{
paths: ['node_modules/live-event-widget/dist'],
urlPrefix: '~/node_modules/live-event-widget/dist',
},
{
paths: ['node_modules/attendee-to-attendee-widget/dist'],
urlPrefix: '~/node_modules/attendee-to-attendee-widget/dist',
},
{
paths: ['node_modules/openstack-uicore-foundation/lib'],
urlPrefix: '~/node_modules/openstack-uicore-foundation/lib',
},
{
paths: ['node_modules/speakers-widget/dist'],
urlPrefix: '~/node_modules/speakers-widget/dist',
},
],
// Auth tokens can be obtained from https://sentry.io/settings/account/api/auth-tokens/
// and needs the `project:releases` and `org:read` scopes
authToken: process.env.GATSBY_SENTRY_AUTH_TOKEN,
// Optionally uncomment the line below to override automatic release name detection
release: process.env.GATSBY_SENTRY_RELEASE,
})]:[],
]
});
};