2022-06-19 00:25:21 +00:00
|
|
|
import fs from "fs";
|
|
|
|
import path from "path";
|
|
|
|
import _ from "lodash";
|
2018-01-05 17:40:34 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
import Config from "../../config";
|
|
|
|
import Utils from "../../command-line/utils";
|
2018-01-05 17:40:34 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
type Module = {
|
|
|
|
type?: string;
|
|
|
|
name?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
type ThemeModule = Module & {
|
|
|
|
type: "theme";
|
|
|
|
themeColor: string;
|
|
|
|
css: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type ThemeForClient = {
|
|
|
|
displayName: string;
|
|
|
|
filename?: string;
|
|
|
|
name: string;
|
|
|
|
themeColor: string | null;
|
|
|
|
};
|
|
|
|
|
|
|
|
const themes = new Map<string, ThemeForClient>();
|
|
|
|
|
|
|
|
export default {
|
2018-01-05 17:40:34 +00:00
|
|
|
addTheme,
|
|
|
|
getAll,
|
2019-07-22 16:50:04 +00:00
|
|
|
getByName,
|
2018-01-05 17:40:34 +00:00
|
|
|
loadLocalThemes,
|
|
|
|
};
|
|
|
|
|
|
|
|
function loadLocalThemes() {
|
2022-06-19 00:25:21 +00:00
|
|
|
const builtInThemes = fs.readdirSync(Utils.getFileFromRelativeToRoot("public", "themes"));
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2019-07-22 16:50:04 +00:00
|
|
|
builtInThemes
|
|
|
|
.filter((theme) => theme.endsWith(".css"))
|
|
|
|
.map(makeLocalThemeObject)
|
|
|
|
.forEach((theme) => themes.set(theme.name, theme));
|
2018-01-05 17:40:34 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
function addTheme(packageName: string, packageObject: ThemeModule) {
|
2018-01-05 17:40:34 +00:00
|
|
|
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() {
|
2022-06-19 00:25:21 +00:00
|
|
|
const filteredThemes: ThemeForClient[] = [];
|
2018-01-05 17:40:34 +00:00
|
|
|
|
2019-07-22 16:50:04 +00:00
|
|
|
for (const theme of themes.values()) {
|
|
|
|
filteredThemes.push(_.pick(theme, ["displayName", "name", "themeColor"]));
|
2018-01-05 17:40:34 +00:00
|
|
|
}
|
2019-07-22 16:50:04 +00:00
|
|
|
|
|
|
|
return _.sortBy(filteredThemes, "displayName");
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
function getByName(name: string) {
|
2019-07-22 16:50:04 +00:00
|
|
|
return themes.get(name);
|
2018-01-05 17:40:34 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
function makeLocalThemeObject(css: string) {
|
2018-01-05 17:40:34 +00:00
|
|
|
const themeName = css.slice(0, -4);
|
|
|
|
return {
|
|
|
|
displayName: themeName.charAt(0).toUpperCase() + themeName.slice(1),
|
|
|
|
name: themeName,
|
2019-07-22 16:50:04 +00:00
|
|
|
themeColor: null,
|
2018-01-05 17:40:34 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
function makePackageThemeObject(
|
|
|
|
moduleName: string,
|
|
|
|
module: ThemeModule
|
|
|
|
): ThemeForClient | undefined {
|
2018-01-05 17:40:34 +00:00
|
|
|
if (!module || module.type !== "theme") {
|
|
|
|
return;
|
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2019-07-22 16:50:04 +00:00
|
|
|
const themeColor = /^#[0-9A-F]{6}$/i.test(module.themeColor) ? module.themeColor : null;
|
2022-05-01 19:12:39 +00:00
|
|
|
const modulePath = Config.getPackageModulePath(moduleName);
|
2018-01-05 17:40:34 +00:00
|
|
|
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,
|
2019-07-22 16:50:04 +00:00
|
|
|
themeColor: themeColor,
|
2018-01-05 17:40:34 +00:00
|
|
|
};
|
|
|
|
}
|