add config module and update exports in package.json
This commit is contained in:
@@ -38,6 +38,7 @@
|
|||||||
"exports": {
|
"exports": {
|
||||||
".": "./dist/index.js",
|
".": "./dist/index.js",
|
||||||
"./markdown": "./dist/markdown.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
27
src/config/index.ts
Normal 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;
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user