-
Notifications
You must be signed in to change notification settings - Fork 36
/
gatsby-node.js
245 lines (224 loc) · 6.22 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
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
require("source-map-support").install()
require("ts-node").register({
compilerOptions: {
module: "commonjs",
target: "es2017",
},
})
const fs = require("fs")
const path = require("path")
const Router = require("./src/lib/router").Router
let router = new Router()
// This is added so we can get the contents of the snippets files in our Snippet component
exports.onCreateNode = ({ node, actions }) => {
const { createNodeField } = actions
if (node.internal.type === "File" && node.absolutePath.match(/snippets/)) {
createNodeField({
node,
name: "content",
value: fs.readFileSync(node.absolutePath, "utf8"),
})
}
}
exports.createPages = ({ actions }) => {
const { createPage, createRedirect } = actions
const createAppPage = function (url) {
createPage({
path: url, // dont shadow path module with local path variable
matchPath: "/*",
component: path.resolve(`./src/routes/app.js`),
})
}
createAppPage("404")
// Loop over all routes in the router
// 1. If the route is a page, create a page using gatsby API
// 2. If the route is a parent, redirect the parent URL to the first child page
router.allRoutes
.filter((route) => !route.isDynamic)
.forEach((route) => {
if (route.isPage) {
createAppPage(route.url)
} else {
let firstPage = route.pages[0]
if (!firstPage.isDynamic) {
createRedirect({
fromPath: route.fullPath,
toPath: firstPage.url,
})
}
}
})
// TODO: Create all API docs pages dynamically
createAppPage("/api/classes/association")
createAppPage("/api/classes/collection")
createAppPage("/api/classes/db")
createAppPage("/api/classes/db-collection")
createAppPage("/api/classes/identity-manager")
createAppPage("/api/classes/jsonapi-serializer")
createAppPage("/api/classes/model")
createAppPage("/api/classes/response")
createAppPage("/api/classes/schema")
createAppPage("/api/classes/serializer")
createAppPage("/api/classes/server")
createRedirect({
fromPath: "/api",
toPath: "/api/classes/association",
})
createRedirect({
fromPath: "/api/classes",
toPath: "/api/classes/association",
})
// Old links
createRedirect({
fromPath: "/quickstarts/react/development",
toPath: "/quickstarts/react/develop-a-component",
})
createRedirect({
fromPath: "/quickstarts/react/react-testing-library",
toPath: "/quickstarts/react/test-a-component-with-react-testing-library",
})
createRedirect({
fromPath: "/quickstarts/vue/cypress",
toPath: "/quickstarts/cypress",
})
createRedirect({
fromPath: "/quickstarts/cypress/setup",
toPath: "/quickstarts/cypress",
})
createRedirect({
fromPath: "/docs/route-handlers/functions",
toPath: "/docs/main-concepts/route-handlers",
})
createRedirect({
fromPath: "/docs/route-handlers/shorthands",
toPath: "/docs/main-concepts/shorthands",
})
createRedirect({
fromPath: "/docs/data-layer/database",
toPath: "/docs/main-concepts/database",
})
createRedirect({
fromPath: "/docs/data-layer/orm",
toPath: "/docs/main-concepts/orm",
})
createRedirect({
fromPath: "/docs/data-layer/models",
toPath: "/docs/main-concepts/models",
})
createRedirect({
fromPath: "/docs/data-layer/relationships",
toPath: "/docs/main-concepts/relationships",
})
createRedirect({
fromPath: "/docs/data-layer/factories",
toPath: "/docs/main-concepts/factories",
})
createRedirect({
fromPath: "/docs/data-layer/fixtures",
toPath: "/docs/main-concepts/fixtures",
})
createRedirect({
fromPath: "/docs/data-layer/serializers",
toPath: "/docs/main-concepts/serializers",
})
createRedirect({
fromPath: "/docs/meta/comparison-with-other-tools",
toPath: "/docs/comparison-with-other-tools",
})
createRedirect({
fromPath: "/docs/meta/about",
toPath: "/docs/about",
})
// Netlify Redirects
createAppPage("/repl/v1/ssr-shell")
createAppPage("/repl/v2/ssr-shell")
createRedirect({
fromPath: "/repl/v1/*",
toPath: "/repl/v1/ssr-shell",
statusCode: 200,
})
createRedirect({
fromPath: "/repl/v2/*",
toPath: "/repl/v2/ssr-shell",
statusCode: 200,
})
createRedirect({
fromPath: "/*",
toPath: "/404.html",
statusCode: 404,
})
}
// DOC STUFF TODO Extract
let esdoc = require("esdoc").default
let tmp = require("tmp")
let slugify = function (str) {
return str
.replace(/[^a-zA-Z0-9]+/g, "-")
.replace(/([A-Z]+)([A-Z][a-z])/g, "$1-$2")
.replace(/([a-z])([A-Z])/g, "$1-$2")
.replace(/([0-9])([^0-9])/g, "$1-$2")
.replace(/([^0-9])([0-9])/g, "$1-$2")
.replace(/-+/g, "-")
.toLowerCase()
}
let generateESDoc = function (config) {
var tmpdir = tmp.dirSync({ unsafeCleanup: true })
let originalLog = console.log
console.log("Generating ESDoc index")
console.log = () => {}
esdoc.generate({ ...config, ...{ destination: tmpdir.name } })
console.log = originalLog
let index = fs.readFileSync(`${tmpdir.name}/index.json`)
let result = JSON.parse(index)
tmpdir.removeCallback()
return result
}
exports.sourceNodes = async ({
actions: { createNode },
createNodeId,
createContentDigest,
}) => {
let docNodes = generateESDoc({
source: "./node_modules/miragejs/lib",
excludes: ["(node_modules|tests|tmp)"],
plugins: [
{
name: "esdoc-ecmascript-proposal-plugin",
option: {
classProperties: true,
objectRestSpread: true,
doExpressions: true,
functionBind: true,
functionSent: true,
asyncGenerators: true,
decorators: true,
exportExtensions: true,
dynamicImport: true,
},
},
{ name: "esdoc-accessor-plugin" },
],
})
docNodes.forEach((docNode) => {
let node = {
...{ slug: slugify(docNode.name) },
...docNode,
}
let data = {
...node,
...{
id: createNodeId(`esdoc-${node.__docId__}`),
internal: {
type: "ESDoc",
contentDigest: createContentDigest(JSON.stringify(node)),
},
},
}
createNode(data)
})
}