This commit is contained in:
Benjamin Toby
2025-07-05 14:59:30 +01:00
parent 6e334c2525
commit 7e8bb37c09
526 changed files with 17560 additions and 11386 deletions
@@ -0,0 +1,10 @@
import { SiteConfig } from "../../../types";
type Params = {
userId?: string | number;
};
type Return = {
appConfig: SiteConfig;
userConfig: SiteConfig | null;
};
export default function grabConfig(params?: Params): Return;
export {};
+24
View File
@@ -0,0 +1,24 @@
import fs from "fs";
import grabDirNames from "../names/grab-dir-names";
import EJSON from "../../ejson";
import envsub from "../../envsub";
export default function grabConfig(params) {
const { appConfigJSONFile, userConfigJSONFilePath } = grabDirNames({
userId: params === null || params === void 0 ? void 0 : params.userId,
});
const appConfigJSON = envsub(fs.readFileSync(appConfigJSONFile, "utf-8"));
const appConfig = EJSON.parse(appConfigJSON);
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: {},
});
return { appConfig, userConfig };
}
@@ -0,0 +1,10 @@
import { SiteConfigMain } from "../../../types";
type Params = {
userId?: string | number;
};
type Return = {
appMainConfig: SiteConfigMain;
userMainConfig?: SiteConfigMain;
};
export default function grabMainConfig(params?: Params): Return;
export {};
@@ -0,0 +1,6 @@
import grabConfig from "./grab-config";
export default function grabMainConfig(params) {
const { appConfig } = grabConfig();
const { userConfig } = grabConfig({ userId: params === null || params === void 0 ? void 0 : params.userId });
return { appMainConfig: appConfig.main, userMainConfig: userConfig === null || userConfig === void 0 ? void 0 : userConfig.main };
}
@@ -0,0 +1,11 @@
import { SiteConfig } from "../../../types";
type Params = {
userId?: string | number;
newConfig?: SiteConfig;
};
type Return = {
success?: boolean;
msg?: string;
};
export default function updateUserConfig({ newConfig, userId, }: Params): Return;
export {};
@@ -0,0 +1,25 @@
import fs from "fs";
import grabDirNames from "../names/grab-dir-names";
import grabConfig from "./grab-config";
import _ from "lodash";
export default function updateUserConfig({ newConfig, userId, }) {
if (!userId || !newConfig) {
return {
success: false,
msg: `UserID or newConfig not provided`,
};
}
const { userConfigJSONFilePath } = grabDirNames({
userId,
});
if (!userConfigJSONFilePath || !fs.existsSync(userConfigJSONFilePath)) {
return {
success: false,
msg: `userConfigJSONFilePath not found!`,
};
}
const { userConfig: existingUserConfig } = grabConfig({ userId });
const updateConfig = _.merge(existingUserConfig, newConfig);
fs.writeFileSync(userConfigJSONFilePath, JSON.stringify(updateConfig), "utf-8");
return { success: true };
}