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-05-17 00:39:05 +02:00
|
|
|
import type { BuildOptions } from "../../shared/buildOptions";
|
2024-05-12 21:23:20 +02:00
|
|
|
import { assert } from "tsafe/assert";
|
|
|
|
import * as fs from "fs";
|
|
|
|
|
2024-05-17 00:39:05 +02:00
|
|
|
export type BuildOptionsLike = {
|
|
|
|
keycloakifyBuildDirPath: string;
|
|
|
|
};
|
2024-05-12 21:23:20 +02:00
|
|
|
|
2024-05-17 00:39:05 +02:00
|
|
|
assert<BuildOptions extends BuildOptionsLike ? true : false>();
|
|
|
|
|
2024-05-17 01:21:26 +02:00
|
|
|
export function generateSrcMainResourcesForThemeVariant(params: { themeName: string; themeVariantName: string; buildOptions: BuildOptionsLike }) {
|
2024-05-17 00:39:05 +02:00
|
|
|
const { themeName, themeVariantName, buildOptions } = params;
|
|
|
|
|
|
|
|
const mainThemeDirPath = pathJoin(buildOptions.keycloakifyBuildDirPath, "src", "main", "resources", "theme", themeName);
|
2024-05-12 21:23:20 +02:00
|
|
|
|
|
|
|
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 };
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
{
|
2024-05-17 00:39:05 +02:00
|
|
|
const keycloakThemeJsonFilePath = pathJoin(
|
|
|
|
buildOptions.keycloakifyBuildDirPath,
|
|
|
|
"src",
|
|
|
|
"main",
|
|
|
|
"resources",
|
|
|
|
"META-INF",
|
|
|
|
"keycloak-themes.json"
|
|
|
|
);
|
2024-05-12 21:23:20 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|