forked from Swizec/jamcommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
184 lines (168 loc) · 4.25 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
require('dotenv').config();
const _ = require('lodash');
const crypto = require('crypto');
const path = require('path');
const yaml = require('js-yaml');
const webpack = require('webpack');
const createSlug = _.flow([
s => s.toLowerCase(),
_.partialRight(_.replace, ' ', '-'),
_.partialRight(_.replace, '.', ''),
_.escape,
]);
exports.createPages = ({ graphql, boundActionCreators: { createPage } }) => {
const productsTemplate = path.resolve('src/templates/products.js');
const productTemplate = path.resolve('src/templates/product.js');
createPage({
path: '/women/shoes',
component: productsTemplate,
context: {
category: 'shoes',
directory: 'women',
},
});
return graphql(`
{
allJamProduct {
edges {
node {
id
name
path
}
}
}
}
`).then(({ errors, data: { allJamProduct: { edges } } } = null) => {
if (errors) {
throw errors;
}
edges.map(({ node }) => node).forEach(({ id, name, path }) => {
createPage({
path,
component: productTemplate,
context: {
id,
name,
},
});
});
return undefined;
});
};
function transformYaml(actions, loadNodeContent, node) {
const { createNode } = actions;
function objToNode(obj, i) {
const isCopy = /copy$/.test(node.dir);
const objStr = JSON.stringify(obj);
const contentDigest = crypto.createHash('md5').update(objStr).digest('hex');
const type = isCopy
? 'JAMCopy'
: _.upperFirst(_.camelCase(`${node.name}Yaml`));
let id = obj.id;
if (!id && isCopy) {
if (isCopy) {
id = `Copy-${node.name}`;
} else {
id = `${node.id} [${i}] >>> Yaml`;
}
}
const newNode = {
...obj,
id,
children: [],
parent: node.id,
internal: {
contentDigest,
// TODO make choosing the "type" a lot smarter. This assumes
// the parent node is a file.
// PascalCase
type,
},
};
if (isCopy) {
newNode.name = node.name;
}
return newNode;
}
return loadNodeContent(node)
.then(content => yaml.safeLoad(content))
.then(contents => {
if (!_.isArray(contents)) {
contents = [contents];
}
return contents.map(objToNode);
})
.then(nodes => nodes.map(child => createNode(child)));
}
const isProduct = node => node && node.frontmatter && node.frontmatter.images;
const createSrcset = (imgs, { alt, sources }, side) => {
imgs[side] = {
alt,
sources,
src: sources[0] && sources[0].src,
srcSet: sources.map(({ width, src }) => `${src} ${width}`).join(', '),
};
return imgs;
};
function createProductNodes(createNode, oldNode) {
const { frontmatter } = oldNode;
const slug = createSlug(frontmatter.name);
const node = {
...frontmatter,
children: [],
id: frontmatter.name,
slug,
sku: slug,
path: `/women/shoes/${slug}`,
title: frontmatter.name,
parent: oldNode.id,
thumbnails: _.reduce(frontmatter.thumbnails, createSrcset, {}),
images: _.reduce(frontmatter.images, createSrcset, {}),
internal: {
type: 'JAMProduct',
},
};
node.internal.contentDigest = crypto
.createHash('md5')
.update(JSON.stringify(node))
.digest('hex');
createNode(node);
}
exports.onCreateNode = ({
node,
loadNodeContent,
boundActionCreators: actions,
}) => {
if (node.internal.mediaType === 'text/yaml') {
return transformYaml(actions, loadNodeContent, node);
}
if (isProduct(node)) {
return createProductNodes(actions.createNode, node);
}
return undefined;
};
exports.modifyWebpackConfig = ({ config }) => {
// add env vars here
config.merge({
plugins: [
new webpack.DefinePlugin({
'process.env': {
GOCOMMERCE_URI: JSON.stringify(process.env.GOCOMMERCE_URI),
STRIPE_API_KEY: JSON.stringify(process.env.STRIPE_API_KEY),
},
}),
],
});
return config;
};
exports.onCreatePage = async ({ page, boundActionCreators }) => {
const { createPage } = boundActionCreators;
return new Promise((resolve, _) => {
if (page.path.match(/^\/account/)) {
page.layout = 'account';
createPage(page);
}
resolve();
});
};