hardlounge/src/plugins/packages/themes.js

67 lines
1.4 KiB
JavaScript
Raw Normal View History

2018-01-05 17:40:34 +00:00
"use strict";
const fs = require("fs");
const Helper = require("../../helper");
const path = require("path");
const _ = require("lodash");
const themes = new Map();
module.exports = {
addTheme,
getAll,
getFilename,
loadLocalThemes,
};
function loadLocalThemes() {
fs.readdir(path.join(__dirname, "..", "..", "..", "public", "themes"), (err, builtInThemes) => {
2018-01-05 17:40:34 +00:00
if (err) {
return;
}
2018-01-05 17:40:34 +00:00
builtInThemes
.filter((theme) => theme.endsWith(".css"))
.map(makeLocalThemeObject)
.forEach((theme) => themes.set(theme.name, theme));
});
}
function addTheme(packageName, packageObject) {
const theme = makePackageThemeObject(packageName, packageObject);
2018-01-05 17:40:34 +00:00
if (theme) {
themes.set(theme.name, theme);
}
}
function getAll() {
return _.sortBy(Array.from(themes.values()), "displayName");
}
function getFilename(module) {
if (themes.has(module)) {
return themes.get(module).filename;
}
}
function makeLocalThemeObject(css) {
const themeName = css.slice(0, -4);
return {
displayName: themeName.charAt(0).toUpperCase() + themeName.slice(1),
name: themeName,
};
}
function makePackageThemeObject(moduleName, module) {
if (!module || module.type !== "theme") {
return;
}
2018-01-05 17:40:34 +00:00
const modulePath = Helper.getPackageModulePath(moduleName);
return {
displayName: module.name || moduleName,
filename: path.join(modulePath, module.css),
2018-01-05 17:40:34 +00:00
name: moduleName,
};
}