/
gatsby-node.js
120 lines (103 loc) · 2.73 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
const fs = require(`fs`)
const v8 = require(`v8`)
const glob = require(`glob`)
const path = require(`path`)
const _ = require(`lodash`)
const { open } = require(`lmdb`)
const { saveState } = require(`gatsby/dist/redux/save-state`)
const {
ON_PRE_BOOTSTRAP_FILE_PATH,
ON_POST_BUILD_FILE_PATH,
} = require(`./utils/constants`)
const { getAllPlugins } = require(`./utils/collect-scenarios`)
const getDiskCacheSnapshot = () => {
const plugins = getAllPlugins()
const snapshot = {}
let store
try {
store = open({
name: `root`,
path: path.join(process.cwd(), `.cache/caches-lmdb`),
compression: true,
maxDbs: 200,
})
plugins.forEach(pluginName => {
const pluginDb = store.openDB({
name: pluginName,
encoding: `json`,
})
snapshot[pluginName] = Array.from(pluginDb.getKeys({ snapshot: false }))
})
} catch (e) {
console.error(e)
} finally {
if (store) {
store.close()
}
}
return snapshot
}
exports.onPreBootstrap = ({ getNodes }) => {
fs.writeFileSync(
ON_PRE_BOOTSTRAP_FILE_PATH,
v8.serialize({
nodes: getNodes(),
diskCacheSnapshot: getDiskCacheSnapshot(),
})
)
}
exports.onPostBuild = async ({ getNodes, store }) => {
const queryResults = {}
const scenarioPagesDirectory = path.join(
__dirname,
`src`,
`pages`,
`scenarios`
)
const scenarioPagesFiles = glob.sync(
path.join(scenarioPagesDirectory, `**`, `index.js`)
)
scenarioPagesFiles.forEach(scenarioPageFile => {
const relativePathToPageData = path.dirname(
path.relative(scenarioPagesDirectory, scenarioPageFile)
)
const { dir: scenarioName, name: type } = path.parse(relativePathToPageData)
const pageDataPath = path.join(
__dirname,
`public`,
`page-data`,
`scenarios`,
scenarioName,
type,
`page-data.json`
)
const result = require(pageDataPath).result.data
// some normalization so order of fields in type queries is consistent
if (result) {
if (result.typeinfoParent && result.typeinfoParent.fields) {
result.typeinfoParent.fields = result.typeinfoParent.fields.sort(
(a, b) => {
return a.name.localeCompare(b.name)
}
)
}
if (result.typeinfoChild && result.typeinfoChild.fields) {
result.typeinfoChild.fields = result.typeinfoChild.fields.sort(
(a, b) => {
return a.name.localeCompare(b.name)
}
)
}
}
_.set(queryResults, [scenarioName, type], result)
})
fs.writeFileSync(
ON_POST_BUILD_FILE_PATH,
v8.serialize({
nodes: getNodes(),
diskCacheSnapshot: getDiskCacheSnapshot(),
queryResults,
})
)
await saveState()
}