This repository has been archived on 2026-08-05. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
nodejs-moonth-generator/moonth-generator.js
T

112 lines
2.4 KiB
JavaScript
Executable File

#!/usr/bin/env nodejs
'use strict';
// 20260730 - Hyperling
// Generate .ics files with entries to be imported in a shared calendar.
/*** Parameters ***/
const args = process.argv;
const cmdExec = args[0];
const cmdPath = args[1];
const startDate = args[2];
const endDate = args[3];
const DEBUG = args[4];
console.log(`Running ${cmdPath} with ${cmdExec}...`);
console.log("Start Date: " + startDate);
console.log(" End Date: " + endDate);
console.log("Converting strings to dates...");
const dayStart = new Date(startDate);
const dayEnd = new Date(endDate);
console.log("...done!");
/*** Setup ***/
// Local Vars
const name = "Hyperling's Moonth Calendar";
const domain = "cloud.hyperling.com";
// iCalendar Library
// https://www.npmjs.com/package/ical-generator
import ical, { ICalCalendarMethod } from 'ical-generator';
const cal =
ical({
domain: "$domain",
name: "$name"
})
;
// File System Reading
import fs from "fs";
function readFile (file) {
try {
const text = fs.readFileSync(file, "utf8");
console.log(file + " loaded!");
return text;
} catch (e) {
console.error("ERROR: Could not read " + file + ": ", e);
}
}
function writeFile (name, text) {
try {
fs.writeFileSync(name, text);
console.log(name + " written successfully");
} catch (e) {
console.error("Error writing " + name + ": ", e);
}
}
const templateTitle = readFile("templates/TITLE.txt")
const templateDesc = readFile("templates/DESCRIPTION.txt");
/*** Main ***/
const dayOne = new Date("2026-04-01");
const dayLength = 1000 * 60 * 60 * 24;
function getNumDaysBetween (date1, date2) {
return Math.abs((date1-date2)/dayLength);
}
for (
let dayCurr = dayStart;
dayCurr <= dayEnd;
dayCurr.setDate(dayCurr.getDate() + 1)
) {
if (DEBUG) console.log("DEBUG: curr-Apr1 = ", getNumDaysBetween(dayCurr, dayOne));
// Pull data for UID.
const year = dayCurr.getFullYear();
const moonth = dayCurr.getMonth();
const day = dayCurr.getDate();
const extra = "00";
// Reset vars to defaults.
const title = templateTitle
.replaceAll("{{MOONTH}}", moonth)
.replaceAll("{{DAY}}", day)
.replaceAll("{{YEAR}}", year)
;
const description = templateDesc;
cal.createEvent({
uid: `${year}${moonth}${day}${extra}@${domain}`,
start: new Date(dayCurr),
allDay: true,
summary: title,
description: description,
});
}
/*** Finish ***/
// Create Events
//cal.save('schedule.ics');
writeFile("calendar.ics", cal.toString());