Testing
This commit is contained in:
parent
aa872bf782
commit
b712748867
@ -1,8 +1,13 @@
|
|||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
const { log } = require("../src/util");
|
const { log } = require("../src/util");
|
||||||
|
const path = require("path");
|
||||||
|
|
||||||
class PluginsManager {
|
class PluginsManager {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Plugin List
|
||||||
|
* @type {Plugin[]}
|
||||||
|
*/
|
||||||
pluginList = [];
|
pluginList = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -10,29 +15,93 @@ class PluginsManager {
|
|||||||
*/
|
*/
|
||||||
pluginsDir;
|
pluginsDir;
|
||||||
|
|
||||||
constructor(dir) {
|
/**
|
||||||
|
*
|
||||||
|
* @param {UptimeKumaServer} server
|
||||||
|
* @param {string} dir
|
||||||
|
*/
|
||||||
|
constructor(server, dir) {
|
||||||
this.pluginsDir = dir;
|
this.pluginsDir = dir;
|
||||||
|
|
||||||
if (! fs.existsSync(this.pluginsDir)) {
|
if (! fs.existsSync(this.pluginsDir)) {
|
||||||
fs.mkdirSync(this.pluginsDir, { recursive: true });
|
fs.mkdirSync(this.pluginsDir, { recursive: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.debug("plugin", "Scanning plugin directory");
|
||||||
let list = fs.readdirSync(this.pluginsDir);
|
let list = fs.readdirSync(this.pluginsDir);
|
||||||
|
|
||||||
this.pluginList = [];
|
this.pluginList = [];
|
||||||
for (let item of list) {
|
for (let item of list) {
|
||||||
let indexFile = this.pluginsDir + item + "/index.js";
|
let plugin = new Plugin(server, this.pluginsDir + item);
|
||||||
|
|
||||||
if (fs.existsSync(indexFile)) {
|
try {
|
||||||
this.pluginList.push(require(indexFile));
|
plugin.load();
|
||||||
log.debug("plugin", indexFile);
|
this.pluginList.push(plugin);
|
||||||
log.info("plugin", `${item} loaded`);
|
} catch (e) {
|
||||||
|
log.error("plugin", "Failed to load plugin: " + this.pluginsDir + item);
|
||||||
|
log.error("plugin", "Reason: " + e.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Plugin {
|
||||||
|
|
||||||
|
server = undefined;
|
||||||
|
pluginDir = undefined;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Must be an `new-able` class.
|
||||||
|
* @type {function}
|
||||||
|
*/
|
||||||
|
pluginClass = undefined;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @type {*}
|
||||||
|
*/
|
||||||
|
object = undefined;
|
||||||
|
info = {};
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {UptimeKumaServer} server
|
||||||
|
* @param {string} pluginDir
|
||||||
|
*/
|
||||||
|
constructor(server, pluginDir) {
|
||||||
|
this.server = server;
|
||||||
|
this.pluginDir = pluginDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
load() {
|
||||||
|
let indexFile = this.pluginDir + "/index.js";
|
||||||
|
let packageJSON = this.pluginDir + "/package.json";
|
||||||
|
|
||||||
|
if (fs.existsSync(indexFile)) {
|
||||||
|
this.pluginClass = require(path.join(process.cwd(), indexFile));
|
||||||
|
|
||||||
|
let pluginClassType = typeof this.pluginClass;
|
||||||
|
|
||||||
|
if (pluginClassType === "function") {
|
||||||
|
this.object = new this.pluginClass(this.server);
|
||||||
|
} else {
|
||||||
|
throw new Error("Invalid plugin, it does not export a class");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fs.existsSync(packageJSON)) {
|
||||||
|
this.info = require(path.join(process.cwd(), packageJSON));
|
||||||
|
} else {
|
||||||
|
this.info.fullName = this.pluginDir;
|
||||||
|
this.info.name = "[unknown]";
|
||||||
|
this.info.version = "[unknown-version]";
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("plugin", `${this.info.fullName} v${this.info.version} loaded`);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
PluginsManager
|
PluginsManager,
|
||||||
|
Plugin
|
||||||
};
|
};
|
||||||
|
@ -135,7 +135,7 @@ class UptimeKumaServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
loadPlugins(dir) {
|
loadPlugins(dir) {
|
||||||
this.pluginsManager = new PluginsManager(dir);
|
this.pluginsManager = new PluginsManager(this, dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user