hardlounge/src/plugins/packages/index.js

96 lines
2.1 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,
};
const packageApis = function(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: inputs.addPluginCommand,
runAsUser: (command, targetId, client) => client.inputLine({target: targetId, text: command}),
2019-07-02 16:02:02 +00:00
},
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);
}
function loadPackages() {
const packageJson = path.join(Helper.getPackagesPath(), "package.json");
let packages;
2019-07-05 07:26:22 +00:00
let anyPlugins = false;
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);
2019-07-05 07:26:22 +00:00
} else {
anyPlugins = true;
}
2018-02-05 06:30:57 +00:00
if (packageFile.onServerStart) {
packageFile.onServerStart(packageApis(packageName));
}
2018-02-05 06:30:57 +00:00
log.info(`Package ${colors.bold(packageName)} loaded`);
});
2019-07-05 07:26:22 +00: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 17:40:34 +00:00
}