Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Set configs of plugin modules in package.json #35

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions packages/tcore-api/src/Plugins/Module/Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { IModuleSetup, TModuleState } from "@tago-io/tcore-sdk/types";
import { flattenConfigFields } from "@tago-io/tcore-shared";
import { io } from "../../Socket/SocketServer";
import { getPluginSettings } from "../../Services/Settings";
import { getPluginSettings, getPluginSettingsFolder } from "../../Services/Settings";
import Plugin from "../Plugin/Plugin";
import { checkMainDatabaseModuleHook } from "../../Services/Plugins";

Expand Down Expand Up @@ -132,7 +132,7 @@ class Module {
const moduleSettings = settings?.modules?.find((x) => x.id === this.id);
const values = moduleSettings?.values || {};

const conf = this.setup.configs || [];
const conf = await this.getConfigDefinitions();
const defs = conf?.filter((x) => "defaultValue" in x && x.defaultValue !== "");
const flat = flattenConfigFields(defs);
const defsObject = {};
Expand All @@ -148,6 +148,21 @@ class Module {
...values,
};
}

public async getConfigDefinitions() {
const configurations = this.plugin.package?.tcore?.configs?.[this.setup.id] || [];
for (const config of configurations) {
if (typeof config.defaultValue === "string" && config.defaultValue.includes("$PLUGIN_SETTINGS$")) {
// replace $PLUGIN_SETTINGS$ with the folder location where the plugin has its settings
config.defaultValue = config.defaultValue.replace(
"$PLUGIN_SETTINGS$",
await getPluginSettingsFolder(this.plugin.id)
);
}
}

return configurations;
}
}

export default Module;
12 changes: 7 additions & 5 deletions packages/tcore-api/src/Services/Plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,13 @@ export async function getPluginInfo(id: TGenericID): Promise<IPlugin | null> {
const mainSettings = await getMainSettings();
const settings = await getPluginSettings(id);

const modules = [...plugin.modules.values()].map((module) => {
const modules: any[] = [];

for (const module of [...plugin.modules.values()]) {
const moduleSettings = settings?.modules?.find((y) => y.id === module.setup.id);
const moduleValues = moduleSettings?.values || {};

const moduleFields = flattenConfigFields(module.setup.configs || []);
const moduleFields = flattenConfigFields(await module.getConfigDefinitions());
for (const field of moduleFields) {
// we override the default value of the config field to use the last
// inserted value or the actual default from the module configuration
Expand All @@ -322,13 +324,13 @@ export async function getPluginInfo(id: TGenericID): Promise<IPlugin | null> {
}
}

return {
modules.push({
...module.setup,
error: module.error,
message: module.message,
state: module.state,
};
});
});
}

const dbPluginID = String(mainSettings.database_plugin).split(":")[0];
const allow_disable = dbPluginID !== id;
Expand Down
2 changes: 1 addition & 1 deletion packages/tcore-api/src/Services/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export async function setPluginModulesSettings(id: string, values: any) {

for (const item of values) {
const module = plugin?.modules?.get(item.moduleID);
const configs = flattenConfigFields(module?.setup.configs || []);
const configs = flattenConfigFields((await module?.getConfigDefinitions()) || []);
const field = configs.find((x) => "field" in x && x.field === item.field);
const isPassword = field?.type === "password";

Expand Down
2 changes: 1 addition & 1 deletion packages/tcore-console/src/Components/Icon/Icon.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Types of icons available in the system.
* Each one must match the corresponding filename in the /assets/icons folder.
*/
enum EIcon {
enum EIcon {
"alpine" = "alpine",
"apple" = "apple",
"arrow-alt-circle-down" = "arrow-alt-circle-down",
Expand Down