/** * Sync settings from the environment. A job is enabled only when its config * is present: the public sources (no credentials) are named in SYNC_JOBS; * jobs that need a URL or a login are enabled by those variables alone. */ import { homelabSettings, type HomelabSettings } from "./jobs/homelab.ts"; export interface SyncSettings { /** Public jobs switched on in SYNC_JOBS, e.g. "archidekt,bgg,covers". */ enabled: Set; /** SYNC__MINUTES overrides a job's interval. */ minutes: Record; /** SYNC__HEALTHCHECK_URL. */ healthchecks: Record; bggToken?: string; tmdbApiKey?: string; /** Suwayomi, Kavita, Bambuddy and Homebox: enabled by their own URL and login. */ homelab: HomelabSettings; } const envKey = (job: string) => job.toUpperCase().replace(/[^A-Z0-9]/g, "_"); export function syncSettings(env: NodeJS.ProcessEnv = process.env): SyncSettings { const enabled = new Set((env.SYNC_JOBS ?? "").split(",").map((s) => s.trim().toLowerCase()).filter(Boolean)); const minutes: Record = {}; const healthchecks: Record = {}; for (const [k, v] of Object.entries(env)) { if (!v) continue; const m = /^SYNC_([A-Z0-9_]+)_(MINUTES|HEALTHCHECK_URL)$/.exec(k); if (!m) continue; const job = m[1]!.toLowerCase().replace(/_/g, "-"); if (m[2] === "MINUTES" && Number(v) > 0) minutes[job] = Number(v); if (m[2] === "HEALTHCHECK_URL") healthchecks[job] = v; } return { enabled, minutes, healthchecks, bggToken: env.BGG_TOKEN || undefined, tmdbApiKey: env.TMDB_API_KEY || undefined, homelab: homelabSettings(env), }; } /** The env var name for one job's setting, for docs and messages. */ export const syncEnvName = (job: string, what: "MINUTES" | "HEALTHCHECK_URL") => `SYNC_${envKey(job)}_${what}`;