2024-05-17 02:05:14 +02:00
|
|
|
import { join as pathJoin, dirname as pathDirname } from "path";
|
|
|
|
import type { ThemeType } from "./constants";
|
|
|
|
import * as fs from "fs";
|
|
|
|
|
|
|
|
export type MetaInfKeycloakTheme = {
|
|
|
|
themes: { name: string; types: (ThemeType | "email")[] }[];
|
|
|
|
};
|
|
|
|
|
2024-06-15 14:23:35 +02:00
|
|
|
export function writeMetaInfKeycloakThemes(params: {
|
2024-06-10 07:57:12 +02:00
|
|
|
resourcesDirPath: string;
|
2024-06-15 14:23:35 +02:00
|
|
|
getNewMetaInfKeycloakTheme: (params: {
|
|
|
|
metaInfKeycloakTheme: MetaInfKeycloakTheme | undefined;
|
|
|
|
}) => MetaInfKeycloakTheme;
|
2024-06-10 07:57:12 +02:00
|
|
|
}) {
|
2024-06-15 14:23:35 +02:00
|
|
|
const { resourcesDirPath, getNewMetaInfKeycloakTheme } = params;
|
2024-05-17 02:05:14 +02:00
|
|
|
|
2024-06-15 14:23:35 +02:00
|
|
|
const filePath = pathJoin(resourcesDirPath, "META-INF", "keycloak-themes.json");
|
2024-06-10 07:57:12 +02:00
|
|
|
|
2024-06-15 14:23:35 +02:00
|
|
|
const currentMetaInfKeycloakTheme = !fs.existsSync(filePath)
|
|
|
|
? undefined
|
|
|
|
: (JSON.parse(
|
|
|
|
fs.readFileSync(filePath).toString("utf8")
|
|
|
|
) as MetaInfKeycloakTheme);
|
2024-05-17 02:05:14 +02:00
|
|
|
|
2024-06-15 14:23:35 +02:00
|
|
|
const newMetaInfKeycloakThemes = getNewMetaInfKeycloakTheme({
|
|
|
|
metaInfKeycloakTheme: currentMetaInfKeycloakTheme
|
2024-05-20 15:48:51 +02:00
|
|
|
});
|
2024-05-17 02:05:14 +02:00
|
|
|
|
|
|
|
{
|
2024-06-15 14:23:35 +02:00
|
|
|
const dirPath = pathDirname(filePath);
|
2024-05-17 02:05:14 +02:00
|
|
|
if (!fs.existsSync(dirPath)) {
|
2024-05-20 15:48:51 +02:00
|
|
|
fs.mkdirSync(dirPath, { recursive: true });
|
2024-05-17 02:05:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
fs.writeFileSync(
|
2024-06-15 14:23:35 +02:00
|
|
|
filePath,
|
|
|
|
Buffer.from(JSON.stringify(newMetaInfKeycloakThemes, null, 2), "utf8")
|
2024-05-20 15:48:51 +02:00
|
|
|
);
|
2024-05-17 02:05:14 +02:00
|
|
|
}
|