Skip to content

Commit

Permalink
Merge pull request #360 from mStirner/dev
Browse files Browse the repository at this point in the history
fix issues
  • Loading branch information
mStirner authored Dec 17, 2023
2 parents 1692b32 + 7b5dcc7 commit c193a9c
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 49 deletions.
151 changes: 103 additions & 48 deletions components/plugins/class.plugin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const fs = require("fs");
const path = require("path");
const logger = require("../../system/logger/index.js");
const semver = require("semver");
const pkg = require("../../package.json");

//const Bootstrap = require("./class.bootstrap.js");

Expand All @@ -26,93 +28,146 @@ class Plugin {
Object.assign(this, obj);
this._id = String(obj._id);

let json = {};

Object.defineProperty(this, "logger", {
value: logger.create(`plugins/${this.uuid}`),
configurable: false,
enumerable: false,
writable: false
});

Object.defineProperty(this, "started", {
value: false,
configurable: false,
enumerable: false,
writable: true
});

try {

let file = path.resolve(process.cwd(), "plugins", this.uuid, "package.json");
let content = fs.readFileSync(file);
json = JSON.parse(content);

} catch (err) {
if (err.code === "ENOENT") {

this.logger.warn(err, `package.json for plugin "${this.name}" not found.`);

} else {

this.logger.warn(err);

}
}

Object.defineProperty(this, "package", {
value: json,
configurable: false,
enumerable: false,
writable: false,
});

}

/**
* @function start
* Start installed plugin
*/
start() {
if (this.enabled) {
if (!this.started) {
if (this.enabled) {

let plugin = path.resolve(process.cwd(), "plugins", this.uuid);
// feedback
logger.debug(`Start plugin "${this.name}"...`);

if (fs.existsSync(plugin)) {
let plugin = path.resolve(process.cwd(), "plugins", this.uuid);
let compatible = semver.satisfies(pkg.version, this.package?.backend);

let init = (dependencies, cb) => {
try {
if (!compatible) {
logger.warn(`Starting incomptaible plugin "${this.name}". It may work not properly or break something!`);
}

const granted = dependencies.every((c) => {
if (this.intents.includes(c)) {
if (fs.existsSync(plugin)) {

return true;
let init = (dependencies, cb) => {
try {

} else {
const granted = dependencies.every((c) => {
if (this.intents.includes(c)) {

logger.warn(`Plugin ${this.uuid} (${this.name}) wants to access not registerd intens "${c}"`);
return false;
return true;

}
});
} else {

if (granted) {
logger.warn(`Plugin ${this.uuid} (${this.name}) wants to access not registerd intens "${c}"`);
return false;

let components = dependencies.map((name) => {
return require(path.resolve(process.cwd(), `components/${name}`));
}
});

cb(this, components);
return init;
if (granted) {

let components = dependencies.map((name) => {
return require(path.resolve(process.cwd(), `components/${name}`));
});

cb(this, components);
return init;

} else {
} else {

throw new Error(`Unregisterd intents access approach`);

}

} catch (err) {

throw new Error(`Unregisterd intents access approach`);
logger.error(err, `Plugin could not initalize!`, err.message);
throw err;

}
};

} catch (err) {
init[Symbol.for("uuid")] = this.uuid;
init[Symbol.for("compatible")] = compatible;

logger.error(err, `Plugin could not initalize!`, err.message);
throw err;
try {

}
};
let returns = require(path.resolve(plugin, "index.js"))(this, this.logger, init);

init[Symbol.for("uuid")] = this.uuid;
if (!returns) {
return;
}

try {
if (returns[Symbol.for("uuid")] !== this.uuid) {
logger.warn(`Plugin "${this.uuid}" (${this.name}) does not return the init function!`);
throw new Error("Invalid init function returnd!");
}

let log = logger.create(`plugins/${this.uuid}`);
let returns = require(path.resolve(plugin, "index.js"))(this, log, init);
this.started = true;

if (!returns) {
return;
} catch (err) {
logger.error(`Error in plugin "${this.name}": `, err);
throw err;
}

if (returns[Symbol.for("uuid")] !== this.uuid) {
logger.warn(`Plugin "${this.uuid}" (${this.name}) does not return the init function!`);
throw new Error("Invalid init function returnd!");
}
} else {

logger.error(`Could not found plugin file/folder "${this.uuid}"`);
throw new Error("Plugin not found");

} catch (err) {
logger.error(`Error in plugin "${this.name}": `, err);
throw err;
}

} else {

logger.error(`Could not found plugin file/folder "${this.uuid}"`);
throw new Error("Plugin not found");
let err = Error("Plugin is not enabled!");
err.code = "PLUGIN_NOT_ENABLED";

}

} else {

let err = Error("Plugin is not enabled!");
err.code = "PLUGIN_NOT_ENABLED";

throw err;
throw err;

}
}
}

Expand Down
1 change: 1 addition & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"jsonwebtoken": "^9.0.0",
"mongodb": "^4.11.0",
"mqtt-packet": "^8.1.2",
"semver": "^7.5.4",
"uuid": "^9.0.0",
"ws": "^8.10.0"
},
Expand Down
5 changes: 4 additions & 1 deletion routes/router.api.plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ module.exports = (app, router) => {

} catch (err) {

res.status(500).end(err);
res.status(500).json({
error: err.message,
stack: err.stack
});

}
});
Expand Down

0 comments on commit c193a9c

Please sign in to comment.