Skip to content

Commit

Permalink
Add iCal file
Browse files Browse the repository at this point in the history
  • Loading branch information
Jbithell committed Jun 15, 2024
1 parent f1e273b commit 73dc64b
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 3 deletions.
43 changes: 41 additions & 2 deletions gatsby-node.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import TidalData from "./data/tides.json";
import path from "path";
import { TidesJson_PDFObject } from "./src/types";
import { CreatePagesArgs } from "gatsby";
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,
Expand Down Expand Up @@ -30,3 +33,39 @@ export const createPages = async function ({
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"
);
TidalData.schedule.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 - ${tide.height}m`,
busystatus: ICalEventBusyStatus.FREE,
class: ICalEventClass.PUBLIC,
description: {
plain: `High Tide ${tide.height}m at ${tide.time}`,
html: `High Tide ${tide.height}m at ${tide.time}. More details at <a href="https://port-tides.com/">port-tides.com</a>`,
},
location: {
title: "Porthmadog",
address: "Harbwr Porthmadog, LL49 9AY, UK",
},
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`
);
};
58 changes: 57 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"gatsby-plugin-sitemap": "^6.13.1",
"gatsby-source-build-date": "^1.0.1",
"gatsby-source-filesystem": "^5.13.1",
"ical-generator": "^7.1.0",
"luxon": "^3.4.4",
"react": "^18.3.1",
"react-dom": "^18.3.1",
Expand Down

0 comments on commit 73dc64b

Please sign in to comment.