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-15 14:23:35 +02:00
|
|
|
import { writeMetaInfKeycloakThemes } from "../../shared/metaInfKeycloakThemes";
|
2024-05-12 21:23:20 +02:00
|
|
|
import { assert } from "tsafe/assert";
|
2024-09-22 04:39:24 +02:00
|
|
|
import type { ThemeType } from "../../shared/constants";
|
2024-05-17 00:39:05 +02:00
|
|
|
|
2024-06-12 14:48:08 +02:00
|
|
|
export function generateResourcesForThemeVariant(params: {
|
2024-06-10 07:57:12 +02:00
|
|
|
resourcesDirPath: string;
|
2024-05-20 15:48:51 +02:00
|
|
|
themeName: string;
|
|
|
|
themeVariantName: string;
|
2024-09-22 04:39:24 +02:00
|
|
|
writeMessagePropertiesFiles: (params: {
|
|
|
|
getMessageDirPath: (params: { themeType: ThemeType }) => string;
|
|
|
|
themeName: string;
|
|
|
|
}) => void;
|
2024-05-20 15:48:51 +02:00
|
|
|
}) {
|
2024-09-22 04:39:24 +02:00
|
|
|
const { resourcesDirPath, themeName, themeVariantName, writeMessagePropertiesFiles } =
|
|
|
|
params;
|
2024-05-17 00:39:05 +02:00
|
|
|
|
2024-06-10 07:57:12 +02:00
|
|
|
const mainThemeDirPath = pathJoin(resourcesDirPath, "theme", themeName);
|
2024-09-22 04:39:24 +02:00
|
|
|
const themeVariantDirPath = pathJoin(mainThemeDirPath, "..", themeVariantName);
|
2024-05-12 21:23:20 +02:00
|
|
|
|
|
|
|
transformCodebase({
|
2024-05-20 15:48:51 +02:00
|
|
|
srcDirPath: mainThemeDirPath,
|
2024-09-22 04:39:24 +02:00
|
|
|
destDirPath: themeVariantDirPath,
|
2024-05-20 15:48:51 +02:00
|
|
|
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(
|
2024-07-13 18:17:21 +02:00
|
|
|
`"themeName": "${themeName}"`,
|
|
|
|
`"themeName": "${themeVariantName}"`
|
2024-05-20 15:48:51 +02:00
|
|
|
),
|
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-15 14:23:35 +02:00
|
|
|
writeMetaInfKeycloakThemes({
|
|
|
|
resourcesDirPath,
|
|
|
|
getNewMetaInfKeycloakTheme: ({ metaInfKeycloakTheme }) => {
|
|
|
|
assert(metaInfKeycloakTheme !== undefined);
|
2024-05-12 21:23:20 +02:00
|
|
|
|
2024-06-15 14:23:35 +02:00
|
|
|
const newMetaInfKeycloakTheme = metaInfKeycloakTheme;
|
|
|
|
|
|
|
|
newMetaInfKeycloakTheme.themes.push({
|
|
|
|
name: themeVariantName,
|
|
|
|
types: (() => {
|
|
|
|
const theme = newMetaInfKeycloakTheme.themes.find(
|
|
|
|
({ name }) => name === themeName
|
|
|
|
);
|
|
|
|
assert(theme !== undefined);
|
|
|
|
return theme.types;
|
|
|
|
})()
|
|
|
|
});
|
2024-05-12 21:23:20 +02:00
|
|
|
|
2024-06-15 14:23:35 +02:00
|
|
|
return newMetaInfKeycloakTheme;
|
|
|
|
}
|
|
|
|
});
|
2024-09-22 04:39:24 +02:00
|
|
|
|
|
|
|
writeMessagePropertiesFiles({
|
|
|
|
getMessageDirPath: ({ themeType }) =>
|
|
|
|
pathJoin(themeVariantDirPath, themeType, "messages"),
|
|
|
|
themeName: themeVariantName
|
|
|
|
});
|
2024-05-12 19:16:59 +02:00
|
|
|
}
|