forked from mongodb/snooty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
165 lines (140 loc) · 5.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
const path = require('path');
const { initStitch } = require('./src/utils/setup/init-stitch');
const { saveAssetFiles, saveStaticFiles } = require('./src/utils/setup/save-asset-files');
const { validateEnvVariables } = require('./src/utils/setup/validate-env-variables');
const { getNestedValue } = require('./src/utils/get-nested-value');
const { getGuideMetadata } = require('./src/utils/get-guide-metadata');
const { getPageSlug } = require('./src/utils/get-page-slug');
const { siteMetadata } = require('./src/utils/site-metadata');
const { assertTrailingSlash } = require('./src/utils/assert-trailing-slash');
const { DOCUMENTS_COLLECTION, METADATA_COLLECTION } = require('./src/build-constants');
const DB = siteMetadata.database;
const constructPageIdPrefix = ({ project, parserUser, parserBranch }) => `${project}/${parserUser}/${parserBranch}`;
const constructBuildFilter = ({ commitHash, patchId, ...rest }) => {
const pageIdPrefix = constructPageIdPrefix(rest);
return {
page_id: { $regex: new RegExp(`^${pageIdPrefix}/*`) },
commit_hash: commitHash || { $exists: false },
patch_id: patchId || { $exists: false },
};
};
const buildFilter = constructBuildFilter(siteMetadata);
// different types of references
const PAGES = [];
const GUIDES_METADATA = {};
// in-memory object with key/value = filename/document
let RESOLVED_REF_DOC_MAPPING = {};
// stich client connection
let stitchClient;
const assets = new Map();
exports.sourceNodes = async () => {
// setup env variables
const envResults = validateEnvVariables();
if (envResults.error) {
throw Error(envResults.message);
}
// wait to connect to stitch
stitchClient = await initStitch();
const documents = await stitchClient.callFunction('fetchDocuments', [DB, DOCUMENTS_COLLECTION, buildFilter]);
if (documents.length === 0) {
console.error('No documents matched your query.');
process.exit(1);
}
const pageIdPrefix = constructPageIdPrefix(siteMetadata);
documents.forEach(doc => {
const { page_id, ...rest } = doc;
RESOLVED_REF_DOC_MAPPING[page_id.replace(`${pageIdPrefix}/`, '')] = rest;
});
// Identify page documents and parse each document for images
Object.entries(RESOLVED_REF_DOC_MAPPING).forEach(([key, val]) => {
const pageNode = getNestedValue(['ast', 'children'], val);
const filename = getNestedValue(['filename'], val) || '';
if (pageNode) {
val.static_assets.forEach(asset => {
const checksum = asset.checksum;
if (assets.has(checksum)) {
assets.set(checksum, new Set([...assets.get(checksum), asset.key]));
} else {
assets.set(checksum, new Set([asset.key]));
}
});
}
if (filename.endsWith('.txt')) {
PAGES.push(key);
if (process.env.GATSBY_SITE === 'guides') {
GUIDES_METADATA[key] = getGuideMetadata(val);
}
}
});
};
exports.createPages = async ({ actions }) => {
const { createPage } = actions;
const [, metadata] = await Promise.all([
saveAssetFiles(assets, stitchClient),
stitchClient.callFunction('fetchDocument', [DB, METADATA_COLLECTION, buildFilter]),
]);
// Save files in the static_files field of metadata document, including intersphinx inventories
if (metadata.static_files) {
await saveStaticFiles(metadata.static_files);
}
return new Promise((resolve, reject) => {
PAGES.forEach(page => {
const pageNodes = RESOLVED_REF_DOC_MAPPING[page];
const slug = getPageSlug(page);
if (RESOLVED_REF_DOC_MAPPING[page] && Object.keys(RESOLVED_REF_DOC_MAPPING[page]).length > 0) {
createPage({
path: assertTrailingSlash(slug),
component: path.resolve(__dirname, './src/components/DocumentBody.js'),
context: {
metadata,
slug,
template: getNestedValue(['ast', 'options', 'template'], pageNodes),
page: pageNodes,
guidesMetadata: GUIDES_METADATA,
},
});
}
});
// Instead of building a 404 page for every docs site, we want one for only docs-landing
if (process.env.GATSBY_SITE === 'landing') {
const slug = '/404';
createPage({
path: slug,
component: path.resolve(__dirname, './src/components/NotFound.js'),
context: {
layout: '404',
metadata,
page: {
ast: {},
},
slug,
},
});
}
resolve();
});
};
// Prevent errors when running gatsby build caused by browser packages run in a node environment.
exports.onCreateWebpackConfig = ({ stage, loaders, actions }) => {
if (stage === 'build-html') {
actions.setWebpackConfig({
module: {
rules: [
{
test: /mongodb-stitch-browser-sdk/,
use: loaders.null(),
},
],
},
});
}
};
// Remove type inference, as our schema is too ambiguous for this to be useful.
// https://www.gatsbyjs.com/docs/scaling-issues/#switch-off-type-inference-for-sitepagecontext
exports.createSchemaCustomization = ({ actions }) => {
actions.createTypes(`
type SitePage implements Node @dontInfer {
path: String!
}
`);
};