28 lines
873 B
TypeScript
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;
|
|
});
|
|
}
|