-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgatsby-node.js
46 lines (45 loc) · 1.24 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
const crypto = require('crypto');
const fetchSheet = require('./lib/fetchSheet.js').default;
exports.sourceNodes = async (
{ actions },
{ spreadsheetId, credentials, apiKey },
) => {
const { createNode } = actions;
console.log('Fetching Google Sheet', fetchSheet, spreadsheetId);
const sheets = await fetchSheet(spreadsheetId, credentials, apiKey);
Object.entries(sheets).forEach(([name, data]) => {
if (Array.isArray(data)) {
data.forEach(row => {
name = name.replace(/[\W_]+/g, '');
return createNode(
Object.assign(row, {
parent: '__SOURCE__',
children: [],
internal: {
type: `google${name.charAt(0).toUpperCase()}${name.slice(
1,
)}Sheet`,
contentDigest: crypto
.createHash('md5')
.update(JSON.stringify(row))
.digest('hex'),
},
}),
);
});
}
});
createNode(
Object.assign(sheets, {
parent: '__SOURCE__',
children: [],
internal: {
type: 'googleSheet',
contentDigest: crypto
.createHash('md5')
.update(JSON.stringify(sheets))
.digest('hex'),
},
}),
);
};