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

71 lines
2.4 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";
2024-06-09 09:15:16 +02:00
import type { BuildContext } from "../../shared/buildContext";
import { writeMetaInfKeycloakThemes } from "../../shared/metaInfKeycloakThemes";
2024-05-12 21:23:20 +02:00
import { assert } from "tsafe/assert";
2024-06-09 09:15:16 +02:00
export type BuildContextLike = {
keycloakifyBuildDirPath: string;
};
2024-05-12 21:23:20 +02:00
2024-06-09 09:15:16 +02:00
assert<BuildContext extends BuildContextLike ? true : false>();
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;
}) {
2024-06-10 07:57:12 +02:00
const { resourcesDirPath, themeName, themeVariantName } = params;
2024-06-10 07:57:12 +02:00
const mainThemeDirPath = pathJoin(resourcesDirPath, "theme", themeName);
2024-05-12 21:23:20 +02:00
transformCodebase({
2024-05-20 15:48:51 +02:00
srcDirPath: mainThemeDirPath,
destDirPath: pathJoin(mainThemeDirPath, "..", themeVariantName),
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;
}
});
}