-
Notifications
You must be signed in to change notification settings - Fork 7
/
gatsby-node.js
103 lines (92 loc) · 2.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
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
const path = require('path');
const fs = require("fs-extra");
const yaml = require("js-yaml");
const flatten = require('flat')
const {createFilePath} = require(`gatsby-source-filesystem`);
exports.onPreBootstrap = () => {
console.log("Copying locales");
const nlTranslation = loadTranslationObject("nl");
const enTranslation = loadTranslationObject("en");
// Create directory structure
fs.existsSync(path.join(__dirname, "/public/intl")) || fs.mkdirSync(path.join(__dirname, "/public/intl"));
// Save bundled translation files
fs.writeFileSync(path.join(__dirname, "/public/intl/nl.json"), JSON.stringify(flatten(nlTranslation)));
fs.writeFileSync(path.join(__dirname, "/public/intl/en.json"), JSON.stringify(flatten(enTranslation)));
// Copy redirects
fs.copySync(
path.join(__dirname, "/_redirects"),
path.join(__dirname, "/public/_redirects")
);
};
exports.createPages = ({graphql, actions}) => {
const {createPage} = actions
return new Promise((resolve, reject) => {
graphql(`
{
allConcertsYaml {
edges {
node {
id
type
date
time
doorsOpen
location
locationLink
tickets
freeEntrance
facebookEvent
}
}
}
}
`).then(result => {
result.data.allConcertsYaml.edges.forEach(({node}) => {
createPage({
path: `/concerts/${node.id}/`,
component: path.resolve(`./src/templates/concertPageTemplate.js`),
context: {
concert: node
},
})
})
}).then((newsResolve, newsReject) => {
graphql(`
{
allNewsYaml {
edges {
node {
id
date
author
}
}
}
}
`).then(result => {
result.data.allNewsYaml.edges.forEach(({node}) => {
createPage({
path: `/news/${node.id}/`,
component: path.resolve(`./src/templates/newsPageTemplate.js`),
context: {
news: node
},
})
})
resolve()
})
})
})
};
function loadTranslationObject(languageCode) {
const srcPath = path.join(__dirname, `/src/locales/${languageCode}/`);
const translationObjects = fs.readdirSync(srcPath).map(file => (
yaml.load(fs.readFileSync(path.join(srcPath, file)), {encoding: "utf-8"})
));
return Object.assign({}, ...translationObjects)
}