|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const yaml = require('js-yaml'); |
| 4 | +const { DateTime } = require('luxon'); |
| 5 | +const { RRule } = require('rrule'); |
| 6 | + |
| 7 | +function parseDateTime(dateTimeString) { |
| 8 | + // Parse "YYYY-MM-DD HH:MM" into Berlin timezone [YYYY, MM, DD, HH, MM] |
| 9 | + const dateTime = DateTime.fromFormat(dateTimeString, 'yyyy-MM-dd HH:mm', { zone: 'Europe/Berlin' }); |
| 10 | + if (dateTime.isValid) { |
| 11 | + return [ |
| 12 | + dateTime.year, |
| 13 | + dateTime.month, |
| 14 | + dateTime.day, |
| 15 | + dateTime.hour, |
| 16 | + dateTime.minute, |
| 17 | + ]; |
| 18 | + } else { |
| 19 | + console.warn(`Invalid date-time format: ${dateTimeString}`); |
| 20 | + return null; // Return null for invalid date-time |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +async function loadEvents() { |
| 25 | + try { |
| 26 | + const yamlText = fs.readFileSync('./assets/calendar/events.yaml', 'utf8'); // Read the YAML file |
| 27 | + const events = yaml.load(yamlText); |
| 28 | + |
| 29 | + if (!Array.isArray(events)) { |
| 30 | + throw new Error("Parsed YAML is not an array"); |
| 31 | + } |
| 32 | + |
| 33 | + const expandedEvents = []; |
| 34 | + for (const event of events) { |
| 35 | + const startDate = new Date(event.start); |
| 36 | + const endDate = new Date(event.end); |
| 37 | + |
| 38 | + if (event.recurrence) { |
| 39 | + const rrule = new RRule({ |
| 40 | + ...RRule.parseString(event.recurrence), |
| 41 | + dtstart: startDate, |
| 42 | + }); |
| 43 | + |
| 44 | + rrule.all().forEach(date => { |
| 45 | + const end = new Date(date.getTime() + (endDate - startDate)); |
| 46 | + expandedEvents.push({ |
| 47 | + start: date.toISOString(), |
| 48 | + end: end.toISOString(), |
| 49 | + title: event.title, |
| 50 | + color: event.color, |
| 51 | + location: event.location, |
| 52 | + description: event.description, |
| 53 | + }); |
| 54 | + }); |
| 55 | + } else { |
| 56 | + // Non-recurring event |
| 57 | + expandedEvents.push({ |
| 58 | + start: event.start, |
| 59 | + end: event.end, |
| 60 | + title: event.title, |
| 61 | + color: event.color, |
| 62 | + location: event.location, |
| 63 | + description: event.description, |
| 64 | + }); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + console.log("Expanded events:", expandedEvents); |
| 69 | + return expandedEvents; |
| 70 | + } catch (error) { |
| 71 | + console.error("Error fetching or parsing YAML:", error); |
| 72 | + return []; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | + |
| 77 | +function generateICal(events) { |
| 78 | + const vtimezone = ` |
| 79 | +BEGIN:VTIMEZONE |
| 80 | +TZID:Europe/Berlin |
| 81 | +BEGIN:STANDARD |
| 82 | +DTSTART:20231029T030000 |
| 83 | +RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU |
| 84 | +TZOFFSETFROM:+0200 |
| 85 | +TZOFFSETTO:+0100 |
| 86 | +TZNAME:CET |
| 87 | +END:STANDARD |
| 88 | +BEGIN:DAYLIGHT |
| 89 | +DTSTART:20240331T020000 |
| 90 | +RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU |
| 91 | +TZOFFSETFROM:+0100 |
| 92 | +TZOFFSETTO:+0200 |
| 93 | +TZNAME:CEST |
| 94 | +END:DAYLIGHT |
| 95 | +END:VTIMEZONE`; |
| 96 | + |
| 97 | + function parseDate(input) { |
| 98 | + try { |
| 99 | + if (typeof input === 'string' && input.includes('T')) { |
| 100 | + return DateTime.fromISO(input); |
| 101 | + } else if (typeof input === 'string') { |
| 102 | + return DateTime.fromFormat(input, "yyyy-MM-dd HH:mm"); |
| 103 | + } else { |
| 104 | + throw new Error("Invalid date format"); |
| 105 | + } |
| 106 | + } catch (error) { |
| 107 | + console.error("Error parsing date:", input, error); |
| 108 | + return null; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + const vevents = events |
| 113 | + .map(event => { |
| 114 | + const dtstart = parseDate(event.start); |
| 115 | + const dtend = parseDate(event.end); |
| 116 | + |
| 117 | + if (!dtstart || !dtend) { |
| 118 | + console.error("Skipping event due to invalid dates:", event); |
| 119 | + return null; |
| 120 | + } |
| 121 | + |
| 122 | + return ` |
| 123 | +BEGIN:VEVENT |
| 124 | +UID:${Math.random().toString(36).substring(2, 15)} |
| 125 | +SUMMARY:${event.title} |
| 126 | +DTSTAMP:${DateTime.now().toUTC().toFormat("yyyyMMdd'T'HHmmss'Z'")} |
| 127 | +DTSTART;TZID=Europe/Berlin:${dtstart.toFormat("yyyyMMdd'T'HHmmss")} |
| 128 | +DTEND;TZID=Europe/Berlin:${dtend.toFormat("yyyyMMdd'T'HHmmss")} |
| 129 | +DESCRIPTION:${event.description || ''} |
| 130 | +LOCATION:${event.location || ''} |
| 131 | +END:VEVENT`; |
| 132 | + }) |
| 133 | + .filter(Boolean) // Remove null values |
| 134 | + .join("\n"); |
| 135 | + |
| 136 | + return `BEGIN:VCALENDAR |
| 137 | +VERSION:2.0 |
| 138 | +CALSCALE:GREGORIAN |
| 139 | +PRODID:-//Digital Work Lab//Calendar Export Tool//EN |
| 140 | +METHOD:PUBLISH |
| 141 | +X-PUBLISHED-TTL:PT1H |
| 142 | +${vtimezone} |
| 143 | +${vevents} |
| 144 | +END:VCALENDAR`; |
| 145 | +} |
| 146 | + |
| 147 | +(async () => { |
| 148 | + try { |
| 149 | + const events = await loadEvents(); |
| 150 | + console.log('Loaded events:', events); // Log loaded events for debugging |
| 151 | + const icalContent = generateICal(events); |
| 152 | + |
| 153 | + const outputPath = path.join('assets/calendar/fs-ise.ical'); |
| 154 | + fs.writeFileSync(outputPath, icalContent, 'utf8'); |
| 155 | + console.log('iCal file generated and saved to:', outputPath); |
| 156 | + } catch (error) { |
| 157 | + console.error('Error during iCal generation:', error); |
| 158 | + process.exit(1); // Exit with error |
| 159 | + } |
| 160 | +})(); |
0 commit comments