From 751a49f268ea57e9d17f3354e2a851ce362c4a0e Mon Sep 17 00:00:00 2001 From: Slawomir Koszewski Date: Sat, 7 Mar 2026 10:46:32 +0100 Subject: [PATCH] add config module and update exports in package.json --- package.json | 3 ++- src/config/index.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 src/config/index.ts diff --git a/package.json b/package.json index 793da92..714bc96 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "exports": { ".": "./dist/index.js", "./markdown": "./dist/markdown.js", - "./cli/utils": "./dist/cli/utils.js" + "./cli/utils": "./dist/cli/utils.js", + "./config": "./dist/config/index.js" } } diff --git a/src/config/index.ts b/src/config/index.ts new file mode 100644 index 0000000..b12c1df --- /dev/null +++ b/src/config/index.ts @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: MIT + +import { readFile } from "node:fs/promises"; +import os from "node:os"; +import path from "node:path"; + +function getConfigDir(moduleName: string): string { + if (process.platform === "win32") { + return path.join(process.env.LOCALAPPDATA ?? path.join(os.homedir(), "AppData", "Local"), moduleName); + } + + return path.join(process.env.XDG_CONFIG_HOME ?? path.join(os.homedir(), ".config"), moduleName); +} + +export async function getConfig(moduleName: string): Promise { + const configPath = path.join(getConfigDir(moduleName), "config.json"); + + return readFile(configPath, "utf8") + .then((configJson) => JSON.parse(configJson) as unknown) + .catch((err: unknown) => { + if ((err as { code?: string } | null)?.code === "ENOENT") { + return {}; + } + + throw err; + }); +}