From 0bc230fb0ba8797aae0ea090061fae9c63989895 Mon Sep 17 00:00:00 2001 From: pike Date: Sun, 3 Dec 2023 17:57:05 +0100 Subject: [PATCH] feat: Add Fairpost as a singleton service --- src/bootstrap-cli.ts | 21 ++++++++++++++ src/cli.ts | 6 ++-- src/core/Fairpost.ts | 65 ++++++++++++++++++++++++++++++++++++++++++++ src/core/Feed.ts | 25 ++--------------- 4 files changed, 91 insertions(+), 26 deletions(-) create mode 100644 src/bootstrap-cli.ts create mode 100644 src/core/Fairpost.ts diff --git a/src/bootstrap-cli.ts b/src/bootstrap-cli.ts new file mode 100644 index 0000000..db50bcf --- /dev/null +++ b/src/bootstrap-cli.ts @@ -0,0 +1,21 @@ +import * as dotenv from "dotenv"; +import * as path from "path"; +import * as fs from "fs"; + +// allow cli to switch .env file +const configPath = + process.argv + .find((element) => element.startsWith(`--config=`)) + ?.replace(`--config=`, "") ?? ".env"; +const configPathResolved = path.resolve(__dirname + "/../" + configPath); + +if (!fs.existsSync(configPathResolved)) { + throw new Error("Missing config file: " + configPathResolved); +} +dotenv.config({ path: configPathResolved }); + +// allow cli to override FAIRPOST_LOGGER_* +if (process.argv.includes("--verbose")) { + process.env.FAIRPOST_LOGGER_LEVEL = "TRACE"; + process.env.FAIRPOST_LOGGER_CONSOLE = "true"; +} diff --git a/src/cli.ts b/src/cli.ts index 10e5395..c848b96 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -3,10 +3,10 @@ Fairpost cli handler */ -import "./bootstrap-cli"; import * as path from "path"; +import "./bootstrap-cli"; +import Fairpost from "./core/Fairpost"; import Logger from "./core/Logger"; -import Feed from "./core/Feed"; import { PostStatus } from "./core/Post"; import { PlatformId } from "./platforms"; @@ -37,7 +37,7 @@ async function main() { let result: unknown; let report = ""; - const feed = new Feed(); + const feed = Fairpost.getFeed(); Logger.trace( "Fairpost " + feed.id + " " + COMMAND, DRY_RUN ? " dry-run" : "", diff --git a/src/core/Fairpost.ts b/src/core/Fairpost.ts new file mode 100644 index 0000000..2b65923 --- /dev/null +++ b/src/core/Fairpost.ts @@ -0,0 +1,65 @@ +import * as fs from "fs"; +import * as path from "path"; +import Feed from "./Feed"; +import Platform from "./Platform"; +import Storage from "./Storage"; +import Logger from "./Logger"; +import * as platforms from "../platforms"; + +/** + * Fairpost - singleton service for fairpost app + * + * - provides getFeed() + * all the fairpost logic comes from a feed + */ + +class Fairpost { + static instance: Fairpost; + + platforms = [] as Platform[]; + + constructor() { + if (Fairpost.instance) { + throw new Error("Fairpost: call getInstance() instead"); + } + this.loadPlatforms(); + } + + static getInstance(): Fairpost { + if (!Fairpost.instance) { + Fairpost.instance = new Fairpost(); + } + return Fairpost.instance; + } + + public loadPlatforms() { + const activePlatformIds = Storage.get("settings", "FEED_PLATFORMS").split( + ",", + ); + + const platformClasses = fs.readdirSync( + path.resolve(__dirname + "/../platforms"), + ); + + platformClasses.forEach((file) => { + const constructor = file.replace(".ts", "").replace(".js", ""); + // nb import * as platforms loaded the constructors + if (platforms[constructor] !== undefined) { + const platform = new platforms[constructor](); + platform.active = activePlatformIds.includes(platform.id); + this.platforms.push(platform); + } + }); + } + + public getFeed() { + return new Feed(this.platforms.filter((p) => p.active)); + } + + public fatal(msg: string) { + Logger.fatal(msg); + throw new Error(msg); + } +} + +export default Fairpost.getInstance(); diff --git a/src/core/Feed.ts b/src/core/Feed.ts index 848cbff..658b0e9 100644 --- a/src/core/Feed.ts +++ b/src/core/Feed.ts @@ -1,12 +1,10 @@ import * as fs from "fs"; -import * as path from "path"; import Logger from "./Logger"; import Storage from "./Storage"; import Platform from "./Platform"; import Folder from "./Folder"; import Post from "./Post"; import { PostStatus } from "./Post"; -import * as platforms from "../platforms"; import { PlatformId } from "../platforms"; /** @@ -36,30 +34,11 @@ export default class Feed { * @param configPath - path to file for dotenv to parse */ - constructor() { + constructor(platforms: Platform[]) { + platforms.forEach((p) => (this.platforms[p.id] = p)); this.path = Storage.get("settings", "FEED_PATH"); this.id = this.path; - this.interval = Number(Storage.get("settings", "FEED_INTERVAL", "7")); - const activePlatformIds = Storage.get("settings", "FEED_PLATFORMS").split( - ",", - ); - - const platformClasses = fs.readdirSync( - path.resolve(__dirname + "/../platforms"), - ); - - platformClasses.forEach((file) => { - const constructor = file.replace(".ts", "").replace(".js", ""); - // nb import * as platforms loaded the constructors - if (platforms[constructor] !== undefined) { - const platform = new platforms[constructor](); - if (activePlatformIds.includes(platform.id)) { - platform.active = true; - this.platforms[platform.id] = platform; - } - } - }); } /**