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-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-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-20 15:48:51 +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;
|
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
const mainThemeDirPath = pathJoin(
|
|
|
|
buildOptions.keycloakifyBuildDirPath,
|
|
|
|
"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({
|
|
|
|
keycloakifyBuildDirPath: buildOptions.keycloakifyBuildDirPath
|
|
|
|
});
|
2024-05-12 21:23:20 +02:00
|
|
|
|
2024-05-17 02:05:14 +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;
|
|
|
|
})()
|
|
|
|
});
|
|
|
|
|
2024-05-17 02:05:14 +02:00
|
|
|
writeMetaInfKeycloakThemes({
|
2024-05-20 15:48:51 +02:00
|
|
|
keycloakifyBuildDirPath: buildOptions.keycloakifyBuildDirPath,
|
|
|
|
metaInfKeycloakThemes: updatedMetaInfKeycloakThemes
|
2024-05-17 02:05:14 +02:00
|
|
|
});
|
2024-05-12 21:23:20 +02:00
|
|
|
}
|
2024-05-12 19:16:59 +02:00
|
|
|
}
|