import { readFile } from "fs/promises"; import stripJsonComments from "strip-json-comments"; import args from "./cli.js"; import crashHandler from "./error.js"; import { config } from "./types.js"; /** * Reads the config file and returns the config object * @returns The config object */ async function readConfig(): Promise { const conf = await readFile(args.config, "utf8").catch((err) => { crashHandler("Error reading config file.", err); return "CRASH"; }); if (args.verbose) { console.log(`Read config file: ${args.config}\n${conf}`); } return conf; } /** * Parses the config file and returns it as a JSON object * * See config.sample.jsonc for an example of the config file * @returns {Promise} The config file as a JSON object(see {@link config}) * @example * const cfg = await getConfig(); * console.log(cfg.instance); * // Prints "https://mastodon.social" */ export default async function getConfig(): Promise { let cfg: config; try { cfg = JSON.parse( stripJsonComments( await readConfig().catch((err) => { crashHandler("Error reading config file.", err); return ""; }) ) ); } catch (err) { crashHandler("Error parsing config file.", err); return {} as config; } return cfg; }