thelounge/src/plugins/packages/index.js

79 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-01-05 18:40:34 +01:00
"use strict";
const fs = require("fs");
const colors = require("colors/safe");
const Helper = require("../../helper");
const themes = require("./themes");
const packageMap = new Map();
const stylesheets = [];
module.exports = {
getStylesheets,
getPackage,
loadPackages,
};
const packageApis = function(packageName) {
return {
Stylesheets: {
addFile: addStylesheet.bind(this, packageName),
},
};
};
function addStylesheet(packageName, filename) {
stylesheets.push(packageName + "/" + filename);
}
function getStylesheets() {
return stylesheets;
}
function getPackage(name) {
return packageMap.get(name);
}
function loadPackages() {
fs.readdir(Helper.getPackagesPath(), (err, packages) => {
if (err) {
return;
}
2018-02-05 07:30:57 +01:00
2018-01-05 18:40:34 +01:00
packages.forEach((packageName) => {
2018-02-05 07:30:57 +01:00
fs.stat(Helper.getPackageModulePath(packageName), function(err2, stat) {
if (err2 || !stat.isDirectory()) {
return;
}
const packageFile = getModuleInfo(packageName);
if (!packageFile) {
return;
}
packageMap.set(packageName, packageFile);
if (packageFile.type === "theme") {
themes.addTheme(packageName, packageFile);
}
if (packageFile.onServerStart) {
packageFile.onServerStart(packageApis(packageName));
}
});
2018-01-05 18:40:34 +01:00
});
});
}
function getModuleInfo(packageName) {
2018-02-05 07:30:57 +01:00
const module = require(Helper.getPackageModulePath(packageName));
2018-01-05 18:40:34 +01:00
if (!module.thelounge) {
log.warn(`Specified package ${colors.yellow(packageName)} doesn't have required information.`);
return;
}
2018-02-05 07:30:57 +01:00
2018-01-05 18:40:34 +01:00
return module.thelounge;
}