thelounge/src/plugins/packages/index.js

96 lines
2.2 KiB
JavaScript
Raw Normal View History

2018-01-05 18:40:34 +01:00
"use strict";
2018-06-15 22:31:06 +02:00
const log = require("../../log");
2018-03-02 19:28:54 +01:00
const colors = require("chalk");
const path = require("path");
2018-01-05 18:40:34 +01:00
const Helper = require("../../helper");
const themes = require("./themes");
const packageMap = new Map();
2019-07-02 18:02:02 +02:00
const inputs = require("../inputs");
2018-01-05 18:40:34 +01:00
const stylesheets = [];
module.exports = {
getStylesheets,
getPackage,
loadPackages,
};
2019-07-02 18:02:02 +02:00
const packageApis = function(clientManager, packageName) {
2018-01-05 18:40:34 +01:00
return {
Stylesheets: {
addFile: addStylesheet.bind(this, packageName),
},
2019-07-02 18:02:02 +02:00
Commands: {
add: (command, func) => inputs.userInputs[command] = func,
runAsUser: (line, userName, target) => clientManager.findClient(userName).inputLine({target, text: line}),
},
2018-03-12 10:46:28 +01:00
Config: {
getConfig: () => Helper.config,
},
2018-01-05 18:40:34 +01:00
};
};
function addStylesheet(packageName, filename) {
stylesheets.push(packageName + "/" + filename);
}
function getStylesheets() {
return stylesheets;
}
function getPackage(name) {
return packageMap.get(name);
}
2019-07-02 18:02:02 +02:00
function loadPackages(clientManager) {
const packageJson = path.join(Helper.getPackagesPath(), "package.json");
let packages;
2019-07-05 09:26:22 +02:00
let anyPlugins = false;
2018-02-05 07:30:57 +01:00
try {
packages = Object.keys(require(packageJson).dependencies);
} catch (e) {
packages = [];
}
2018-02-05 07:30:57 +01:00
packages.forEach((packageName) => {
const errorMsg = `Package ${colors.bold(packageName)} could not be loaded`;
let packageInfo;
let packageFile;
2018-02-05 07:30:57 +01:00
try {
packageInfo = require(path.join(Helper.getPackageModulePath(packageName), "package.json"));
packageFile = require(Helper.getPackageModulePath(packageName));
} catch (e) {
log.warn(errorMsg);
return;
}
2018-02-05 07:30:57 +01:00
if (!packageInfo.thelounge) {
log.warn(errorMsg);
return;
}
2018-02-05 07:30:57 +01:00
packageInfo = packageInfo.thelounge;
packageMap.set(packageName, packageFile);
2018-01-05 18:40:34 +01:00
if (packageInfo.type === "theme") {
themes.addTheme(packageName, packageInfo);
2019-07-05 09:26:22 +02:00
} else {
anyPlugins = true;
}
2018-02-05 07:30:57 +01:00
if (packageFile.onServerStart) {
2019-07-02 18:02:02 +02:00
packageFile.onServerStart(packageApis(clientManager, packageName));
}
2018-02-05 07:30:57 +01:00
log.info(`Package ${colors.bold(packageName)} loaded`);
});
2019-07-05 09:26:22 +02:00
if (anyPlugins) {
log.info("There are packages using the experimental plugin API. Be aware that this API is not yet stable and may change in future The Lounge releases.");
}
2018-01-05 18:40:34 +01:00
}