keycloak_theme/src/bin/keycloakify/generateResources/generateResourcesForThemeVariant.ts

77 lines
2.7 KiB
TypeScript
Raw Normal View History

2024-05-12 21:23:20 +02:00
import { join as pathJoin, extname as pathExtname, sep as pathSep } from "path";
import { transformCodebase } from "../../tools/transformCodebase";
import { writeMetaInfKeycloakThemes } from "../../shared/metaInfKeycloakThemes";
2024-05-12 21:23:20 +02:00
import { assert } from "tsafe/assert";
import type { ThemeType } from "../../shared/constants";
export function generateResourcesForThemeVariant(params: {
2024-06-10 07:57:12 +02:00
resourcesDirPath: string;
2024-05-20 15:48:51 +02:00
themeName: string;
themeVariantName: string;
writeMessagePropertiesFiles: (params: {
getMessageDirPath: (params: { themeType: ThemeType }) => string;
themeName: string;
}) => void;
2024-05-20 15:48:51 +02:00
}) {
const { resourcesDirPath, themeName, themeVariantName, writeMessagePropertiesFiles } =
params;
2024-06-10 07:57:12 +02:00
const mainThemeDirPath = pathJoin(resourcesDirPath, "theme", themeName);
const themeVariantDirPath = pathJoin(mainThemeDirPath, "..", themeVariantName);
2024-05-12 21:23:20 +02:00
transformCodebase({
2024-05-20 15:48:51 +02:00
srcDirPath: mainThemeDirPath,
destDirPath: themeVariantDirPath,
2024-05-20 15:48:51 +02:00
transformSourceCode: ({ fileRelativePath, sourceCode }) => {
if (
pathExtname(fileRelativePath) === ".ftl" &&
fileRelativePath.split(pathSep).length === 2
) {
2024-05-12 21:23:20 +02:00
const modifiedSourceCode = Buffer.from(
Buffer.from(sourceCode)
.toString("utf-8")
2024-05-20 15:48:51 +02:00
.replace(
2024-07-13 18:17:21 +02:00
`"themeName": "${themeName}"`,
`"themeName": "${themeVariantName}"`
2024-05-20 15:48:51 +02:00
),
2024-05-12 21:23:20 +02:00
"utf8"
);
assert(Buffer.compare(modifiedSourceCode, sourceCode) !== 0);
return { modifiedSourceCode };
}
2024-05-20 15:48:51 +02:00
return { modifiedSourceCode: sourceCode };
2024-05-12 21:23:20 +02:00
}
});
writeMetaInfKeycloakThemes({
resourcesDirPath,
getNewMetaInfKeycloakTheme: ({ metaInfKeycloakTheme }) => {
assert(metaInfKeycloakTheme !== undefined);
2024-05-12 21:23:20 +02:00
const newMetaInfKeycloakTheme = metaInfKeycloakTheme;
newMetaInfKeycloakTheme.themes.push({
name: themeVariantName,
types: (() => {
const theme = newMetaInfKeycloakTheme.themes.find(
({ name }) => name === themeName
);
assert(theme !== undefined);
return theme.types;
})()
});
2024-05-12 21:23:20 +02:00
return newMetaInfKeycloakTheme;
}
});
writeMessagePropertiesFiles({
getMessageDirPath: ({ themeType }) =>
pathJoin(themeVariantDirPath, themeType, "messages"),
themeName: themeVariantName
});
}