Updates
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
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 };
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { SiteConfigMain } from "../../../types";
|
||||
import grabConfig from "./grab-config";
|
||||
|
||||
type Params = {
|
||||
userId?: string | number;
|
||||
};
|
||||
|
||||
type Return = {
|
||||
appMainConfig: SiteConfigMain;
|
||||
userMainConfig?: SiteConfigMain;
|
||||
};
|
||||
|
||||
export default function grabMainConfig(params?: Params): Return {
|
||||
const { appConfig } = grabConfig();
|
||||
const { userConfig } = grabConfig({ userId: params?.userId });
|
||||
|
||||
return { appMainConfig: appConfig.main, userMainConfig: userConfig?.main };
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import fs from "fs";
|
||||
import grabDirNames from "../names/grab-dir-names";
|
||||
import grabConfig from "./grab-config";
|
||||
import _ from "lodash";
|
||||
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 {
|
||||
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 };
|
||||
}
|
||||
Reference in New Issue
Block a user