datasquirel/package-shared/utils/backend/config/grab-config.ts
Benjamin Toby 7e8bb37c09 Updates
2025-07-05 14:59:30 +01:00

48 lines
1.2 KiB
TypeScript

import fs from "fs";
import { SiteConfig } from "../../../types";
import grabDirNames from "../names/grab-dir-names";
import EJSON from "../../ejson";
import envsub from "../../envsub";
type Params = {
userId?: string | number;
};
type Return = {
appConfig: SiteConfig;
userConfig: SiteConfig | null;
};
export default function grabConfig(params?: Params): Return {
const { appConfigJSONFile, userConfigJSONFilePath } = grabDirNames({
userId: params?.userId,
});
const appConfigJSON = envsub(fs.readFileSync(appConfigJSONFile, "utf-8"));
const appConfig = EJSON.parse(appConfigJSON) as SiteConfig;
if (!userConfigJSONFilePath) {
return { appConfig, userConfig: null };
}
if (!fs.existsSync(userConfigJSONFilePath)) {
fs.writeFileSync(
userConfigJSONFilePath,
JSON.stringify({
main: {},
}),
"utf-8"
);
}
const userConfigJSON = envsub(
fs.readFileSync(userConfigJSONFilePath, "utf-8")
);
const userConfig = (EJSON.parse(userConfigJSON) || {
main: {},
}) as SiteConfig;
return { appConfig, userConfig };
}