forked from mpartipilo/gatsby-source-cockpit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
118 lines (105 loc) · 3.61 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
const _ = require(`lodash`)
const crypto = require(`crypto`)
const cp = require(`cockpit-api-client`)
const digest = (data) => (crypto
.createHash(`md5`)
.update(JSON.stringify(data))
.digest(`hex`))
function createTextNode (node, key, text) {
const str = _.isString(text) ? text : ` `
const textNode = {
id: `${node.id}${key}TextNode`,
parent: node.id,
children: [],
[key]: str,
internal: {
type: _.camelCase(`${node.internal.type} ${key} TextNode`),
mediaType: `text/markdown`,
content: str,
contentDigest: digest(str)
}
}
return textNode
}
exports.sourceNodes = async ({ boundActionCreators, getNode, hasNodeChanged, reporter }, pluginOptions) => {
const { createNode } = boundActionCreators
const { accessToken, host, collectionName } = pluginOptions
const client = new cp.Cockpit({ host, accessToken })
reporter.info(`Cockpit host: ${host}`)
reporter.info(`Cockpit access token: ${accessToken}`)
const spinner = reporter.activity()
spinner.tick(`Fetching list of assets`)
const assetResponse = await client.assets()
spinner.tick(`Assets retrieved`)
for (let idx = 0; idx < collectionName.length; idx += 1) {
const c = collectionName[idx]
spinner.tick(`Fetching Cockpit Collection ${c}`)
// const data = getFakeData(pluginOptions);
const data = await client.collectionEntries(c)
spinner.tick(`Collection retrieved: ${c}`)
spinner.tick(`Processing collection: ${c}`)
// Process data into nodes.
await data.entries.forEach(async i => {
const entry =
Object.keys(data.fields)
.map(f => data.fields[f].name)
.reduce((x, y) => ({ ...x, [y]: i[y] }), {})
const properties =
Object.keys(i)
.filter(f => !data.fields[f])
.reduce((x, y) => ({ ...x, [y]: i[y] }), {})
const node = {
entry: { ...entry },
properties: { ...properties },
host,
// eslint-disable-next-line
id: i._id,
parent: null, // or null if it's a source node without a parent
children: [],
internal: {
type: `Cockpit${c}`,
contentDigest: digest(i)
}
}
await Object.keys(data.fields)
.forEach(async f => {
if (data.fields[f].type === `gallery`) {
node.entry[f].forEach(async image => {
if (image.meta.asset) {
const assetDetails = assetResponse.assets.find(a => a._id === image.meta.asset)
const {_id, width, height} = assetDetails
const sizes = {
s2: {
width: Math.floor(width / 3),
height: Math.floor(height / 3)
},
s3: {
width: Math.floor(width / 2),
height: Math.floor(height / 2)
}
}
image.meta.asset = assetDetails
image.thumb2 = {
src: await client.image(_id, sizes.s2),
...sizes.s2
}
image.thumb3 = {
src: await client.image(_id, sizes.s3),
...sizes.s3
}
}
})
}
if (data.fields[f].type === `markdown`) {
const textNode = createTextNode(node, data.fields[f].name, node.entry[f], createNode)
createNode(textNode)
node.children.push(textNode.id)
}
})
createNode(node)
})
spinner.tick(`Finished processing Collection: ${c}`)
}
reporter.success(`Finished processing all collections`)
spinner.end()
}