keycloak_theme/src/bin/shared/metaInfKeycloakThemes.ts

85 lines
2.4 KiB
TypeScript
Raw Normal View History

import { join as pathJoin, dirname as pathDirname } from "path";
import type { ThemeType } from "./constants";
import * as fs from "fs";
2024-06-10 07:57:12 +02:00
import { assert } from "tsafe/assert";
import { extractArchive } from "../tools/extractArchive";
export type MetaInfKeycloakTheme = {
themes: { name: string; types: (ThemeType | "email")[] }[];
};
2024-05-20 15:48:51 +02:00
export function getMetaInfKeycloakThemesJsonFilePath(params: {
2024-06-10 07:57:12 +02:00
resourcesDirPath: string;
2024-05-20 15:48:51 +02:00
}) {
2024-06-10 07:57:12 +02:00
const { resourcesDirPath } = params;
2024-05-20 15:48:51 +02:00
return pathJoin(
2024-06-10 07:57:12 +02:00
resourcesDirPath === "." ? "" : resourcesDirPath,
2024-05-20 15:48:51 +02:00
"META-INF",
"keycloak-themes.json"
);
}
2024-06-10 07:57:12 +02:00
export function readMetaInfKeycloakThemes_fromResourcesDirPath(params: {
resourcesDirPath: string;
}) {
const { resourcesDirPath } = params;
2024-05-20 15:48:51 +02:00
return JSON.parse(
fs
.readFileSync(
getMetaInfKeycloakThemesJsonFilePath({
2024-06-10 07:57:12 +02:00
resourcesDirPath
2024-05-20 15:48:51 +02:00
})
)
.toString("utf8")
) as MetaInfKeycloakTheme;
}
2024-06-10 07:57:12 +02:00
export async function readMetaInfKeycloakThemes_fromJar(params: {
jarFilePath: string;
}): Promise<MetaInfKeycloakTheme> {
const { jarFilePath } = params;
let metaInfKeycloakThemes: MetaInfKeycloakTheme | undefined = undefined;
await extractArchive({
archiveFilePath: jarFilePath,
onArchiveFile: async ({ relativeFilePathInArchive, readFile, earlyExit }) => {
if (
relativeFilePathInArchive ===
getMetaInfKeycloakThemesJsonFilePath({ resourcesDirPath: "." })
) {
metaInfKeycloakThemes = JSON.parse((await readFile()).toString("utf8"));
earlyExit();
}
}
});
assert(metaInfKeycloakThemes !== undefined);
return metaInfKeycloakThemes;
}
2024-05-20 15:48:51 +02:00
export function writeMetaInfKeycloakThemes(params: {
2024-06-10 07:57:12 +02:00
resourcesDirPath: string;
2024-05-20 15:48:51 +02:00
metaInfKeycloakThemes: MetaInfKeycloakTheme;
}) {
2024-06-10 07:57:12 +02:00
const { resourcesDirPath, metaInfKeycloakThemes } = params;
2024-05-20 15:48:51 +02:00
const metaInfKeycloakThemesJsonPath = getMetaInfKeycloakThemesJsonFilePath({
2024-06-10 07:57:12 +02:00
resourcesDirPath
2024-05-20 15:48:51 +02:00
});
{
const dirPath = pathDirname(metaInfKeycloakThemesJsonPath);
if (!fs.existsSync(dirPath)) {
2024-05-20 15:48:51 +02:00
fs.mkdirSync(dirPath, { recursive: true });
}
}
2024-05-20 15:48:51 +02:00
fs.writeFileSync(
metaInfKeycloakThemesJsonPath,
Buffer.from(JSON.stringify(metaInfKeycloakThemes, null, 2), "utf8")
);
}