-
Notifications
You must be signed in to change notification settings - Fork 15
/
gatsby-node.js
103 lines (97 loc) · 3.04 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
/* eslint-disable no-param-reassign */
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
const slugify = require('slugify');
const path = require('path');
const fs = require('fs');
const _ = require('lodash');
const webpack = require('webpack');
const db = JSON.parse(fs.readFileSync('./src/db/catalog.json'));
const slug = (d) => slugify(d, { replacement: '-', remove: /[#,.:?()'"/]/g, lower: true }).toLowerCase();
exports.createPages = async ({ actions }) => {
const { createPage } = actions;
const makerTpl = require.resolve('./src/pages-dynamic/maker.js');
const cwTpl = require.resolve('./src/pages-dynamic/colorway.js');
const sculptTpl = require.resolve('./src/pages-dynamic/sculpt.js');
/**
* Artisan catalog
*/
db.forEach((maker) => {
maker.sculpts.forEach((element) => {
element.link = `/maker/${slug(maker.name)}/${slug(element.name)}`;
const rng = Math.floor(Math.random() * element.colorways.length);
const f = element.colorways.find((x) => x.isCover === true);
if (f) {
element.previewImg = `https://cdn.keycap-archivist.com/keycaps/250/${f.id}.jpg`;
} else if (element.colorways[rng]) {
element.previewImg = `https://cdn.keycap-archivist.com/keycaps/250/${element.colorways[rng].id}.jpg`;
}
});
const makerLightObj = _.cloneDeep(maker);
makerLightObj.sculpts.forEach((s) => {
delete s.colorways;
});
const makerUrl = `/maker/${slug(maker.name)}`;
createPage({
path: makerUrl,
component: makerTpl,
context: {
maker: makerLightObj,
selfOrder: maker.selfOrder,
type: 'maker',
slug: makerUrl,
},
});
maker.sculpts.forEach((sculpt) => {
// Light maker object
const outMaker = {
...maker,
};
delete outMaker.sculpts;
createPage({
path: sculpt.link,
component: sculptTpl,
context: {
makerUrl,
denySubmission: maker.denySubmission,
selfOrder: maker.selfOrder,
type: 'sculpt',
sculpt,
maker: outMaker,
slug: sculpt.link,
},
});
sculpt.colorways.forEach((cw) => {
createPage({
path: `${sculpt.link}/${cw.id}`,
component: cwTpl,
context: {
makerUrl,
makerName: maker.name,
sculptName: sculpt.name,
sculptUrl: sculpt.link,
type: 'colorway',
colorway: cw,
slug: `${sculpt.link}/${cw.id}`,
},
});
});
});
});
};
exports.onCreateWebpackConfig = async ({ actions, plugins }) => {
const revision = fs.readFileSync(path.join(__dirname, 'catalog-revision.txt'), 'utf-8');
actions.setWebpackConfig({
resolve: { fallback: { process: require.resolve('process/browser') } },
plugins: [
plugins.define({ DBREV: JSON.stringify(revision) }),
new webpack.ProvidePlugin({
Buffer: ['buffer', 'Buffer'],
process: 'process',
}),
],
});
};