Files
sk-tools/src/config/index.ts
Slawomir Koszewski f5877d2ccd
All checks were successful
build / build (push) Successful in 7s
Exporteed getConfigDir.
2026-03-07 15:26:38 +01:00

28 lines
873 B
TypeScript

// SPDX-License-Identifier: MIT
import { readFile } from "node:fs/promises";
import os from "node:os";
import path from "node:path";
export 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, configName = "config"): Promise<unknown> {
const configPath = path.join(getConfigDir(moduleName), `${configName}.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;
});
}