-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.ts
97 lines (93 loc) · 3.23 KB
/
gatsby-node.ts
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
import TidalData from "./data/tides.json";
import path from "path";
import { TidesJson_PDFObject, TidesJson_ScheduleObject } from "./src/types";
import { BuildArgs, CreatePagesArgs } from "gatsby";
import fs from "fs";
import ical, { ICalEventBusyStatus, ICalEventClass } from "ical-generator";
import { DateTime } from "luxon";
export const createPages = async function ({
actions,
graphql,
}: CreatePagesArgs) {
TidalData.pdfs.forEach((pdf: TidesJson_PDFObject) => {
actions.createPage({
path: "/tide-tables/" + pdf.url,
component: path.resolve(`./src/components/templates/TideTablePage.tsx`),
context: { pdf },
defer: false,
});
actions.createRedirect({
fromPath: "/tide-tables/" + pdf.url.replace("/", "-"),
toPath: "/tide-tables/" + pdf.url,
});
actions.createRedirect({
fromPath: "/tide-tables/" + pdf.url.replace("/", "-") + ".pdf",
toPath: "/tide-tables/" + pdf.url + ".pdf",
});
});
TidalData.schedule.forEach((day: TidesJson_ScheduleObject, index: number) => {
actions.createPage({
path: "/tide-graph/" + day.date,
component: path.resolve(`./src/components/templates/TideGraphPage.tsx`),
context: {
day,
nextDay:
index < TidalData.schedule.length - 1
? TidalData.schedule[index + 1].date
: false,
previousDay: index > 0 ? TidalData.schedule[index - 1].date : false,
},
defer: false,
});
});
// Legacy page
actions.createRedirect({
fromPath: "/historical-tables",
toPath: "/tide-tables",
});
};
export const onPostBootstrap = function ({ reporter }: BuildArgs) {
reporter.info(`Generating iCal file for tide times`);
const cal = ical();
cal.timezone("Europe/London");
cal.name("Porthmadog Tide Times");
cal.description(
"Tide times for Porthmadog, Borth-y-gest, Morfa Bychan and Black Rock Sands from Port-Tides.com"
);
const today = new Date();
today.setHours(0, 0, 0, 0);
const nextYear = new Date(today);
nextYear.setDate(today.getDate() + 365);
TidalData.schedule
.filter((tideDay: TidesJson_ScheduleObject) => {
let date = new Date(tideDay.date);
return date >= today && date <= nextYear;
})
.forEach((day: TidesJson_ScheduleObject) =>
day.groups.forEach((tide) => {
cal.createEvent({
start: DateTime.fromSQL(day.date + " " + tide.time).toJSDate(),
end: DateTime.fromSQL(day.date + " " + tide.time)
.plus({ minutes: 30 })
.toJSDate(),
summary: `High Tide Porthmadog - ${tide.height}m`,
description: {
plain: "Powered by port-tides.com",
html: `More details at <a href="https://port-tides.com/">port-tides.com</a>`,
},
// Commented out to reduce file size
/*location: {
title: "Porthmadog",
address: "Harbwr Porthmadog, LL49 9AY, UK",
},
busystatus: ICalEventBusyStatus.FREE,
class: ICalEventClass.PUBLIC,
url: "https://port-tides.com/tide-tables",*/
});
})
);
fs.writeFileSync("public/porthmadog-tides.ical", cal.toString());
reporter.success(
`Generated iCal file for tide times at public/porthmadog-tides.ical`
);
};