2024-05-12 20:47:03 +02:00
|
|
|
import { transformCodebase } from "../../tools/transformCodebase";
|
|
|
|
import * as fs from "fs";
|
2024-06-19 01:41:22 +02:00
|
|
|
import {
|
|
|
|
join as pathJoin,
|
|
|
|
resolve as pathResolve,
|
|
|
|
relative as pathRelative,
|
|
|
|
dirname as pathDirname
|
|
|
|
} from "path";
|
2024-05-12 20:47:03 +02:00
|
|
|
import { replaceImportsInJsCode } from "../replacers/replaceImportsInJsCode";
|
|
|
|
import { replaceImportsInCssCode } from "../replacers/replaceImportsInCssCode";
|
2024-06-06 01:31:00 +02:00
|
|
|
import {
|
|
|
|
generateFtlFilesCodeFactory,
|
2024-06-09 09:15:16 +02:00
|
|
|
type BuildContextLike as BuildContextLike_kcContextExclusionsFtlCode
|
2024-06-06 01:31:00 +02:00
|
|
|
} from "../generateFtl";
|
2024-05-12 20:47:03 +02:00
|
|
|
import {
|
|
|
|
type ThemeType,
|
|
|
|
lastKeycloakVersionWithAccountV1,
|
|
|
|
keycloak_resources,
|
|
|
|
accountV1ThemeName,
|
2024-05-19 04:02:36 +02:00
|
|
|
basenameOfTheKeycloakifyResourcesDir,
|
|
|
|
loginThemePageIds,
|
|
|
|
accountThemePageIds
|
2024-05-15 05:14:01 +02:00
|
|
|
} from "../../shared/constants";
|
2024-06-09 09:15:16 +02:00
|
|
|
import type { BuildContext } from "../../shared/buildContext";
|
2024-05-12 20:47:03 +02:00
|
|
|
import { assert, type Equals } from "tsafe/assert";
|
2024-06-06 01:31:00 +02:00
|
|
|
import {
|
|
|
|
downloadKeycloakStaticResources,
|
2024-06-09 09:15:16 +02:00
|
|
|
type BuildContextLike as BuildContextLike_downloadKeycloakStaticResources
|
2024-06-06 01:31:00 +02:00
|
|
|
} from "../../shared/downloadKeycloakStaticResources";
|
2024-05-12 20:47:03 +02:00
|
|
|
import { readFieldNameUsage } from "./readFieldNameUsage";
|
|
|
|
import { readExtraPagesNames } from "./readExtraPageNames";
|
|
|
|
import { generateMessageProperties } from "./generateMessageProperties";
|
2024-06-06 01:31:00 +02:00
|
|
|
import {
|
|
|
|
bringInAccountV1,
|
2024-06-09 09:15:16 +02:00
|
|
|
type BuildContextLike as BuildContextLike_bringInAccountV1
|
2024-06-06 01:31:00 +02:00
|
|
|
} from "./bringInAccountV1";
|
2024-05-12 20:47:03 +02:00
|
|
|
import { rmSync } from "../../tools/fs.rmSync";
|
2024-05-18 10:24:55 +02:00
|
|
|
import { readThisNpmPackageVersion } from "../../tools/readThisNpmPackageVersion";
|
2024-05-20 15:48:51 +02:00
|
|
|
import {
|
|
|
|
writeMetaInfKeycloakThemes,
|
|
|
|
type MetaInfKeycloakTheme
|
|
|
|
} from "../../shared/metaInfKeycloakThemes";
|
2024-05-17 02:05:14 +02:00
|
|
|
import { objectEntries } from "tsafe/objectEntries";
|
2024-06-08 14:02:07 +02:00
|
|
|
import { escapeStringForPropertiesFile } from "../../tools/escapeStringForPropertiesFile";
|
2024-05-12 20:47:03 +02:00
|
|
|
|
2024-06-09 09:15:16 +02:00
|
|
|
export type BuildContextLike = BuildContextLike_kcContextExclusionsFtlCode &
|
|
|
|
BuildContextLike_downloadKeycloakStaticResources &
|
|
|
|
BuildContextLike_bringInAccountV1 & {
|
2024-06-06 01:31:00 +02:00
|
|
|
extraThemeProperties: string[] | undefined;
|
|
|
|
loginThemeResourcesFromKeycloakVersion: string;
|
2024-06-09 09:03:43 +02:00
|
|
|
projectDirPath: string;
|
2024-06-10 07:57:12 +02:00
|
|
|
projectBuildDirPath: string;
|
2024-06-08 14:02:07 +02:00
|
|
|
environmentVariables: { name: string; default: string }[];
|
2024-06-16 01:29:15 +02:00
|
|
|
recordIsImplementedByThemeType: BuildContext["recordIsImplementedByThemeType"];
|
|
|
|
themeSrcDirPath: string;
|
2024-06-06 01:31:00 +02:00
|
|
|
};
|
2024-05-12 20:47:03 +02:00
|
|
|
|
2024-06-09 09:15:16 +02:00
|
|
|
assert<BuildContext extends BuildContextLike ? true : false>();
|
2024-05-12 20:47:03 +02:00
|
|
|
|
2024-06-12 14:48:08 +02:00
|
|
|
export async function generateResourcesForMainTheme(params: {
|
2024-05-20 15:48:51 +02:00
|
|
|
themeName: string;
|
2024-06-10 07:57:12 +02:00
|
|
|
resourcesDirPath: string;
|
2024-06-09 09:15:16 +02:00
|
|
|
buildContext: BuildContextLike;
|
2024-05-20 15:48:51 +02:00
|
|
|
}): Promise<void> {
|
2024-06-10 07:57:12 +02:00
|
|
|
const { themeName, resourcesDirPath, buildContext } = params;
|
2024-05-16 09:35:31 +02:00
|
|
|
|
2024-05-12 20:47:03 +02:00
|
|
|
const getThemeTypeDirPath = (params: { themeType: ThemeType | "email" }) => {
|
|
|
|
const { themeType } = params;
|
2024-06-10 07:57:12 +02:00
|
|
|
return pathJoin(resourcesDirPath, "theme", themeName, themeType);
|
2024-05-12 20:47:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
for (const themeType of ["login", "account"] as const) {
|
2024-06-16 01:29:15 +02:00
|
|
|
if (!buildContext.recordIsImplementedByThemeType[themeType]) {
|
2024-05-12 20:47:03 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const themeTypeDirPath = getThemeTypeDirPath({ themeType });
|
|
|
|
|
|
|
|
apply_replacers_and_move_to_theme_resources: {
|
2024-05-20 15:48:51 +02:00
|
|
|
const destDirPath = pathJoin(
|
|
|
|
themeTypeDirPath,
|
|
|
|
"resources",
|
|
|
|
basenameOfTheKeycloakifyResourcesDir
|
|
|
|
);
|
2024-05-12 20:47:03 +02:00
|
|
|
|
|
|
|
// NOTE: Prevent accumulation of files in the assets dir, as names are hashed they pile up.
|
2024-05-20 15:48:51 +02:00
|
|
|
rmSync(destDirPath, { recursive: true, force: true });
|
2024-05-12 20:47:03 +02:00
|
|
|
|
2024-06-16 01:29:15 +02:00
|
|
|
if (
|
|
|
|
themeType === "account" &&
|
|
|
|
buildContext.recordIsImplementedByThemeType.login
|
|
|
|
) {
|
2024-05-16 09:20:37 +02:00
|
|
|
// NOTE: We prevent doing it twice, it has been done for the login theme.
|
2024-05-12 20:47:03 +02:00
|
|
|
|
|
|
|
transformCodebase({
|
2024-05-20 15:48:51 +02:00
|
|
|
srcDirPath: pathJoin(
|
2024-05-12 20:47:03 +02:00
|
|
|
getThemeTypeDirPath({
|
2024-05-20 15:48:51 +02:00
|
|
|
themeType: "login"
|
2024-05-12 20:47:03 +02:00
|
|
|
}),
|
|
|
|
"resources",
|
|
|
|
basenameOfTheKeycloakifyResourcesDir
|
|
|
|
),
|
|
|
|
destDirPath
|
|
|
|
});
|
|
|
|
|
|
|
|
break apply_replacers_and_move_to_theme_resources;
|
|
|
|
}
|
|
|
|
|
2024-06-15 14:23:35 +02:00
|
|
|
{
|
|
|
|
const dirPath = pathJoin(
|
|
|
|
buildContext.projectBuildDirPath,
|
|
|
|
keycloak_resources
|
|
|
|
);
|
|
|
|
|
|
|
|
if (fs.existsSync(dirPath)) {
|
|
|
|
assert(buildContext.bundler === "webpack");
|
|
|
|
|
|
|
|
throw new Error(
|
|
|
|
[
|
|
|
|
`Keycloakify build error: The ${keycloak_resources} directory shouldn't exist in your build directory.`,
|
|
|
|
`(${pathRelative(process.cwd(), dirPath)}).\n`,
|
|
|
|
`Theses assets are only required for local development with Storybook.",
|
|
|
|
"Please remove this directory as an additional step of your command.\n`,
|
|
|
|
`For example: \`"build": "... && rimraf ${pathRelative(buildContext.projectDirPath, dirPath)}"\``
|
|
|
|
].join(" ")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-12 20:47:03 +02:00
|
|
|
transformCodebase({
|
2024-06-09 09:15:16 +02:00
|
|
|
srcDirPath: buildContext.projectBuildDirPath,
|
2024-05-12 20:47:03 +02:00
|
|
|
destDirPath,
|
2024-06-19 01:41:22 +02:00
|
|
|
transformSourceCode: ({ filePath, fileRelativePath, sourceCode }) => {
|
2024-06-15 14:23:35 +02:00
|
|
|
if (filePath.endsWith(".css")) {
|
2024-06-19 01:41:22 +02:00
|
|
|
const { fixedCssCode } = replaceImportsInCssCode({
|
|
|
|
cssCode: sourceCode.toString("utf8"),
|
2024-06-19 22:41:25 +02:00
|
|
|
cssFileRelativeDirPath: pathDirname(fileRelativePath),
|
2024-06-19 01:41:22 +02:00
|
|
|
buildContext
|
2024-05-12 20:47:03 +02:00
|
|
|
});
|
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
return {
|
|
|
|
modifiedSourceCode: Buffer.from(fixedCssCode, "utf8")
|
|
|
|
};
|
2024-05-12 20:47:03 +02:00
|
|
|
}
|
|
|
|
|
2024-06-15 14:23:35 +02:00
|
|
|
if (filePath.endsWith(".js")) {
|
2024-05-12 20:47:03 +02:00
|
|
|
const { fixedJsCode } = replaceImportsInJsCode({
|
2024-05-20 15:48:51 +02:00
|
|
|
jsCode: sourceCode.toString("utf8"),
|
2024-06-09 09:15:16 +02:00
|
|
|
buildContext
|
2024-05-12 20:47:03 +02:00
|
|
|
});
|
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
return {
|
|
|
|
modifiedSourceCode: Buffer.from(fixedJsCode, "utf8")
|
|
|
|
};
|
2024-05-12 20:47:03 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
return { modifiedSourceCode: sourceCode };
|
2024-05-12 20:47:03 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const { generateFtlFilesCode } = generateFtlFilesCodeFactory({
|
|
|
|
themeName,
|
2024-05-20 15:48:51 +02:00
|
|
|
indexHtmlCode: fs
|
2024-06-09 09:15:16 +02:00
|
|
|
.readFileSync(pathJoin(buildContext.projectBuildDirPath, "index.html"))
|
2024-05-20 15:48:51 +02:00
|
|
|
.toString("utf8"),
|
2024-06-09 09:15:16 +02:00
|
|
|
buildContext,
|
2024-05-20 15:48:51 +02:00
|
|
|
keycloakifyVersion: readThisNpmPackageVersion(),
|
2024-05-12 20:47:03 +02:00
|
|
|
themeType,
|
2024-05-20 15:48:51 +02:00
|
|
|
fieldNames: readFieldNameUsage({
|
2024-06-16 01:29:15 +02:00
|
|
|
themeSrcDirPath: buildContext.themeSrcDirPath,
|
2024-05-12 20:47:03 +02:00
|
|
|
themeType
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
[
|
|
|
|
...(() => {
|
|
|
|
switch (themeType) {
|
|
|
|
case "login":
|
|
|
|
return loginThemePageIds;
|
|
|
|
case "account":
|
|
|
|
return accountThemePageIds;
|
|
|
|
}
|
|
|
|
})(),
|
|
|
|
...readExtraPagesNames({
|
|
|
|
themeType,
|
2024-06-16 01:29:15 +02:00
|
|
|
themeSrcDirPath: buildContext.themeSrcDirPath
|
2024-05-12 20:47:03 +02:00
|
|
|
})
|
|
|
|
].forEach(pageId => {
|
|
|
|
const { ftlCode } = generateFtlFilesCode({ pageId });
|
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
fs.writeFileSync(
|
|
|
|
pathJoin(themeTypeDirPath, pageId),
|
|
|
|
Buffer.from(ftlCode, "utf8")
|
|
|
|
);
|
2024-05-12 20:47:03 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
generateMessageProperties({
|
2024-06-16 01:29:15 +02:00
|
|
|
themeSrcDirPath: buildContext.themeSrcDirPath,
|
2024-05-12 20:47:03 +02:00
|
|
|
themeType
|
|
|
|
}).forEach(({ languageTag, propertiesFileSource }) => {
|
|
|
|
const messagesDirPath = pathJoin(themeTypeDirPath, "messages");
|
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
fs.mkdirSync(pathJoin(themeTypeDirPath, "messages"), {
|
|
|
|
recursive: true
|
|
|
|
});
|
2024-05-12 20:47:03 +02:00
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
const propertiesFilePath = pathJoin(
|
|
|
|
messagesDirPath,
|
|
|
|
`messages_${languageTag}.properties`
|
|
|
|
);
|
2024-05-12 20:47:03 +02:00
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
fs.writeFileSync(
|
|
|
|
propertiesFilePath,
|
|
|
|
Buffer.from(propertiesFileSource, "utf8")
|
|
|
|
);
|
2024-05-12 20:47:03 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
await downloadKeycloakStaticResources({
|
2024-05-20 15:48:51 +02:00
|
|
|
keycloakVersion: (() => {
|
2024-05-12 20:47:03 +02:00
|
|
|
switch (themeType) {
|
|
|
|
case "account":
|
|
|
|
return lastKeycloakVersionWithAccountV1;
|
|
|
|
case "login":
|
2024-06-09 09:15:16 +02:00
|
|
|
return buildContext.loginThemeResourcesFromKeycloakVersion;
|
2024-05-12 20:47:03 +02:00
|
|
|
}
|
|
|
|
})(),
|
2024-05-20 15:48:51 +02:00
|
|
|
themeDirPath: pathResolve(pathJoin(themeTypeDirPath, "..")),
|
2024-05-12 20:47:03 +02:00
|
|
|
themeType,
|
2024-06-09 09:15:16 +02:00
|
|
|
buildContext
|
2024-05-12 20:47:03 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
fs.writeFileSync(
|
|
|
|
pathJoin(themeTypeDirPath, "theme.properties"),
|
|
|
|
Buffer.from(
|
|
|
|
[
|
|
|
|
`parent=${(() => {
|
|
|
|
switch (themeType) {
|
|
|
|
case "account":
|
|
|
|
return accountV1ThemeName;
|
|
|
|
case "login":
|
|
|
|
return "keycloak";
|
|
|
|
}
|
|
|
|
assert<Equals<typeof themeType, never>>(false);
|
|
|
|
})()}`,
|
2024-06-09 09:15:16 +02:00
|
|
|
...(buildContext.extraThemeProperties ?? []),
|
2024-06-12 14:43:53 +02:00
|
|
|
...buildContext.environmentVariables.map(
|
2024-06-08 14:02:07 +02:00
|
|
|
({ name, default: defaultValue }) =>
|
|
|
|
`${name}=\${env.${name}:${escapeStringForPropertiesFile(defaultValue)}}`
|
|
|
|
)
|
2024-05-12 20:47:03 +02:00
|
|
|
].join("\n\n"),
|
|
|
|
"utf8"
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
email: {
|
2024-06-16 01:29:15 +02:00
|
|
|
if (!buildContext.recordIsImplementedByThemeType.email) {
|
2024-05-12 20:47:03 +02:00
|
|
|
break email;
|
|
|
|
}
|
|
|
|
|
2024-06-16 01:29:15 +02:00
|
|
|
const emailThemeSrcDirPath = pathJoin(buildContext.themeSrcDirPath, "email");
|
2024-05-12 20:47:03 +02:00
|
|
|
|
|
|
|
transformCodebase({
|
2024-05-20 15:48:51 +02:00
|
|
|
srcDirPath: emailThemeSrcDirPath,
|
|
|
|
destDirPath: getThemeTypeDirPath({ themeType: "email" })
|
2024-05-12 20:47:03 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-06-16 01:29:15 +02:00
|
|
|
if (buildContext.recordIsImplementedByThemeType.account) {
|
2024-05-12 20:47:03 +02:00
|
|
|
await bringInAccountV1({
|
2024-06-10 07:57:12 +02:00
|
|
|
resourcesDirPath,
|
2024-06-09 09:15:16 +02:00
|
|
|
buildContext
|
2024-05-12 20:47:03 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2024-05-20 15:48:51 +02:00
|
|
|
const metaInfKeycloakThemes: MetaInfKeycloakTheme = { themes: [] };
|
2024-05-17 02:05:14 +02:00
|
|
|
|
|
|
|
metaInfKeycloakThemes.themes.push({
|
2024-05-20 15:48:51 +02:00
|
|
|
name: themeName,
|
2024-06-16 01:29:15 +02:00
|
|
|
types: objectEntries(buildContext.recordIsImplementedByThemeType)
|
2024-05-17 02:05:14 +02:00
|
|
|
.filter(([, isImplemented]) => isImplemented)
|
|
|
|
.map(([themeType]) => themeType)
|
|
|
|
});
|
2024-05-12 20:47:03 +02:00
|
|
|
|
2024-06-16 01:29:15 +02:00
|
|
|
if (buildContext.recordIsImplementedByThemeType.account) {
|
2024-05-17 02:05:14 +02:00
|
|
|
metaInfKeycloakThemes.themes.push({
|
2024-05-20 15:48:51 +02:00
|
|
|
name: accountV1ThemeName,
|
|
|
|
types: ["account"]
|
2024-05-17 02:05:14 +02:00
|
|
|
});
|
|
|
|
}
|
2024-05-12 20:47:03 +02:00
|
|
|
|
2024-05-17 02:05:14 +02:00
|
|
|
writeMetaInfKeycloakThemes({
|
2024-06-10 07:57:12 +02:00
|
|
|
resourcesDirPath,
|
2024-06-15 14:23:35 +02:00
|
|
|
getNewMetaInfKeycloakTheme: () => metaInfKeycloakThemes
|
2024-05-17 02:05:14 +02:00
|
|
|
});
|
2024-05-12 20:47:03 +02:00
|
|
|
}
|
|
|
|
}
|