-
Notifications
You must be signed in to change notification settings - Fork 10
/
gridsome.server.js
113 lines (95 loc) · 3.17 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
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
// 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';
const HistoricBenchmarkingDataFile = 'benchmarking-all-years.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',
'uic',
'iit',
'northwestern',
'loyola',
'cps',
'cha',
'cityofchicago',
'columbia',
'ccc',
'moody',
'saic',
'npu',
];
module.exports = function(api) {
// Use the Data Store API here: https://gridsome.org/docs/data-store-api/
api.loadSource(async (actions) => {
loadBuildingBenchmarkData(actions);
loadHistoricBenchmarkDat(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 latestBenchmarksRaw = readFileSync(`${DataDirectory}${BuildingEmissionsDataFile}`, 'utf8');
/**
* Load in building benchmarks and expose as Buildings collection
*/
const LatestBenchmarksData = parse(latestBenchmarksRaw, {
columns: true,
skip_empty_lines: true,
});
const collection = actions.addCollection({typeName: 'Building'});
for (const building of LatestBenchmarksData) {
// Make a slugSource that is the property name or the address as a fallback (skip one letter
// names, e.g. '-)
building.slugSource = building.PropertyName.length > 1 ? building.PropertyName : building.Address;
if (!building.slugSource || typeof building.slugSource !== 'string') {
throw new Error('No building slug source (name or address)!', building);
}
collection.addNode(building);
}
}
/**
* Load in the historic benchmark data
*
* @param {unknown} actions The actions class?
*/
function loadHistoricBenchmarkDat(actions) {
const historicBenchmarksRaw = readFileSync(`${DataDirectory}${HistoricBenchmarkingDataFile}`, 'utf8');
/**
* Load in building benchmarks and expose as Buildings collection
*/
const HistoricBenchmarksData = parse(historicBenchmarksRaw, {
columns: true,
skip_empty_lines: true,
});
const collection = actions.addCollection({ typeName: 'Benchmark' });
for (const benchmark of HistoricBenchmarksData) {
collection.addNode(benchmark);
}
}