Skip to content

Commit

Permalink
settings: get settings from application's settings.json file
Browse files Browse the repository at this point in the history
  • Loading branch information
ewsgit committed Jun 8, 2024
1 parent 0fc60cd commit 450725f
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 41 deletions.
8 changes: 4 additions & 4 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"proseWrap": "preserve",
"printWidth": 120,
"printWidth": 140,
"tabWidth": 2,
"useTabs": false,
"singleAttributePerLine": true,
Expand Down
48 changes: 27 additions & 21 deletions applications/settings/backend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ import YourDashPanel from "@yourdash/backend/src/core/helpers/panel.js";
import BackendModule, { YourDashModuleArguments } from "@yourdash/backend/src/core/moduleManager/backendModule.js";
import { readdirSync } from "fs";
import core from "@yourdash/backend/src/core/core.js";
import SettingsCategory from "../shared/types/category.js";
import EndpointSettingsCategory from "../shared/types/endpoints/setting/category.js";
import EndpointSettingCategorySetting from "../shared/types/endpoints/setting/category/setting.js";
import ISetting from "../shared/types/setting.js";
import SETTING_TYPE from "../shared/types/settingType.js";
import StoreBackendModule from "@yourdash/applications/store/backend/index.js";
import { readFileSync } from "fs";
import path from "path";

/*
* Future settings plans
Expand All @@ -22,7 +27,7 @@ import SETTING_TYPE from "../shared/types/settingType.js";

export default class SettingsModule extends BackendModule {
installableApplications: string[] = [];
settingsCategories: <SettingsCategory>[] = []
settingsCategories: { [category: string]: SettingsCategory } = {};

constructor(args: YourDashModuleArguments) {
super(args);
Expand All @@ -42,9 +47,27 @@ export default class SettingsModule extends BackendModule {
return this;
}

public loadEndpoints() {
public async loadEndpoints() {
super.loadEndpoints();

// get installed applications
const installedApplications = this.api.core.moduleManager.getModule<StoreBackendModule>("store").getInstalledApplications();

// rework this to create an object with an array for each category with an array of all it's settings
this.settingsCategories = {};

await Promise.all(
installedApplications.map(async (application) => {
const applicationSettings = JSON.parse(
readFileSync(path.join(process.cwd(), "../../applications/" + application + "/settings.json")).toString(),
) as ISetting<SETTING_TYPE>[];

applicationSettings.forEach((setting) => {
console.log(setting);
});
}),
);

// legacy endpoints
this.api.request.post("/app/settings/core/panel/position", async (req, res) => {
const { username } = req.headers as {
Expand Down Expand Up @@ -78,10 +101,7 @@ export default class SettingsModule extends BackendModule {
this.installableApplications.map(async (app) => {
if (core.globalDb.get<string[]>("core:installedApplications").includes(app)) return;

core.globalDb.set("core:installedApplications", [
...core.globalDb.get<string[]>("core:installedApplications"),
app,
]);
core.globalDb.set("core:installedApplications", [...core.globalDb.get<string[]>("core:installedApplications"), app]);

await this.api.core.restartInstance();
});
Expand All @@ -94,21 +114,7 @@ export default class SettingsModule extends BackendModule {
this.api.request.get("/cat/:categoryid", async (req, res) => {
const { categoryid } = req.params;

// TODO: implement this

return res.json(<EndpointSettingsCategory>{
displayName: "TEST CATEGORY NAME",
name: categoryid,
settings: [
{
displayName: "TEST SETTING NAME",
id: "test-setting",
type: SETTING_TYPE.BOOLEAN,
value: true,
description: "Hello world this is a description",
},
],
});
return res.json(<EndpointSettingsCategory>this.settingsCategories[categoryid]);
});

this.api.request.get("/setting/:category/:setting", async (req, res) => {
Expand Down
17 changes: 17 additions & 0 deletions applications/settings/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"id": "setting-test-id-1",
"type": "boolean",
"defaultValue": false
},
{
"id": "setting-test-id-string",
"type": "string",
"defaultValue": "default string value"
},
{
"id": "setting-test-id-date",
"type": "date",
"defaultValue": 0
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import ISetting from "../../setting.js";

export default interface EndpointSettingsCategory {
displayName: string;
name: string;
id: string;
// UNUSED but possible future idea
icon?: string;
description?: string;
settings: ISetting<never>[];
}
1 change: 1 addition & 0 deletions applications/settings/shared/types/setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export default interface ISetting<T extends SETTING_TYPE> {
type: T;
value: TsTypeForSettingType<T>;
description?: string;
category: string;
}
4 changes: 4 additions & 0 deletions applications/store/backend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export default class StoreModule extends BackendModule {
super(args);
}

getInstalledApplications(): string[] {
return this.api.core.globalDb.get<string[]>("core:installedApplications");
}

public loadEndpoints() {
super.loadEndpoints();

Expand Down
4 changes: 3 additions & 1 deletion main/backend/src/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,9 @@ export class Core {

try {
console.time("core:load_modules");
loadedModules = (await this.moduleManager.loadInstalledApplications()).filter((x) => x !== undefined);
loadedModules = (await this.moduleManager.loadInstalledApplications()).filter(
(x) => x !== undefined && x !== null,
);

console.timeEnd("core:load_modules");
this.log.info("startup", "All modules loaded successfully");
Expand Down
16 changes: 16 additions & 0 deletions main/backend/src/core/coreLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ export default class CoreLog {
}

info(level: string, ...message: (string | Uint8Array)[]) {
if (level.length === 0) {
throw new Error("log level is empty");
}

if (message.length === 0) {
throw new Error("log message is empty");
}
Expand All @@ -79,6 +83,10 @@ export default class CoreLog {
}

success(level: string, ...message: (string | Uint8Array)[]) {
if (level.length === 0) {
throw new Error("log level is empty");
}

if (message.length === 0) {
throw new Error("log message is empty");
}
Expand All @@ -92,6 +100,10 @@ export default class CoreLog {
}

warning(level: string, ...message: (string | Uint8Array)[]) {
if (level.length === 0) {
throw new Error("log level is empty");
}

if (message.length === 0) {
throw new Error("log message is empty");
}
Expand Down Expand Up @@ -124,6 +136,10 @@ export default class CoreLog {
return this;
}

if (level.length === 0) {
throw new Error("log level is empty");
}

if (message.length === 0) {
throw new Error("log message is empty");
}
Expand Down
19 changes: 7 additions & 12 deletions main/backend/src/core/moduleManager/coreModuleManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default class CoreModuleManager {
async loadModule(moduleName: string, modulePath: string): Promise<BackendModule | undefined> {
// if the module is not valid or doesn't require a backend module, return
if (!this.checkModule(modulePath)) {
return;
throw "Invalid module";
}

if (!(await this.core.fs.doesExist(`${modulePath}/index.ts`, true))) {
Expand All @@ -55,23 +55,18 @@ export default class CoreModuleManager {
try {
const mod = await import(`${fileUrl(modulePath)}/index.js`);
if (!mod.default) {
this.core.log.error(
"module_manager",
`Unable to load ${moduleName}! This application does not contain a default export!`,
);
this.core.log.error("module_manager", `Unable to load ${moduleName}! This application does not contain a default export!`);
return;
}

const initializedModule = new mod.default({ moduleName: moduleName, modulePath: modulePath });
this.loadedModules.push(mod);
this.core.log.success(
"module_manager",
`Loaded module: "${moduleName}" in ${new Date().getTime() - startTime.getTime()}ms`,
);
this.core.log.success("module_manager", `Loaded module: "${moduleName}" in ${new Date().getTime() - startTime.getTime()}ms`);
this.loadedModules.push(initializedModule);
return initializedModule;
} catch (err) {
this.core.log.error("module_manager", `Invalid module: "${moduleName}"`, err);
return null;
return;
}
}

Expand Down Expand Up @@ -120,7 +115,7 @@ export default class CoreModuleManager {
return loadedApplications;
}

getModule(moduleName: string): BackendModule | undefined {
return this.loadedModules.find((module) => module.moduleName === moduleName);
getModule<T extends BackendModule>(moduleName: string): T | undefined {
return this.loadedModules.find((module) => module.moduleName === moduleName) as T | undefined;
}
}
7 changes: 6 additions & 1 deletion main/backend/src/lib/keyValueDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ export default class KeyValueDatabase extends KVD {

async writeToDisk(path: string): Promise<boolean> {
try {
await (await core.fs.getFile(path)).write(JSON.stringify(this.keys));
if (core.isDevMode) {
await (await core.fs.getFile(path)).write(JSON.stringify(this.keys, null, 2));
} else {
await (await core.fs.getFile(path)).write(JSON.stringify(this.keys));
}

return true;
} catch (_err) {
return false;
Expand Down

0 comments on commit 450725f

Please sign in to comment.