thelounge/src/plugins/packages/themes.js

72 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-01-05 18:40:34 +01:00
"use strict";
const fs = require("fs");
const Config = require("../../config");
2018-01-05 18:40:34 +01:00
const path = require("path");
const _ = require("lodash");
const themes = new Map();
module.exports = {
addTheme,
getAll,
2019-07-22 18:50:04 +02:00
getByName,
2018-01-05 18:40:34 +01:00
loadLocalThemes,
};
function loadLocalThemes() {
2019-07-22 18:50:04 +02:00
const builtInThemes = fs.readdirSync(
path.join(__dirname, "..", "..", "..", "public", "themes")
);
2019-07-22 18:50:04 +02:00
builtInThemes
.filter((theme) => theme.endsWith(".css"))
.map(makeLocalThemeObject)
.forEach((theme) => themes.set(theme.name, theme));
2018-01-05 18:40:34 +01:00
}
function addTheme(packageName, packageObject) {
const theme = makePackageThemeObject(packageName, packageObject);
2018-01-05 18:40:34 +01:00
if (theme) {
themes.set(theme.name, theme);
}
}
function getAll() {
2019-07-22 18:50:04 +02:00
const filteredThemes = [];
2018-01-05 18:40:34 +01:00
2019-07-22 18:50:04 +02:00
for (const theme of themes.values()) {
filteredThemes.push(_.pick(theme, ["displayName", "name", "themeColor"]));
2018-01-05 18:40:34 +01:00
}
2019-07-22 18:50:04 +02:00
return _.sortBy(filteredThemes, "displayName");
}
function getByName(name) {
return themes.get(name);
2018-01-05 18:40:34 +01:00
}
function makeLocalThemeObject(css) {
const themeName = css.slice(0, -4);
return {
displayName: themeName.charAt(0).toUpperCase() + themeName.slice(1),
name: themeName,
2019-07-22 18:50:04 +02:00
themeColor: null,
2018-01-05 18:40:34 +01:00
};
}
function makePackageThemeObject(moduleName, module) {
if (!module || module.type !== "theme") {
return;
}
2019-07-22 18:50:04 +02:00
const themeColor = /^#[0-9A-F]{6}$/i.test(module.themeColor) ? module.themeColor : null;
const modulePath = Config.getPackageModulePath(moduleName);
2018-01-05 18:40:34 +01:00
return {
displayName: module.name || moduleName,
filename: path.join(modulePath, module.css),
2018-01-05 18:40:34 +01:00
name: moduleName,
2019-07-22 18:50:04 +02:00
themeColor: themeColor,
2018-01-05 18:40:34 +01:00
};
}