specpress
Version:
Export PDF and/or DOCX files from a subset of Markdown, ASN.1 and JSON files
36 lines (27 loc) • 854 B
JavaScript
import { readFileSync, existsSync } from "fs";
import { resolve, join } from "path";
const configCache = new Map();
export const getConfig = (key, folderPath) => {
const configPath = findFileInParentFolders("sp.config.json", folderPath);
if (!configPath) throw new Error("sp.config.json not found");
if (!configCache.has(configPath)) {
configCache.set(
configPath,
JSON.parse(readFileSync(configPath, "utf-8")),
);
}
return configCache.get(configPath)?.[key];
};
export const clearConfigCache = () => {
configCache.clear();
};
const findFileInParentFolders = (fileName, currentDir = process.cwd()) => {
let dir = currentDir;
while (true) {
const filePath = join(dir, fileName);
if (existsSync(filePath)) return filePath;
const parentDir = resolve(dir, "..");
if (parentDir === dir) return null;
dir = parentDir;
}
};