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 { assert } from "tsafe/assert";
|
|
|
|
import * as fs from "fs";
|
|
|
|
|
|
|
|
export function generateThemeVariations(params: { themeName: string; themeVariantName: string; srcMainResourcesDirPath: string }) {
|
|
|
|
const { themeName, themeVariantName, srcMainResourcesDirPath } = params;
|
|
|
|
|
|
|
|
const mainThemeDirPath = pathJoin(srcMainResourcesDirPath, "theme", themeName);
|
|
|
|
|
|
|
|
transformCodebase({
|
|
|
|
"srcDirPath": mainThemeDirPath,
|
2024-05-13 23:47:28 +02:00
|
|
|
"destDirPath": pathJoin(mainThemeDirPath, "..", themeVariantName),
|
2024-05-12 21:23:20 +02:00
|
|
|
"transformSourceCode": ({ fileRelativePath, sourceCode }) => {
|
|
|
|
if (pathExtname(fileRelativePath) === ".ftl" && fileRelativePath.split(pathSep).length === 2) {
|
|
|
|
const modifiedSourceCode = Buffer.from(
|
|
|
|
Buffer.from(sourceCode)
|
|
|
|
.toString("utf-8")
|
|
|
|
.replace(`out["themeName"] = "${themeName}";`, `out["themeName"] = "${themeVariantName}";`),
|
|
|
|
"utf8"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert(Buffer.compare(modifiedSourceCode, sourceCode) !== 0);
|
|
|
|
|
|
|
|
return { modifiedSourceCode };
|
|
|
|
}
|
|
|
|
|
|
|
|
return { "modifiedSourceCode": sourceCode };
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
{
|
|
|
|
const keycloakThemeJsonFilePath = pathJoin(srcMainResourcesDirPath, "META-INF", "keycloak-themes.json");
|
|
|
|
|
|
|
|
const modifiedParsedJson = JSON.parse(fs.readFileSync(keycloakThemeJsonFilePath).toString("utf8")) as {
|
|
|
|
themes: { name: string; types: string[] }[];
|
|
|
|
};
|
|
|
|
|
|
|
|
modifiedParsedJson.themes.push({
|
|
|
|
"name": themeVariantName,
|
|
|
|
"types": (() => {
|
|
|
|
const theme = modifiedParsedJson.themes.find(({ name }) => name === themeName);
|
|
|
|
assert(theme !== undefined);
|
|
|
|
return theme.types;
|
|
|
|
})()
|
|
|
|
});
|
|
|
|
|
|
|
|
fs.writeFileSync(keycloakThemeJsonFilePath, Buffer.from(JSON.stringify(modifiedParsedJson, null, 2), "utf8"));
|
|
|
|
}
|
2024-05-12 19:16:59 +02:00
|
|
|
}
|