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

75 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";
2024-05-20 15:48:51 +02:00
import {
2024-06-10 07:57:12 +02:00
readMetaInfKeycloakThemes_fromResourcesDirPath,
2024-05-20 15:48:51 +02:00
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: {
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(
`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-06-10 07:57:12 +02:00
const updatedMetaInfKeycloakThemes =
readMetaInfKeycloakThemes_fromResourcesDirPath({
resourcesDirPath
});
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-10 07:57:12 +02:00
resourcesDirPath,
2024-05-20 15:48:51 +02:00
metaInfKeycloakThemes: updatedMetaInfKeycloakThemes
});
2024-05-12 21:23:20 +02:00
}
}