add config module and update exports in package.json

This commit is contained in:
2026-03-07 10:46:32 +01:00
parent 9a6ff66d72
commit 751a49f268
2 changed files with 29 additions and 1 deletions

View File

@@ -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"
}
}

27
src/config/index.ts Normal file
View File

@@ -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<unknown> {
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;
});
}