keycloak_theme/src/bin/keycloakify/generateSrcMainResources/generateSrcMainResourcesForThemeVariant.ts

81 lines
2.5 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";
2024-05-20 15:48:51 +02:00
import {
readMetaInfKeycloakThemes,
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>();
2024-05-20 15:48:51 +02:00
export function generateSrcMainResourcesForThemeVariant(params: {
themeName: string;
themeVariantName: string;
2024-06-09 09:15:16 +02:00
buildContext: BuildContextLike;
2024-05-20 15:48:51 +02:00
}) {
2024-06-09 09:15:16 +02:00
const { themeName, themeVariantName, buildContext } = params;
2024-05-20 15:48:51 +02:00
const mainThemeDirPath = pathJoin(
2024-06-09 09:15:16 +02:00
buildContext.keycloakifyBuildDirPath,
2024-05-20 15:48:51 +02:00
"src",
"main",
"resources",
"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(
`out["themeName"] = "${themeName}";`,
`out["themeName"] = "${themeVariantName}";`
),
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
}
});
{
2024-05-20 15:48:51 +02:00
const updatedMetaInfKeycloakThemes = readMetaInfKeycloakThemes({
2024-06-09 09:15:16 +02:00
keycloakifyBuildDirPath: buildContext.keycloakifyBuildDirPath
2024-05-20 15:48:51 +02:00
});
2024-05-12 21:23:20 +02:00
updatedMetaInfKeycloakThemes.themes.push({
2024-05-20 15:48:51 +02:00
name: themeVariantName,
types: (() => {
const theme = updatedMetaInfKeycloakThemes.themes.find(
({ name }) => name === themeName
);
2024-05-12 21:23:20 +02:00
assert(theme !== undefined);
return theme.types;
})()
});
writeMetaInfKeycloakThemes({
2024-06-09 09:15:16 +02:00
keycloakifyBuildDirPath: buildContext.keycloakifyBuildDirPath,
2024-05-20 15:48:51 +02:00
metaInfKeycloakThemes: updatedMetaInfKeycloakThemes
});
2024-05-12 21:23:20 +02:00
}
}