hardlounge/src/plugins/packages/index.js

89 lines
1.9 KiB
JavaScript
Raw Normal View History

2018-01-05 17:40:34 +00:00
"use strict";
2018-06-15 20:31:06 +00:00
const log = require("../../log");
2018-03-02 18:28:54 +00:00
const colors = require("chalk");
const path = require("path");
2018-01-05 17:40:34 +00:00
const Helper = require("../../helper");
const themes = require("./themes");
const packageMap = new Map();
2019-07-02 16:02:02 +00:00
const inputs = require("../inputs");
2018-01-05 17:40:34 +00:00
const stylesheets = [];
module.exports = {
getStylesheets,
getPackage,
loadPackages,
};
2019-07-02 16:02:02 +00:00
const packageApis = function(clientManager, packageName) {
2018-01-05 17:40:34 +00:00
return {
Stylesheets: {
addFile: addStylesheet.bind(this, packageName),
},
2019-07-02 16:02:02 +00:00
Commands: {
add: (command, func) => inputs.userInputs[command] = func,
runAsUser: (line, userName, target) => clientManager.findClient(userName).inputLine({target, text: line}),
},
2018-03-12 09:46:28 +00:00
Config: {
getConfig: () => Helper.config,
},
2018-01-05 17:40:34 +00:00
};
};
function addStylesheet(packageName, filename) {
stylesheets.push(packageName + "/" + filename);
}
function getStylesheets() {
return stylesheets;
}
function getPackage(name) {
return packageMap.get(name);
}
2019-07-02 16:02:02 +00:00
function loadPackages(clientManager) {
const packageJson = path.join(Helper.getPackagesPath(), "package.json");
let packages;
2018-02-05 06:30:57 +00:00
try {
packages = Object.keys(require(packageJson).dependencies);
} catch (e) {
packages = [];
}
2018-02-05 06:30:57 +00:00
packages.forEach((packageName) => {
const errorMsg = `Package ${colors.bold(packageName)} could not be loaded`;
let packageInfo;
let packageFile;
2018-02-05 06:30:57 +00: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 06:30:57 +00:00
if (!packageInfo.thelounge) {
log.warn(errorMsg);
return;
}
2018-02-05 06:30:57 +00:00
packageInfo = packageInfo.thelounge;
packageMap.set(packageName, packageFile);
2018-01-05 17:40:34 +00:00
if (packageInfo.type === "theme") {
themes.addTheme(packageName, packageInfo);
}
2018-02-05 06:30:57 +00:00
if (packageFile.onServerStart) {
2019-07-02 16:02:02 +00:00
packageFile.onServerStart(packageApis(clientManager, packageName));
}
2018-02-05 06:30:57 +00:00
log.info(`Package ${colors.bold(packageName)} loaded`);
});
2018-01-05 17:40:34 +00:00
}