-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgatsby-node.ts
275 lines (231 loc) · 6.52 KB
/
gatsby-node.ts
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
/* eslint-disable import/no-import-module-exports */
import fs from 'node:fs';
import path from 'node:path';
import { GatsbyNode, NodePluginArgs } from 'gatsby';
import axios from 'axios';
import keyBy from 'lodash/keyBy';
import {
ContentfulRichTextGatsbyReference,
RenderRichTextData,
} from 'gatsby-source-contentful/rich-text';
import { ContactUsConfig, OfferingPlanDto } from './src/utils/types';
import { contactUsBaseConfigs } from './src/utils/contactUsConfig';
// importing GraphQL fragments to be available in the app
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import * as fragments from './src/fragments';
interface Slug {
slug: string;
}
interface PostTypeDto {
allContentfulBlogPost: {
nodes: Slug[];
};
}
interface CaseTypeDto {
allContentfulCaseStudy: {
nodes: Slug[];
};
}
interface Repos {
total: number;
repos: Record<string, string>;
}
interface ContactUsDto {
internalTitle: string;
title: string;
message: RenderRichTextData<ContentfulRichTextGatsbyReference>;
messagePosition: string;
offeringPlan?: OfferingPlanDto;
}
interface ContactUsQuery {
allContentfulContactUs: {
nodes: ContactUsDto[];
};
}
const validThumbnailKeys = ['default', 'high', 'maxres', 'medium', 'standard'] as const;
type ValidThumbnailKeysType = (typeof validThumbnailKeys)[number];
type Thumbnail = Record<
ValidThumbnailKeysType,
{
height: number;
width: number;
url: string;
}
>;
interface YoutubeVideoDto {
id: string;
title: string;
duration: string;
published_at: string;
statistics: {
comment_count: number;
like_count: number;
view_count: number;
};
thumbnail: Partial<Thumbnail>;
}
const acceleratorsTemplatesPath = './src/templates/accelerators';
const pricingTemplatesPath = './src/templates/pricing';
const sponsorsTemplatesPath = './src/templates/sponsorship-program';
export const createPages: GatsbyNode['createPages'] = async ({ graphql, actions, reporter }) => {
const { createPage } = actions;
await axios
.get('https://status.reportportal.io/github/stars')
.then((response: { data: Repos }) => response.data)
.then((data: Repos) => {
fs.writeFileSync('static/github.json', JSON.stringify(data));
});
await axios
.get('https://status.reportportal.io/youtube?count=12')
.then((response: { data: YoutubeVideoDto[] }) => response.data)
.then(data => {
fs.writeFileSync('static/youtube.json', JSON.stringify(data));
});
const blogPost = path.resolve('./src/templates/blog-post/blog-post.tsx');
const blogsResponse = await graphql<PostTypeDto>(
`
{
allContentfulBlogPost {
nodes {
slug
}
}
}
`,
);
if (blogsResponse.errors) {
reporter.panicOnBuild('There was an error loading your Contentful posts', blogsResponse.errors);
return;
}
const posts = blogsResponse.data?.allContentfulBlogPost.nodes;
// Create blog posts pages
// But only if there's at least one blog post found in Contentful
// `context` is available in the template as a prop and as a variable in GraphQL
posts?.forEach(post => {
createPage({
path: `/blog/${post.slug}/`,
component: blogPost,
context: {
slug: post.slug,
},
});
});
const ContactUsPage = path.resolve('./src/templates/contact-us/contact-us.tsx');
const contactUsResponse = await graphql<ContactUsQuery>(
`
{
allContentfulContactUs {
nodes {
... on ContentfulContactUs {
internalTitle
title
messagePosition
message {
raw
}
offeringPlan {
price {
currency
period
yearly
quarterly
}
}
}
}
}
}
`,
);
if (contactUsResponse.errors) {
reporter.panicOnBuild(
'There was an error loading Contentful contact us configs',
contactUsResponse.errors,
);
return;
}
const contactUsConfigs = keyBy(
contactUsResponse.data?.allContentfulContactUs.nodes as ContactUsDto[],
'internalTitle',
);
contactUsBaseConfigs.forEach(config => {
const contentfulConfig = contactUsConfigs[config.id];
const contactUsProps: ContactUsConfig = {
...config,
title: contentfulConfig.title,
message: contentfulConfig.message,
messagePosition: contentfulConfig.messagePosition,
price: contentfulConfig.offeringPlan?.price,
};
createPage({
path: config.url,
component: ContactUsPage,
context: contactUsProps,
});
});
const caseStudyTemplate = path.resolve('./src/templates/case-study/case-study.tsx');
const caseStudiesResponse = await graphql<CaseTypeDto>(
`
{
allContentfulCaseStudy {
nodes {
slug
}
}
}
`,
);
if (caseStudiesResponse.errors) {
reporter.panicOnBuild(
'There was an error loading your Contentful case studies',
caseStudiesResponse.errors,
);
return;
}
const caseStudies = caseStudiesResponse.data?.allContentfulCaseStudy.nodes;
caseStudies?.forEach(caseStudy => {
createPage({
path: `/case-studies/${caseStudy.slug}/`,
component: caseStudyTemplate,
context: {
slug: caseStudy.slug,
},
});
});
fs.readdirSync(acceleratorsTemplatesPath).forEach(file => {
const key = path.basename(file, '.tsx');
createPage({
path: `/accelerators/${key}/`,
component: path.resolve(path.join(acceleratorsTemplatesPath, file)),
});
});
fs.readdirSync(pricingTemplatesPath).forEach(file => {
const key = path.basename(file, '.tsx');
createPage({
path: `/pricing/${key}/`,
component: path.resolve(path.join(pricingTemplatesPath, file)),
});
});
fs.readdirSync(sponsorsTemplatesPath).forEach(file => {
const key = path.basename(file, '.tsx');
createPage({
path: `/sponsorship-program/${key}/`,
component: path.resolve(path.join(sponsorsTemplatesPath, file)),
});
});
};
exports.onCreateWebpackConfig = ({ actions }: NodePluginArgs) => {
actions.setWebpackConfig({
resolve: {
alias: {
'@app': path.resolve(__dirname, 'src'),
},
},
});
};
exports.onPostBuild = () => {
// Remove autogenerated `sitemap-index.xml` in favor of the existing one (sitemap.xml)
if (fs.existsSync('./public/sitemap-index.xml')) {
fs.unlinkSync('./public/sitemap-index.xml');
}
};