-
Notifications
You must be signed in to change notification settings - Fork 1
/
gridsome.server.js
72 lines (59 loc) · 2.31 KB
/
gridsome.server.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
// Server API makes it possible to hook into various parts of Gridsome
// on server-side and add custom data to the GraphQL data layer.
// Learn more: https://gridsome.org/docs/server-api/
// Changes here require a server restart.
// To restart press CTRL + C in terminal and run `gridsome develop`
const yaml = require('js-yaml'),
fs = require('fs'),
util = require('util'),
path = require('path'),
glob = util.promisify(require('glob'));
module.exports = function(api) {
// See: https://gridsome.org/docs/data-store-api/
api.loadSource(async ({ addCollection, store }) => {
const drinks = addCollection('Drink'),
ingredients = addCollection('Ingredient'),
tags = addCollection('Tag'),
drinkFiles = await glob(path.join(__dirname, 'data', 'drinks', '**', '*.yml')),
foundTags = new Set();
yaml.load(await fs.promises.readFile(path.join(__dirname, 'data', 'ingredients.yml'), 'utf8'))
.ingredients
.forEach((ingredient) => {
ingredients.addNode(ingredient);
});
drinks.addReference('tags', 'Tag');
await Promise.all(drinkFiles.map(async (file) => {
const raw = await fs.promises.readFile(file, 'utf8'),
data = yaml.load(raw);
data.ingredients.forEach((ingredient) => {
// Ensure the value is always a string to keep a consistent schema
ingredient.amount.value = ingredient.amount.value.toString();
ingredient.item = store.createReference('Ingredient', ingredient.ingredient);
delete ingredient.ingredient;
});
if (data.garnish) {
data.garnish.forEach((garnish) => {
garnish.item = store.createReference('Ingredient', garnish.ingredient);
delete garnish.ingredient;
});
}
if (data.tags) {
data.tags.forEach((tag) => {
foundTags.add(tag);
});
}
drinks.addNode({
id: path.basename(file, '.yml'),
...data,
});
}));
foundTags.forEach((tag) => {
tags.addNode({
id: tag,
});
});
});
api.createPages(({ createPage }) => {
// Use the Pages API here: https://gridsome.org/docs/pages-api/
});
};