-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
114 lines (85 loc) · 3.62 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
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
const axios = require(`axios`);
const crypto = require(`crypto`);
const _ = require(`lodash`);
const makeTypeName = type => `drupal__${type.replace(/-/g, `_`)}`;
const processEntities = ents => ents.map(ent => {
const newEnt = _extends({
id: ent.id,
internal: {
type: ent.type
}
}, ent.attributes, {
created: new Date(ent.attributes.createdAt * 1000).toJSON(),
changed: new Date(ent.attributes.updatedAt * 1000).toJSON()
});
if (newEnt.revision_timestamp) {
newEnt.revision_timestamp = new Date(newEnt.revision_timestamp * 1000).toJSON();
}
return newEnt;
});
exports.sourceNodes = async ({ boundActionCreators, getNode, hasNodeChanged, store }, { baseUrl }) => {
const { createNode, setPluginStatus, touchNode } = boundActionCreators;
// Touch existing Drupal nodes so Gatsby doesn't garbage collect them.
_.values(store.getState().nodes).filter(n => n.internal.type.slice(0, 8) === `drupal__`).forEach(n => touchNode(n.id));
// Fetch articles.
console.time(`fetch Drupal data`);
console.log(`Starting to fetch data from Drupal`);
let lastFetched;
if (store.getState().status.plugins && store.getState().status.plugins[`gatsby-source-contenta`]) {
lastFetched = store.getState().status.plugins[`gatsby-source-contenta`].status.lastFetched;
}
let url = `${baseUrl}/api/recipes`;
let result;
try {
result = await axios.get(url);
} catch (e) {
console.log(`error fetching articles`, e);
}
console.log(`articles fetched`, result.data.data.length);
setPluginStatus({
status: {
lastFetched: new Date().toJSON()
}
});
console.timeEnd(`fetch Drupal data`);
const nodes = processEntities(result.data.data);
nodes.forEach((node, i) => {
const nodeStr = JSON.stringify(node);
const gatsbyNode = _extends({}, node, {
children: [],
parent: `__SOURCE__`,
internal: _extends({}, node.relationships, {
type: makeTypeName(node.internal.type)
}),
image___NODE: result.data.data[i].relationships.image.data.id,
// Get content digest of node.
});const contentDigest = crypto.createHash(`md5`).update(JSON.stringify(gatsbyNode)).digest(`hex`);
gatsbyNode.internal.contentDigest = contentDigest;
createNode(gatsbyNode);
});
// Fetch images
const imageUrl = `${baseUrl}/api/images`;
const imageResult = await axios.get(imageUrl);
const images = imageResult.data.data;
const blue = await Promise.all(images.map((image, i) => new Promise(resolve => {
const imgStr = JSON.stringify(image);
const gatsbyImage = _extends({}, image, {
children: [],
parent: `__SOURCE__`,
internal: {
type: makeTypeName(image.type)
}
});
// axios.get(imageResult.data.data[i].relationships.imageFile.links.related, { timeout: 200000 }).catch(() => console.log(`fail fetch`, gatsbyImage)).then(pictureResult => {
axios.get(imageResult.data.data[i].relationships.imageFile.links.related.href, { timeout: 1000 * 50 }).catch(() => console.log(`fail fetch`)).then(pictureResult => {
gatsbyImage.url = `${pictureResult.data.data.attributes.uri.url}`;
// Get content digest of node.
const contentDigest = crypto.createHash(`md5`).update(JSON.stringify(gatsbyImage)).digest(`hex`);
gatsbyImage.internal.contentDigest = contentDigest;
createNode(gatsbyImage);
resolve();
});
})));
return;
};