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() {
|
2018-01-12 05:46:55 +00:00
|
|
|
fs.readdir(path.join(__dirname, "..", "..", "..", "public", "themes"), (err, builtInThemes) => {
|
2018-01-05 17:40:34 +00:00
|
|
|
if (err) {
|
|
|
|
return;
|
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
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-02-20 07:28:04 +00:00
|
|
|
|
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-02-20 07:28:04 +00:00
|
|
|
|
2018-01-05 17:40:34 +00:00
|
|
|
const modulePath = Helper.getPackageModulePath(moduleName);
|
|
|
|
return {
|
2018-03-05 00:59:16 +00:00
|
|
|
displayName: module.name || moduleName,
|
|
|
|
filename: path.join(modulePath, module.css),
|
2018-01-05 17:40:34 +00:00
|
|
|
name: moduleName,
|
|
|
|
};
|
|
|
|
}
|