From e09acedf67970fdf18916cd9067e7bcbdb40f006 Mon Sep 17 00:00:00 2001 From: Joseph Garrone Date: Fri, 17 May 2024 02:05:14 +0200 Subject: [PATCH] factorize access to META-INF/keycloak-themes.json --- src/bin/keycloakify/buildJars/buildJar.ts | 3 +- src/bin/keycloakify/buildJars/buildJars.ts | 7 ++- .../generateSrcMainResourcesForMainTheme.ts | 59 ++++++++----------- ...generateSrcMainResourcesForThemeVariant.ts | 24 +++----- src/bin/shared/metaInfKeycloakThemes.ts | 34 +++++++++++ 5 files changed, 72 insertions(+), 55 deletions(-) create mode 100644 src/bin/shared/metaInfKeycloakThemes.ts diff --git a/src/bin/keycloakify/buildJars/buildJar.ts b/src/bin/keycloakify/buildJars/buildJar.ts index 2b27d43a..824aeab1 100644 --- a/src/bin/keycloakify/buildJars/buildJar.ts +++ b/src/bin/keycloakify/buildJars/buildJar.ts @@ -10,6 +10,7 @@ import { readFileSync } from "fs"; import { isInside } from "../../tools/isInside"; import child_process from "child_process"; import { rmSync } from "../../tools/fs.rmSync"; +import { getMetaInfKeycloakThemesJsonPath } from "../../shared/metaInfKeycloakThemes"; export type BuildOptionsLike = BuildOptionsLike_generatePom & { keycloakifyBuildDirPath: string; @@ -34,7 +35,7 @@ export async function buildJar(params: { rmSync(keycloakifyBuildTmpDirPath, { "recursive": true, "force": true }); { - const keycloakThemesJsonFilePath = pathJoin("src", "main", "resources", "META-INF", "keycloak-themes.json"); + const keycloakThemesJsonFilePath = getMetaInfKeycloakThemesJsonPath({ "keycloakifyBuildDirPath": "" }); const themePropertiesFilePathSet = new Set( ...buildOptions.themeNames.map(themeName => pathJoin("src", "main", "resources", "theme", themeName, "account", "theme.properties")) diff --git a/src/bin/keycloakify/buildJars/buildJars.ts b/src/bin/keycloakify/buildJars/buildJars.ts index 271ee564..7c7500dd 100644 --- a/src/bin/keycloakify/buildJars/buildJars.ts +++ b/src/bin/keycloakify/buildJars/buildJars.ts @@ -5,6 +5,8 @@ import { getKeycloakVersionRangeForJar } from "./getKeycloakVersionRangeForJar"; import { buildJar, BuildOptionsLike as BuildOptionsLike_buildJar } from "./buildJar"; import type { BuildOptions } from "../../shared/buildOptions"; import { getJarFileBasename } from "./getJarFileBasename"; +import { readMetaInfKeycloakThemes } from "../../shared/metaInfKeycloakThemes"; +import { accountV1ThemeName } from "../../shared/constants"; export type BuildOptionsLike = BuildOptionsLike_buildJar & { keycloakifyBuildDirPath: string; @@ -15,8 +17,9 @@ assert(); export async function buildJars(params: { buildOptions: BuildOptionsLike }): Promise { const { buildOptions } = params; - // TODO: - const doesImplementAccountTheme= ; + const doesImplementAccountTheme = readMetaInfKeycloakThemes({ + "keycloakifyBuildDirPath": buildOptions.keycloakifyBuildDirPath + }).themes.some(({ name }) => name === accountV1ThemeName); await Promise.all( keycloakAccountV1Versions diff --git a/src/bin/keycloakify/generateSrcMainResources/generateSrcMainResourcesForMainTheme.ts b/src/bin/keycloakify/generateSrcMainResources/generateSrcMainResourcesForMainTheme.ts index 38584b41..ce006f15 100644 --- a/src/bin/keycloakify/generateSrcMainResources/generateSrcMainResourcesForMainTheme.ts +++ b/src/bin/keycloakify/generateSrcMainResources/generateSrcMainResourcesForMainTheme.ts @@ -1,6 +1,6 @@ import { transformCodebase } from "../../tools/transformCodebase"; import * as fs from "fs"; -import { join as pathJoin, resolve as pathResolve, dirname as pathDirname } from "path"; +import { join as pathJoin, resolve as pathResolve } from "path"; import { replaceImportsInJsCode } from "../replacers/replaceImportsInJsCode"; import { replaceImportsInCssCode } from "../replacers/replaceImportsInCssCode"; import { generateFtlFilesCodeFactory } from "../generateFtl"; @@ -23,6 +23,8 @@ import { bringInAccountV1 } from "./bringInAccountV1"; import { getThemeSrcDirPath } from "../../shared/getThemeSrcDirPath"; import { rmSync } from "../../tools/fs.rmSync"; import { readThisNpmProjectVersion } from "../../tools/readThisNpmProjectVersion"; +import { writeMetaInfKeycloakThemes, type MetaInfKeycloakTheme } from "../../shared/metaInfKeycloakThemes"; +import { objectEntries } from "tsafe/objectEntries"; export type BuildOptionsLike = { bundler: "vite" | "webpack"; @@ -40,10 +42,7 @@ export type BuildOptionsLike = { assert(); -export async function generateSrcMainResourcesForMainTheme(params: { - themeName: string; - buildOptions: BuildOptionsLike -}): Promise { +export async function generateSrcMainResourcesForMainTheme(params: { themeName: string; buildOptions: BuildOptionsLike }): Promise { const { themeName, buildOptions } = params; const { themeSrcDirPath } = getThemeSrcDirPath({ "reactAppRootDirPath": buildOptions.reactAppRootDirPath }); @@ -230,44 +229,32 @@ export async function generateSrcMainResourcesForMainTheme(params: { }); } - const parsedKeycloakThemeJson: { themes: { name: string; types: string[] }[] } = { "themes": [] }; - - parsedKeycloakThemeJson.themes.push({ - "name": themeName, - "types": Object.entries(implementedThemeTypes) - .filter(([, isImplemented]) => isImplemented) - .map(([themeType]) => themeType) - }); - - account_specific_extra_work: { - if (!implementedThemeTypes.account) { - break account_specific_extra_work; - } - + if (implementedThemeTypes.account) { await bringInAccountV1({ buildOptions }); - - parsedKeycloakThemeJson.themes.push({ - "name": accountV1ThemeName, - "types": ["account"] - }); } { - const keycloakThemeJsonFilePath = pathJoin( - buildOptions.keycloakifyBuildDirPath, - "src", - "main", - "resources", - "META-INF", - "keycloak-themes.json" - ); + const metaInfKeycloakThemes: MetaInfKeycloakTheme = { "themes": [] }; - try { - fs.mkdirSync(pathDirname(keycloakThemeJsonFilePath)); - } catch {} + metaInfKeycloakThemes.themes.push({ + "name": themeName, + "types": objectEntries(implementedThemeTypes) + .filter(([, isImplemented]) => isImplemented) + .map(([themeType]) => themeType) + }); - fs.writeFileSync(keycloakThemeJsonFilePath, Buffer.from(JSON.stringify(parsedKeycloakThemeJson, null, 2), "utf8")); + if (implementedThemeTypes.account) { + metaInfKeycloakThemes.themes.push({ + "name": accountV1ThemeName, + "types": ["account"] + }); + } + + writeMetaInfKeycloakThemes({ + "keycloakifyBuildDirPath": buildOptions.keycloakifyBuildDirPath, + metaInfKeycloakThemes + }); } } diff --git a/src/bin/keycloakify/generateSrcMainResources/generateSrcMainResourcesForThemeVariant.ts b/src/bin/keycloakify/generateSrcMainResources/generateSrcMainResourcesForThemeVariant.ts index 0f68f2bd..9dcd4012 100644 --- a/src/bin/keycloakify/generateSrcMainResources/generateSrcMainResourcesForThemeVariant.ts +++ b/src/bin/keycloakify/generateSrcMainResources/generateSrcMainResourcesForThemeVariant.ts @@ -1,8 +1,8 @@ import { join as pathJoin, extname as pathExtname, sep as pathSep } from "path"; import { transformCodebase } from "../../tools/transformCodebase"; import type { BuildOptions } from "../../shared/buildOptions"; +import { readMetaInfKeycloakThemes, writeMetaInfKeycloakThemes } from "../../shared/metaInfKeycloakThemes"; import { assert } from "tsafe/assert"; -import * as fs from "fs"; export type BuildOptionsLike = { keycloakifyBuildDirPath: string; @@ -37,28 +37,20 @@ export function generateSrcMainResourcesForThemeVariant(params: { themeName: str }); { - const keycloakThemeJsonFilePath = pathJoin( - buildOptions.keycloakifyBuildDirPath, - "src", - "main", - "resources", - "META-INF", - "keycloak-themes.json" - ); + const updatedMetaInfKeycloakThemes = readMetaInfKeycloakThemes({ "keycloakifyBuildDirPath": buildOptions.keycloakifyBuildDirPath }); - const modifiedParsedJson = JSON.parse(fs.readFileSync(keycloakThemeJsonFilePath).toString("utf8")) as { - themes: { name: string; types: string[] }[]; - }; - - modifiedParsedJson.themes.push({ + updatedMetaInfKeycloakThemes.themes.push({ "name": themeVariantName, "types": (() => { - const theme = modifiedParsedJson.themes.find(({ name }) => name === themeName); + const theme = updatedMetaInfKeycloakThemes.themes.find(({ name }) => name === themeName); assert(theme !== undefined); return theme.types; })() }); - fs.writeFileSync(keycloakThemeJsonFilePath, Buffer.from(JSON.stringify(modifiedParsedJson, null, 2), "utf8")); + writeMetaInfKeycloakThemes({ + "keycloakifyBuildDirPath": buildOptions.keycloakifyBuildDirPath, + "metaInfKeycloakThemes": updatedMetaInfKeycloakThemes + }); } } diff --git a/src/bin/shared/metaInfKeycloakThemes.ts b/src/bin/shared/metaInfKeycloakThemes.ts new file mode 100644 index 00000000..7f1df3fd --- /dev/null +++ b/src/bin/shared/metaInfKeycloakThemes.ts @@ -0,0 +1,34 @@ +import { join as pathJoin, dirname as pathDirname } from "path"; +import type { ThemeType } from "./constants"; +import * as fs from "fs"; + +export type MetaInfKeycloakTheme = { + themes: { name: string; types: (ThemeType | "email")[] }[]; +}; + +export function getMetaInfKeycloakThemesJsonPath(params: { keycloakifyBuildDirPath: string }) { + const { keycloakifyBuildDirPath } = params; + + return pathJoin(keycloakifyBuildDirPath, "src", "main", "resources", "META-INF", "keycloak-themes.json"); +} + +export function readMetaInfKeycloakThemes(params: { keycloakifyBuildDirPath: string }): MetaInfKeycloakTheme { + const { keycloakifyBuildDirPath } = params; + + return JSON.parse(fs.readFileSync(getMetaInfKeycloakThemesJsonPath({ keycloakifyBuildDirPath })).toString("utf8")) as MetaInfKeycloakTheme; +} + +export function writeMetaInfKeycloakThemes(params: { keycloakifyBuildDirPath: string; metaInfKeycloakThemes: MetaInfKeycloakTheme }) { + const { keycloakifyBuildDirPath, metaInfKeycloakThemes } = params; + + const metaInfKeycloakThemesJsonPath = getMetaInfKeycloakThemesJsonPath({ keycloakifyBuildDirPath }); + + { + const dirPath = pathDirname(metaInfKeycloakThemesJsonPath); + if (!fs.existsSync(dirPath)) { + fs.mkdirSync(dirPath, { "recursive": true }); + } + } + + fs.writeFileSync(metaInfKeycloakThemesJsonPath, Buffer.from(JSON.stringify(metaInfKeycloakThemes, null, 2), "utf8")); +}