From 45fa85a6f46fff3e2883738954bb9c18e8904ad8 Mon Sep 17 00:00:00 2001 From: BinarySandia04 Date: Tue, 1 Oct 2024 14:57:53 +0200 Subject: [PATCH] Connected Plugins with backend (Part 1) --- .gitignore | 14 +++++---- README.md | 21 ++++++++++++- backend/server.js | 4 +++ backend/services/plugins.js | 34 ++++++++++++++++++++++ client/src/services/ContextMenu.js | 1 + client/src/views/partials/ConceptEntry.vue | 4 +-- plugins/dnd-5e/backend/main.js | 6 ++++ plugins/dnd-5e/client/main.js | 3 +- plugins/dnd-5e/plugin.json | 4 +++ plugins/example/plugin.json | 1 + prebuild.js | 5 ++++ 11 files changed, 87 insertions(+), 10 deletions(-) create mode 100644 backend/services/plugins.js create mode 100644 plugins/dnd-5e/backend/main.js diff --git a/.gitignore b/.gitignore index 12df3797..e6e7ad24 100644 --- a/.gitignore +++ b/.gitignore @@ -1,16 +1,20 @@ .DS_Store server/node_modules/ -client/node_modules/ node_modules/ -client/dist/ -client/plugins/ -backend/plugins/ -client/public/data/ server/uploads/ + +# Client +client/plugins/ +client/dist/ +client/public/data/ +client/node_modules/ client/src/locales/ client/public/plugins/ +# Backend +backend/plugins/ + # local env files .env.local .env.*.local diff --git a/README.md b/README.md index b6f0019e..70dec35d 100644 --- a/README.md +++ b/README.md @@ -15,5 +15,24 @@ Dragonroll is an open source online virtual tabletop aimed to be an alternative ## Installation 🛠️ -TODO +Run +``` +./install.sh +``` +This will install all the necesary npm packages both for the frontend and the backend + +## Running 🚀 + +Run +``` +./start.sh +``` + +or + +``` +./start-dev.sh +``` + +if you want to start the development environment diff --git a/backend/server.js b/backend/server.js index 37e753ed..e82e2627 100755 --- a/backend/server.js +++ b/backend/server.js @@ -13,6 +13,8 @@ const passport = require('passport'); const server = http.createServer(app); const config = JSON.parse(fs.readFileSync("config.json")); +const pluginManager = require('./services/plugins') + // SET CONSTANTS const PORT = 8081; global.appRoot = path.resolve(__dirname); @@ -69,6 +71,8 @@ app.use('/user', require('./routes/user')); checkAuth = passport.authenticate('jwt', {session: false}); app.use(checkAuth); +pluginManager.init(); + // ROUTES WITH AUTH app.use('/campaign', require('./routes/campaign')); app.use('/maps', require('./routes/map')) diff --git a/backend/services/plugins.js b/backend/services/plugins.js new file mode 100644 index 00000000..5c9f8c92 --- /dev/null +++ b/backend/services/plugins.js @@ -0,0 +1,34 @@ +const fs = require('fs'); +const path = require('path') +const Api = {} + +const basePath = path.resolve(__dirname, '../') +console.log(basePath) + +let pluginsInfo = []; +let plugins = {}; + +function init(){ + console.log("Initializing plugins"); + const pluginFolders = fs.readdirSync(path.resolve(basePath + '/plugins')); + pluginFolders.forEach(pluginFolder => { + pluginsInfo.push(JSON.parse(fs.readFileSync( + path.resolve(basePath, 'plugins', pluginFolder, "plugin.json") + ))); + }); + + console.log("Launching using the following plugins:"); + pluginsInfo.forEach(pluginInfo => { + console.log(`\t- ${pluginInfo.name}`); + plugins[pluginInfo.package] = require(`${basePath}/plugins/${pluginInfo.package}/${pluginInfo.backend.entrypoint}`); + }); + + // Execute main + Object.keys(plugins).forEach(k => { + plugins[k].Main(Api) + }) +} + +module.exports = { + init +} \ No newline at end of file diff --git a/client/src/services/ContextMenu.js b/client/src/services/ContextMenu.js index f1805498..6639bdba 100644 --- a/client/src/services/ContextMenu.js +++ b/client/src/services/ContextMenu.js @@ -26,6 +26,7 @@ function PopulateContext(val){ let children = []; let elementNum = 0; + console.log(val); val.forEach(element => { let contextMenuElement = document.createElement('div'); contextMenuElement.classList.add("context-menu-element"); diff --git a/client/src/views/partials/ConceptEntry.vue b/client/src/views/partials/ConceptEntry.vue index c87f4d04..e04de74b 100644 --- a/client/src/views/partials/ConceptEntry.vue +++ b/client/src/views/partials/ConceptEntry.vue @@ -23,13 +23,13 @@ async function updateElement(){ if(tooltip) AddTooltip(tooltipContainer.value, tooltip); } -onMounted(() => { +onMounted(async () => { updateElement(); watch(() => props.element, () => { updateElement(); }); - let context = props.context(); + let context = await props.context(); if(context) AddContextMenu(elementDiv.value, context); }) diff --git a/plugins/dnd-5e/backend/main.js b/plugins/dnd-5e/backend/main.js new file mode 100644 index 00000000..2b6e41a1 --- /dev/null +++ b/plugins/dnd-5e/backend/main.js @@ -0,0 +1,6 @@ +// Entrypoint +function Main(Api){ + console.log("Hello World from backend!"); +} + +export { Main }; \ No newline at end of file diff --git a/plugins/dnd-5e/client/main.js b/plugins/dnd-5e/client/main.js index 627afb4d..0a2365af 100644 --- a/plugins/dnd-5e/client/main.js +++ b/plugins/dnd-5e/client/main.js @@ -1,6 +1,5 @@ -// TODO: We should move client/plugin.json to plugin.json and generate the client plugin.json -// with the prebuild.js +// Entrypoint function Main(Api){ console.log("Hello World!"); console.log(Api); diff --git a/plugins/dnd-5e/plugin.json b/plugins/dnd-5e/plugin.json index 85abcb4d..cabb5f72 100644 --- a/plugins/dnd-5e/plugin.json +++ b/plugins/dnd-5e/plugin.json @@ -1,4 +1,5 @@ { + "package": "dnd-5e", "name": "Dnd 5e System", "description": "This plugin provides the Dnd 5e module", "authors": [ @@ -9,5 +10,8 @@ "version": "0.1", "client": { "entrypoint": "main.js" + }, + "backend": { + "entrypoint": "main.js" } } \ No newline at end of file diff --git a/plugins/example/plugin.json b/plugins/example/plugin.json index 32f58e8a..8eeb3ebf 100644 --- a/plugins/example/plugin.json +++ b/plugins/example/plugin.json @@ -1,4 +1,5 @@ { + "package": "example-plugin" "name": "Dragonroll example plugin", "description": "This is an example plugin to help you getting started with the Dragonroll API", "authors": [ diff --git a/prebuild.js b/prebuild.js index 48d0c217..999799b2 100755 --- a/prebuild.js +++ b/prebuild.js @@ -97,6 +97,11 @@ for(let j = 0; j < plugins.length; j++){ fs.copyFileSync(`./plugins/${plugins[j]}/plugin.json`, `./client/plugins/${plugins[j]}/plugin.json`); } + if(fs.existsSync(`./plugins/${plugins[j]}/backend/`)){ + fs.cpSync(`./plugins/${plugins[j]}/backend/`, `./backend/plugins/${plugins[j]}`, {recursive: true}); + fs.copyFileSync(`./plugins/${plugins[j]}/plugin.json`, `./backend/plugins/${plugins[j]}/plugin.json`); + } + if(fs.existsSync(`./plugins/${plugins[j]}/public/`)){ fs.cpSync(`./plugins/${plugins[j]}/public/`, `./client/public/plugins/${plugins[j]}`, {recursive: true}); }