forked from vkoves/electrify-chicago
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgridsome.server.js
72 lines (60 loc) · 2.09 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`
/**
* From fetching CSV data:
* https://gridsome.org/docs/fetching-data/#csv
*/
const {readFileSync} = require('fs');
const parse = require('csv-parse/sync').parse;
const DataDirectory = './src/data/dist/';
const BuildingEmissionsDataFile = 'building-benchmarks.csv';
// This is an array equivalent of Object.keys(BuildingOwners) but this file can't use Typescript and
// import that file
const BuildingOwnerIds = [
'depaul',
'uchicago',
'iit',
'northwestern'
]
module.exports = function(api) {
// Use the Data Store API here: https://gridsome.org/docs/data-store-api/
api.loadSource(async (actions) => {
loadBuildingBenchmarkData(actions);
});
// Use the Pages API here: https://gridsome.org/docs/pages-api/
api.createPages(({ createPage }) => {
// Create pages for building owners. This could be a dynamic route, but making it this way
// should let them get statically built as expected
BuildingOwnerIds.forEach(ownerId => {
createPage({
path: `/owner/${ownerId}`,
component: './src/templates/BuildingOwner.vue',
context: { ownerId },
})
})
});
};
/**
* Load in the building benchmark data
*
* @param {unknown} actions The actions class?
*/
function loadBuildingBenchmarkData(actions) {
const input = readFileSync(`${DataDirectory}${BuildingEmissionsDataFile}`, 'utf8');
/**
* Load in building benchmarks and expose as Buildings collection
*/
const BuildingsData = parse(input, {
columns: true,
skip_empty_lines: true,
});
const collection = actions.addCollection({typeName: 'Building'});
for (const building of BuildingsData) {
// Make a slugSource that is the property name or the address as a fallback
building.slugSource = building.PropertyName || building.Address;
collection.addNode(building);
}
}