From f80e509895b7b2c1d716ac276977b7165a13c192 Mon Sep 17 00:00:00 2001 From: Raphael Kabo Date: Fri, 6 Oct 2023 12:34:36 +0100 Subject: Typescript migration --- src/app.js | 50 -------------------------------------------------- src/app.ts | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/helpers.js | 54 ------------------------------------------------------ src/helpers.ts | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/start.js | 27 --------------------------- src/start.ts | 25 +++++++++++++++++++++++++ 6 files changed, 119 insertions(+), 131 deletions(-) delete mode 100755 src/app.js create mode 100755 src/app.ts delete mode 100644 src/helpers.js create mode 100644 src/helpers.ts delete mode 100755 src/start.js create mode 100755 src/start.ts (limited to 'src') diff --git a/src/app.js b/src/app.js deleted file mode 100755 index 9feb239..0000000 --- a/src/app.js +++ /dev/null @@ -1,50 +0,0 @@ -import express from "express"; -import routes from "./routes.js"; -import hbs from "express-handlebars"; -import bodyParser from "body-parser"; - -const app = express(); - -// Configuration // - -//app.use(cors()); -//app.use(bodyParser.json()); -//app.use(session({ secret: 'slartibartfast', cookie: { maxAge: 60000 }, resave: false, saveUninitialized: false })); - -// View engine // -const hbsInstance = hbs.create({ - defaultLayout: "main", - partialsDir: ["views/partials/"], - layoutsDir: "views/layouts/", - helpers: { - plural: function (number, text) { - var singular = number === 1; - // If no text parameter was given, just return a conditional s. - if (typeof text !== "string") return singular ? "" : "s"; - // Split with regex into group1/group2 or group1(group3) - var match = text.match(/^([^()\/]+)(?:\/(.+))?(?:\((\w+)\))?/); - // If no match, just append a conditional s. - if (!match) return text + (singular ? "" : "s"); - // We have a good match, so fire away - return ( - (singular && match[1]) || // Singular case - match[2] || // Plural case: 'bagel/bagels' --> bagels - match[1] + (match[3] || "s") - ); // Plural case: 'bagel(s)' or 'bagel' --> bagels - }, - }, -}); -app.engine("handlebars", hbsInstance.engine); -app.set("view engine", "handlebars"); -app.set("hbsInstance", hbsInstance); - -// Static files // - -app.use(express.static("public")); - -// Router // -app.use(bodyParser.json({ type: "application/activity+json" })); // support json encoded bodies -app.use(bodyParser.urlencoded({ extended: true })); -app.use("/", routes); - -export default app; diff --git a/src/app.ts b/src/app.ts new file mode 100755 index 0000000..32e89b6 --- /dev/null +++ b/src/app.ts @@ -0,0 +1,44 @@ +import express from "express"; +import routes from "./routes.js"; +import hbs from "express-handlebars"; + +const app = express(); + +// View engine // +const hbsInstance = hbs.create({ + defaultLayout: "main", + partialsDir: ["views/partials/"], + layoutsDir: "views/layouts/", + helpers: { + plural: function (number: number, text: string) { + var singular = number === 1; + // If no text parameter was given, just return a conditional s. + if (typeof text !== "string") return singular ? "" : "s"; + // Split with regex into group1/group2 or group1(group3) + var match = text.match(/^([^()\/]+)(?:\/(.+))?(?:\((\w+)\))?/); + // If no match, just append a conditional s. + if (!match) return text + (singular ? "" : "s"); + // We have a good match, so fire away + return ( + (singular && match[1]) || // Singular case + match[2] || // Plural case: 'bagel/bagels' --> bagels + match[1] + (match[3] || "s") + ); // Plural case: 'bagel(s)' or 'bagel' --> bagels + }, + }, +}); +app.engine("handlebars", hbsInstance.engine); +app.set("view engine", "handlebars"); +app.set("hbsInstance", hbsInstance); + +// Static files // +app.use(express.static("public")); + +// Body parser // +app.use(express.json({ type: "application/activity+json" })); // support json encoded bodies +app.use(express.urlencoded({ extended: true })); + +// Router // +app.use("/", routes); + +export default app; diff --git a/src/helpers.js b/src/helpers.js deleted file mode 100644 index 305187f..0000000 --- a/src/helpers.js +++ /dev/null @@ -1,54 +0,0 @@ -import moment from "moment-timezone"; -import icalGenerator from "ical-generator"; -import Log from "./models/Log.js"; -import { getConfig } from "./lib/config.js"; -const config = getConfig(); -const domain = config.general.domain; -const siteName = config.general.site_name; - -// LOGGING - -export function addToLog(process, status, message) { - let logEntry = new Log({ - status: status, - process: process, - message: message, - timestamp: moment(), - }); - logEntry.save().catch(() => { - console.log("Error saving log entry!"); - }); -} - -export function exportIcal(events, calendarName) { - // Create a new icalGenerator... generator - const cal = icalGenerator({ - name: calendarName || siteName, - x: { - "X-WR-CALNAME": calendarName || siteName, - }, - }); - if (events instanceof Array === false) { - events = [events]; - } - events.forEach((event) => { - // Add the event to the generator - cal.createEvent({ - start: moment.tz(event.start, event.timezone), - end: moment.tz(event.end, event.timezone), - timezone: event.timezone, - timestamp: moment(), - summary: event.name, - description: event.description, - organizer: { - name: event.hostName || "Anonymous", - email: event.creatorEmail || "anonymous@anonymous.com", - }, - location: event.location, - url: "https://" + domain + "/" + event.id, - }); - }); - // Stringify it! - const string = cal.toString(); - return string; -} diff --git a/src/helpers.ts b/src/helpers.ts new file mode 100644 index 0000000..4528042 --- /dev/null +++ b/src/helpers.ts @@ -0,0 +1,50 @@ +import moment from "moment-timezone"; +import icalGenerator from "ical-generator"; +import Log, { ILog } from "./models/Log.js"; +import { getConfig } from "./lib/config.js"; +import { IEvent } from "./models/Event.js"; + +const config = getConfig(); +const domain = config.general.domain; +const siteName = config.general.site_name; + +// LOGGING +export function addToLog(process: string, status: string, message: string) { + const logEntry = { + status, + process, + message, + timestamp: new Date(), + }; + new Log(logEntry).save().catch(() => { + console.log("Error saving log entry!"); + }); +} + +export function exportIcal(events: IEvent[], calendarName: string) { + if (!events || events.length < 1) return; + + // Create a new icalGenerator... generator + const cal = icalGenerator({ + name: calendarName || siteName, + }); + events.forEach((event) => { + // Add the event to the generator + cal.createEvent({ + start: moment.tz(event.start, event.timezone), + end: moment.tz(event.end, event.timezone), + timezone: event.timezone, + summary: event.name, + description: event.description, + organizer: { + name: event.hostName || "Anonymous", + email: event.creatorEmail || "anonymous@anonymous.com", + }, + location: event.location, + url: "https://" + domain + "/" + event.id, + }); + }); + // Stringify it! + const string = cal.toString(); + return string; +} diff --git a/src/start.js b/src/start.js deleted file mode 100755 index ca17862..0000000 --- a/src/start.js +++ /dev/null @@ -1,27 +0,0 @@ -import mongoose from "mongoose"; -import { getConfig } from "./lib/config.js"; -import app from "./app.js"; - -const config = getConfig(); - -mongoose.connect(config.database.mongodb_url, { - useNewUrlParser: true, - useUnifiedTopology: true, -}); -mongoose.set("useCreateIndex", true); -mongoose.Promise = global.Promise; -mongoose.connection - .on("connected", () => { - console.log("Mongoose connection open!"); - }) - .on("error", (err) => { - console.log("Connection error: ${err.message}"); - }); - -const server = app.listen(config.general.port, () => { - console.log( - `Welcome to gathio! The app is now running on http://localhost:${ - server.address().port - }` - ); -}); diff --git a/src/start.ts b/src/start.ts new file mode 100755 index 0000000..fcdfaea --- /dev/null +++ b/src/start.ts @@ -0,0 +1,25 @@ +import mongoose from "mongoose"; +import { getConfig } from "./lib/config.js"; +import app from "./app.js"; + +const config = getConfig(); + +mongoose.connect(config.database.mongodb_url, { + useNewUrlParser: true, + useUnifiedTopology: true, +}); +mongoose.set("useCreateIndex", true); +mongoose.Promise = global.Promise; +mongoose.connection + .on("connected", () => { + console.log("Mongoose connection open!"); + }) + .on("error", (err: any) => { + console.log(`Connection error: ${err.message}`); + }); + +const server = app.listen(config.general.port, () => { + console.log( + `Welcome to gathio! The app is now running on http://localhost:${config.general.port}` + ); +}); -- cgit v1.2.3