-
Notifications
You must be signed in to change notification settings - Fork 17
/
gatsby-node.js
454 lines (428 loc) · 13.9 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
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
const path = require(`path`)
const fs = require(`fs-extra`)
const process = require(`process`)
const sharp = require(`sharp`)
const { slash } = require(`gatsby-core-utils`)
const { createFilePath } = require(`gatsby-source-filesystem`)
const Papa = require("papaparse")
const {
serviceSpecifications,
identifierToUrlPath,
DeviceCatalog,
isInfrastructure,
} = require(`./jacdac-ts/dist/jacdac.cjs`)
const { IgnorePlugin } = require("webpack")
const AVATAR_SIZE = 64
const LAZY_SIZE = 96
const PREVIEW_WIDTH = 480
const PREVIEW_HEIGHT = 360
const DEVICE_LIST_WIDTH = 240
const DEVICE_LIST_HEIGHT = 180
const CATALOG_HEIGHT = 600
const CATALOG_WIDTH = 800
const FULL_HEIGHT = 768
const FULL_WIDTH = 1024
const deviceCatalog = new DeviceCatalog()
async function createServicePages(graphql, actions, reporter) {
console.log(`generating service pages`)
const { createPage, createRedirect } = actions
const result = await graphql(`
{
allMdx(
filter: {
fields: { slug: { glob: "/clients/makecode/extensions/*" } }
}
) {
edges {
node {
fields {
slug
}
}
}
}
allServicesSourcesJson {
nodes {
source
classIdentifier
}
}
}
`)
if (result.errors) {
reporter.panicOnBuild('🚨 ERROR: Loading "createPages" query')
return
}
const makecodeSlugs = result.data.allMdx.edges.map(
edge => edge.node.fields.slug
)
// Create image post pages.
const serviceTemplate = path.resolve(`src/templates/service.tsx`)
const servicePlaygroundTemplate = path.resolve(
`src/templates/service-playground.tsx`
)
serviceSpecifications().map(node => {
const { classIdentifier, shortId } = node
const p = `/services/${shortId}/`
const pplay = `${p}playground/`
const r = `/services/0x${classIdentifier.toString(16)}`
const source = result.data.allServicesSourcesJson.nodes.find(
node => node.classIdentifier === classIdentifier
).source
const makecodeSlug = makecodeSlugs.find(
slug => slug === `/clients/makecode/extensions/${shortId}/`
)
createPage({
path: p,
component: slash(serviceTemplate),
context: {
classIdentifier,
source,
makecodeSlug,
title: node.name,
},
})
createPage({
path: pplay,
component: slash(servicePlaygroundTemplate),
context: {
node,
title: `${node.name} playground`,
},
})
//console.log(`service redirect`, { from: r, to: p })
createRedirect({
fromPath: r,
toPath: p,
})
})
}
async function createRedirects(actions) {
const { createRedirect } = actions
const rs = [
{
fromPath: `/tools/module-tester`,
toPath: `/tools/device-tester`,
},
{
fromPath: `/clients/p5js`,
toPath: `/clients/javascript/p5js`,
},
]
rs.forEach(r => createRedirect(r))
}
async function createDeviceQRPages(actions) {
console.log(`generating device QR pages`)
// legacy CSV file
const { createRedirect } = actions
const csv = fs.readFileSync(
"./jacdac-ts/jacdac-spec/devices/microsoft-research/qr-url-device-map.csv",
"utf-8"
)
const designidcol = "designid"
const productidcol = "productid"
const vanitycol = "vanityname"
const csvData = Papa.parse(csv, { header: true })
const data = csvData.data.filter(d => !!d[designidcol])
for (const qr of data) {
const vanity = qr[vanitycol].trim()
if (!vanity) continue
const productid = parseInt(qr[productidcol], 16)
const spec = deviceCatalog.specificationFromProductIdentifier(productid)
const p = `/devices/codes/${vanity}/`
const toPath = spec
? `/devices/0x${productid.toString(16)}/`
: `/devices/microsoft-research/`
const r = { fromPath: p, toPath }
//console.debug(`redirect ${r.fromPath} -> ${r.toPath}`)
await createRedirect(r)
}
// new way using msr codes
for (const spec of deviceCatalog.specifications().filter(
spec =>
spec.productIdentifiers?.length &&
!isNaN(parseInt(spec.designIdentifier))
)) {
const di = parseInt(spec.designIdentifier)
const vanity = `msr${di < 100 ? "0" : ""}${di < 10 ? "0" : ""}${di}`
const p = `/devices/codes/${vanity}/`
const toPath = `/devices/0x${spec.productIdentifiers[0].toString(16)}/`
const r = { fromPath: p, toPath }
//console.debug(`redirect ${r.fromPath} -> ${r.toPath}`)
await createRedirect(r)
}
console.log(`devices qr code redirect created`)
}
async function createDevicePages(graphql, actions, reporter) {
console.log(`generating device pages`)
const { createPage, createRedirect } = actions
const devices = deviceCatalog.specifications({
includeDeprecated: true,
includeExperimental: true,
})
// Create image post pages.
const deviceTemplate = path.resolve(`src/templates/device.tsx`)
const companyTemplate = path.resolve(`src/templates/device-company.tsx`)
for (const node of devices) {
const p = `/devices/${identifierToUrlPath(node.id)}/`
console.log(`${node.id} -> ${p}`)
createPage({
path: p,
component: slash(deviceTemplate),
context: {
node,
title: node.name,
},
})
// adding firmware identifier redirects
if (node.productIdentifiers)
node.productIdentifiers.forEach(fw => {
const dp = `/devices/0x${fw.toString(16)}`
createRedirect({
fromPath: dp,
toPath: p,
})
})
// copy device image to static
const nodePath = identifierToUrlPath(node.id)
const imgpath = nodePath + ".jpg"
const imgsrc = `./jacdac-ts/jacdac-spec/devices/${imgpath}`
//console.log(`image ${node.id} ${nodePath} -> ${imgpath}`)
await fs.copy(imgsrc, `./public/images/devices/${imgpath}`)
await sharp(imgsrc)
.resize(CATALOG_WIDTH, CATALOG_HEIGHT, {
fit: sharp.fit.cover,
})
.toFormat("jpeg")
.toFile(`./public/images/devices/${nodePath}.catalog.jpg`)
await sharp(imgsrc)
.resize(PREVIEW_WIDTH, PREVIEW_HEIGHT, {
fit: sharp.fit.cover,
})
.toFormat("jpeg")
.toFile(`./public/images/devices/${nodePath}.preview.jpg`)
await sharp(imgsrc)
.resize(FULL_WIDTH, FULL_HEIGHT, {
fit: sharp.fit.cover,
})
.toFormat("jpeg")
.toFile(`./public/images/devices/${nodePath}.full.jpg`)
await sharp(imgsrc)
.resize(LAZY_SIZE, LAZY_SIZE, {
fit: sharp.fit.cover,
})
.toFormat("jpeg")
.toFile(`./public/images/devices/${nodePath}.lazy.jpg`)
await sharp(imgsrc)
.resize(DEVICE_LIST_WIDTH, DEVICE_LIST_HEIGHT, {
fit: sharp.fit.cover,
})
.toFormat("jpeg")
.toFile(`./public/images/devices/${nodePath}.list.jpg`)
await sharp(imgsrc)
.resize(AVATAR_SIZE, AVATAR_SIZE, { fit: sharp.fit.cover })
.toFormat("jpeg")
.toFile(`./public/images/devices/${nodePath}.avatar.jpg`)
}
const snakify = name => {
return name.replace(/([a-z])([A-Z])/g, (_, a, b) => a + "_" + b)
}
const escapeDeviceIdentifier = text => {
if (!text) text = ""
const escaped = text
.trim()
.toLowerCase()
.replace(/([^a-z0-9\_-])+/gi, "-")
.replace(/^-+/, "")
.replace(/-+$/, "")
const id = snakify(escaped)
return id
}
// create device company routes
const companies = {}
devices.forEach(
node => (companies[escapeDeviceIdentifier(node.company)] = node.company)
)
//console.log(companies)
for (const cp of Object.keys(companies)) {
const company = companies[cp]
const p = `/devices/${cp}`
//console.log(`device company page`, { p })
createPage({
path: p,
component: slash(companyTemplate),
context: {
company: company,
title: `${company} devices`,
},
})
}
}
async function generateServicesJSON() {
const dir = "./public"
const services = serviceSpecifications()
// JSON
for (const srv of services) {
const f = path.join(
dir,
"services",
`x${srv.classIdentifier.toString(16)}.json`
)
await fs.outputFile(f, JSON.stringify(srv, null, 2))
}
for (const srv of services) {
const f = path.join(
dir,
"services",
"lite",
`x${srv.classIdentifier.toString(16)}.json`
)
const clone = JSON.parse(JSON.stringify(srv))
delete clone.notes
delete clone.enums
delete clone.constants
delete clone.extends
delete clone.shortName
delete clone.name
delete clone.tags
for (var pkt of clone.packets) {
delete pkt.description
delete pkt.derived
delete pkt.identifierName
if (pkt.fields.length <= 1) delete pkt.fields
else pkt.fields = pkt.fields.map(f => f.name)
}
await fs.outputFile(f, JSON.stringify(clone, null, 2))
}
}
async function createWorkers() {
// copy jacdac-worker.js to static
// include version number to bust out caching
const jacdacTsPackageJson = fs.readJsonSync(`./jacdac-ts/package.json`)
await fs.copy(
`./jacdac-ts/dist/jacdac-worker.js`,
`./public/jacdac-worker-${jacdacTsPackageJson.version}.js`
)
}
async function createVersions() {
await fs.outputFile(
`./public/version.json`,
JSON.stringify({
sha: process.env.GATSBY_GITHUB_SHA,
})
)
}
// Implement the Gatsby API “createPages”. This is
// called after the Gatsby bootstrap is finished so you have
// access to any information necessary to programmatically
// create pages.
exports.createPages = async ({ graphql, actions, reporter }) => {
await generateServicesJSON()
await createServicePages(graphql, actions, reporter)
await createDevicePages(graphql, actions, reporter)
await createDeviceQRPages(actions, reporter)
await createWorkers()
await createVersions()
await createRedirects(actions)
}
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions
//console.log(`${node.internal.type} -> ${node.value}`)
if (node.internal.type === `Mdx`) {
const value = createFilePath({ node, getNode })
createNodeField({
name: `slug`,
node,
value,
})
if (node.frontmatter && !node.frontmatter.title) {
const heading = /#\s*([^\n]+)/.exec(node.rawBody)
if (heading) node.frontmatter.title = heading[1].trim()
}
}
}
exports.onCreateWebpackConfig = ({ stage, actions, getConfig }) => {
const { setWebpackConfig, replaceWebpackConfig } = actions
//console.log({ stage })
const plugins = [
new IgnorePlugin({
resourceRegExp: /^canvas$/,
}),
]
const fallback = {
crypto: false,
util: require.resolve("util/"),
assert: require.resolve("assert/"),
fs: false,
net: false,
webusb: false,
}
if (stage.startsWith("develop")) {
setWebpackConfig({
resolve: {
fallback,
},
plugins,
})
}
if (stage === "build-javascript" || stage === "build-html") {
console.log(`enabling ignore filters`)
setWebpackConfig({
node: {
fs: "empty",
},
resolve: {
fallback,
},
plugins,
})
}
// enable verbose logging
const config = getConfig()
config.stats = "verbose"
config.performance.hints = "warning"
replaceWebpackConfig(config)
// final webpack
//console.log({ webpack: getConfig() })
}
// generate a full list of pages for compliance
exports.onPostBuild = async ({ graphql }) => {
console.log(`compliance step`)
const { data } = await graphql(`
{
pages: allSitePage {
nodes {
path
}
}
}
`)
await fs.writeFile(
path.resolve(__dirname, ".cache/all-pages.csv"),
data.pages.nodes
.map(
node =>
`${
"https://microsoft.github.io/jacdac-docs" + node.path
}, ${node.path.slice(1)}`
)
.join("\n")
)
await fs.writeFile(
path.resolve(__dirname, ".cache/top-pages.csv"),
data.pages.nodes
.filter(
node =>
node.path.slice(1).replace(/\/$/, "").split(/\//g).length <
2 &&
node.path.indexOf("offline-plugin-app-shell-fallback") < 0
)
.map(
node =>
`${
"https://microsoft.github.io/jacdac-docs" + node.path
}, ${node.path.slice(1)}`
)
.join("\n")
)
}