From 1c42de27e11e6d6c2b42388e615a66afa94dc826 Mon Sep 17 00:00:00 2001 From: aXenDeveloper Date: Wed, 5 Jun 2024 11:55:59 +0200 Subject: [PATCH] fix(plugins): Add support for add/remove paths in global.d.ts in frontend --- .../core/admin/plugins/delete/contents.ts | 13 ++++++++++ .../admin/plugins/delete/delete.service.ts | 2 +- .../helpers/files/change/change.service.ts | 25 ++++++++++++++++++- .../plugins/helpers/files/change/contents.ts | 13 ++++++++++ .../utils/actions/helpers/update-plugins.ts | 11 +++++--- 5 files changed, 58 insertions(+), 6 deletions(-) diff --git a/backend/src/plugins/core/admin/plugins/delete/contents.ts b/backend/src/plugins/core/admin/plugins/delete/contents.ts index ccb609f20..389c2d16d 100644 --- a/backend/src/plugins/core/admin/plugins/delete/contents.ts +++ b/backend/src/plugins/core/admin/plugins/delete/contents.ts @@ -30,3 +30,16 @@ export const removeDatabaseFromService = ({ "" ); }; + +export const removeLangFromTypes = ({ + code, + content +}: { + code: string; + content: string; +}) => { + return content.replace( + `\n typeof import("@/plugins/${code}/langs/en.json") &`, + "" + ); +}; diff --git a/backend/src/plugins/core/admin/plugins/delete/delete.service.ts b/backend/src/plugins/core/admin/plugins/delete/delete.service.ts index 04e8cf250..9b0cc6a4f 100644 --- a/backend/src/plugins/core/admin/plugins/delete/delete.service.ts +++ b/backend/src/plugins/core/admin/plugins/delete/delete.service.ts @@ -70,7 +70,7 @@ export class DeleteAdminPluginsService { const modulePath = ABSOLUTE_PATHS.plugin({ code }).root; this.deleteFolderWhenExists(modulePath); // Frontend - const frontendPaths = ["admin_pages", "pages", "plugin"]; + const frontendPaths = ["admin_pages", "pages", "plugin", "pages_container"]; frontendPaths.forEach(path => { this.deleteFolderWhenExists( ABSOLUTE_PATHS.plugin({ code }).frontend[path] diff --git a/backend/src/plugins/core/admin/plugins/helpers/files/change/change.service.ts b/backend/src/plugins/core/admin/plugins/helpers/files/change/change.service.ts index c8de10d30..84ce48c6f 100644 --- a/backend/src/plugins/core/admin/plugins/helpers/files/change/change.service.ts +++ b/backend/src/plugins/core/admin/plugins/helpers/files/change/change.service.ts @@ -3,10 +3,15 @@ import * as fs from "fs"; import { Injectable } from "@nestjs/common"; -import { changeDatabaseService, changeModuleRootSchema } from "./contents"; +import { + changeDatabaseService, + changeLangTypes, + changeModuleRootSchema +} from "./contents"; import { removeDatabaseFromService, + removeLangFromTypes, removeModuleFromRootSchema } from "../../../delete/contents"; import { CustomError } from "@/utils/errors/custom-error"; @@ -68,6 +73,15 @@ export class ChangeFilesAdminPluginsService { code }), condition: () => true + }, + { + path: join(ABSOLUTE_PATHS.frontend.init, "global.d.ts"), + content: content => + changeLangTypes({ + content, + code + }), + condition: () => true } ]; @@ -93,6 +107,15 @@ export class ChangeFilesAdminPluginsService { code }), condition: () => true + }, + { + path: join(ABSOLUTE_PATHS.frontend.init, "global.d.ts"), + content: content => + removeLangFromTypes({ + content, + code + }), + condition: () => true } ]; diff --git a/backend/src/plugins/core/admin/plugins/helpers/files/change/contents.ts b/backend/src/plugins/core/admin/plugins/helpers/files/change/contents.ts index 33ddf4d87..415f68e2e 100644 --- a/backend/src/plugins/core/admin/plugins/helpers/files/change/contents.ts +++ b/backend/src/plugins/core/admin/plugins/helpers/files/change/contents.ts @@ -43,3 +43,16 @@ export const changeDatabaseService = ({ `\n ...table${name},\n // ! === MODULE ===` ); }; + +export const changeLangTypes = ({ + code, + content +}: { + code: string; + content: string; +}) => { + return content.replace( + "// ! === IMPORT ===", + `typeof import("@/plugins/${code}/langs/en.json") &\n // ! === IMPORT ===` + ); +}; diff --git a/backend/src/utils/actions/helpers/update-plugins.ts b/backend/src/utils/actions/helpers/update-plugins.ts index 8ca100d41..4aa1a61a0 100644 --- a/backend/src/utils/actions/helpers/update-plugins.ts +++ b/backend/src/utils/actions/helpers/update-plugins.ts @@ -17,6 +17,10 @@ export const updatePlugins = async ({ }) => { fs.readdir(join(process.cwd(), "src", "plugins"), async (err, plugins) => { let isDefaultIndex: number | null = null; + const defaultPlugin = await db.query.core_plugins.findFirst({ + where: (table, { eq }) => eq(table.default, true) + }); + await Promise.all( plugins .filter( @@ -48,7 +52,6 @@ export const updatePlugins = async ({ await db .update(core_plugins) .set({ - updated: new Date(), name: config.name, description: config.description, support_url: config.support_url, @@ -60,7 +63,7 @@ export const updatePlugins = async ({ }) .where(eq(core_plugins.id, pluginId)); } else { - const plugin = await db + const pluginInsert = await db .insert(core_plugins) .values([ { @@ -73,12 +76,12 @@ export const updatePlugins = async ({ allow_default: config.allow_default, version: version ?? null, version_code: latestVersion ? +latestVersion : null, - default: isDefaultIndex === index + default: isDefaultIndex === index && !defaultPlugin } ]) .returning(); - pluginId = plugin[0].id; + pluginId = pluginInsert[0].id; } }) );