-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
290 lines (255 loc) · 10.4 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
const axios = require("./src/AxiosHelper");
const { mergeData, createNodeManifest } = require("./src/DataHelper");
const moment = require("moment");
const package = require("./package.json");
exports.onPreInit = () => console.log(`Loaded gatsby-source-storm@${package.version}`);
exports.sourceNodes = async ({ actions, createContentDigest, createNodeId, cache }, pluginOptions) => {
const { createNode, unstable_createNodeManifest } = actions;
const pluginName = "gatsby-source-storm";
const cacheKey = "gatsby-source-storm-data";
const cacheTimestamp = "timestamp";
const defaultOptions = {
host: "not-yet-set",
debug: false,
};
pluginOptions.host = pluginOptions.host || defaultOptions.host;
pluginOptions.debug = pluginOptions.debug || defaultOptions.debug;
// see if there is any previously cached data
const cachedData = !pluginOptions.debug ? await cache.get(cacheKey) : null;
// only get updated info if we have previous data, otherwise, get everything
const lastFetched = cachedData ? await cache.get(cacheTimestamp) : null;
const datePart = lastFetched ? `/${moment(lastFetched).format()}` : "";
const url = `https://${pluginOptions.host}/api/system/export${datePart}`;
const utcNow = new Date().getTime();
const slugList = []; // check for dups
if (!pluginOptions.appkey) {
console.log("gatsby-source-storm error: appkey wasn't given");
return;
}
// get the data
var data = {};
await axios
.getData(url, pluginOptions.appkey)
.then((d) => {
data = mergeData(cachedData, d);
})
.catch((err) => {
console.log(err);
return;
});
if (pluginOptions.debug) {
console.log(data.contentTypes);
console.log(`Preview: ${process.env.GATSBY_IS_PREVIEW?.toString()}`);
}
// Create all the gatsby nodes
try {
// go through each items in the json array and create a new "node" in StormContent/allStormContent
data.contentTypes.forEach((y) => {
// go through terms to create StormContentTypes/allStormContentTypes
const childId = createNodeId(`${pluginName}${y.id}type`);
const contentTypeNode = {
...y, // pass all data into this object
contentId: y.id,
slug: y.slug,
sourceInstanceName: pluginName,
id: childId,
children: [],
parent: pluginName,
internal: {
type: "StormContentTypes", // the name of the node used in graphQL
contentDigest: createContentDigest(y),
description: "A Storm Content Type",
},
};
// if we have a dup, then we have problems
if (isSlugDup(slugList, y)) return;
const n = createNode(contentTypeNode);
if (pluginOptions.debug) console.log(`ContentType: ${y.slug}`);
});
// loop through all the content and fill out the gatsby nodes
data.contentList.forEach((c) => {
if (!!!c.slug) return;
// check for media and references and forms
c.meta = getMetaChildren(data, c.meta);
// calculate some unique values
const childId = createNodeId(`storm${c.id}`);
// Regular Entry
const contentNode = {
...c, // pass all data into this object
contentId: c.id,
slug: c.slug,
sourceInstanceName: pluginName,
id: childId,
children: [],
parent: pluginName,
internal: {
type: `StormContent`, // the name of the node used in graphQL
contentDigest: createContentDigest(c),
description: `A piece of Storm Content`,
},
};
// map all meta into first rate properties
for (let m of c.meta) {
contentNode[sluggify(m.name)] = m.value;
// if date or time, create a numeric field equivalent
if ((m.fieldType === 7 || m.fieldType === 8 || m.fieldType === 9) && m.value) {
contentNode[`${sluggify(m.name)}_isFuture`] = Date.parse(m.value) > utcNow;
contentNode[`${sluggify(m.name)}_milliseconds`] = m.value ? parseInt(Date.parse(m.value), 10) : 0;
}
}
const gatsbyNode = createNode(contentNode);
// for these, create a manifest so we can handle incremental builds
createNodeManifest({
entryItem: c,
entryNode: contentNode,
unstable_createNodeManifest,
debug: pluginOptions.debug,
});
});
// systemlists
data.systemLists?.forEach((c) => {
const childId = createNodeId(`${pluginName}${c.id}syslist`);
const listNode = {
...c, // pass all data into this object
menuId: c.id,
slug: c.slug,
sourceInstanceName: pluginName,
id: childId,
children: [],
parent: pluginName,
internal: {
type: "StormLists", // the name of the node used in graphQL
contentDigest: createContentDigest(c),
description: "A Storm List",
},
};
const n = createNode(listNode);
});
// go through menus to create stormMenus/allStormMenus
data.menus.forEach((c) => {
const childId = createNodeId(`${pluginName}${c.id}menu`);
const menuNode = {
...c, // pass all data into this object
menuId: c.id,
slug: c.slug,
sourceInstanceName: pluginName,
id: childId,
children: [],
parent: pluginName,
internal: {
type: "StormMenus", // the name of the node used in graphQL
contentDigest: createContentDigest(c),
description: "A Storm Menu",
},
};
const n = createNode(menuNode);
});
// forms
data.forms.forEach((c) => {
const childId = createNodeId(`${pluginName}${c.id}form`);
const formNode = {
...c, // pass all data into this object
formId: c.id,
slug: c.slug,
sourceInstanceName: pluginName,
id: childId,
children: [],
parent: pluginName,
internal: {
type: "StormForms", // the name of the node used in graphQL
contentDigest: createContentDigest(c),
description: "A Storm Form",
},
};
const n = createNode(formNode);
});
// go through settings to create stormSettings/allStormSettings
data.settings.forEach((c) => {
const childId = createNodeId(`${pluginName}${c.id}setting`);
const menuNode = {
...c, // pass all data into this object
settingId: c.id,
slug: c.name,
sourceInstanceName: pluginName,
id: childId,
children: [],
parent: pluginName,
internal: {
type: "StormSettings", // the name of the node used in graphQL
contentDigest: createContentDigest(c),
description: "Storm Settings",
},
};
const n = createNode(menuNode);
});
} catch (error) {
console.error(error);
}
// cache things for later
await cache.set(cacheKey, data);
await cache.set(cacheTimestamp, Date.now());
return;
};
//#region Helpers
function isSlugDup(slugList, o) {
let s = o.slug ?? o.name;
if (slugList.indexOf(s) > -1) {
console.error(`${s} IS ALREADY IN USE AND WON'T BE RENDERED AGAIN! ${o.name}`);
return true;
} else slugList.push(s);
return false;
}
function sluggify(str) {
return str.replace(/-/gi, "_");
}
function getProperty(properties, slug, propname = "slug") {
if (slug === null) return null;
let prop = properties.find((o) => o[propname]?.toLowerCase() === slug.toLowerCase());
return prop === undefined ? null : prop;
}
function getPropertyValue(properties, slug, def = null) {
let prop = properties.find((o) => o.name === slug.toLowerCase());
return prop === undefined ? def : prop.value;
}
// recursive function for mining meta references and media
function getMetaChildren(content, meta) {
meta = meta.map((o, i) => {
if (o.fieldTypeName === "media") {
let media = []; // always possible to have more than one so we need an array
let values = o.value?.split(",") || [];
for (let a = 0; a < values.length; a++) {
let m = content.media.find((m) => m.id.toString() === values[a]);
if (m) {
media.push(m);
}
}
return media.length > 0 ? { ...o, media } : o;
}
if (o.fieldTypeName === "reference") {
let reference = []; // always possible to have more than one so we need an array
let values = o.value?.split(",") || [];
for (let a = 0; a < values.length; a++) {
let r = content.contentList.find((m) => m.id.toString() === values[a]);
if (r) {
r.meta = getMetaChildren(content, r.meta);
reference.push(r);
}
}
return reference.length > 0 ? { ...o, reference } : o;
}
if (o.fieldTypeName === "form") {
let forms = []; // always possible to have more than one so we need an array
let values = o.value?.split(",") || [];
for (let a = 0; a < values.length; a++) {
let r = content.forms.find((m) => m.id.toString() === values[a]);
if (r) {
forms.push(r);
}
}
return forms.length > 0 ? { ...o, forms } : o;
}
return o;
});
return meta;
}
//#endregion