From cd8548fc32beec97d5a617e065bb8c37194d6f0b Mon Sep 17 00:00:00 2001 From: garronej Date: Mon, 4 Sep 2023 01:19:21 +0200 Subject: [PATCH] Remove extraThemeNames option in favor of extending themeName to accept array --- src/bin/keycloakify/BuildOptions.ts | 41 ++++++++++--------- .../keycloakify/generateFtl/generateFtl.ts | 6 +-- .../generateJavaStackFiles.ts | 5 +-- .../generateTheme/generateTheme.ts | 7 ++-- src/bin/keycloakify/keycloakify.ts | 16 ++++---- src/bin/keycloakify/parsedPackageJson.ts | 6 +-- 6 files changed, 39 insertions(+), 42 deletions(-) diff --git a/src/bin/keycloakify/BuildOptions.ts b/src/bin/keycloakify/BuildOptions.ts index 606f889f..67b30f31 100644 --- a/src/bin/keycloakify/BuildOptions.ts +++ b/src/bin/keycloakify/BuildOptions.ts @@ -8,8 +8,7 @@ import { getAbsoluteAndInOsFormatPath } from "../tools/getAbsoluteAndInOsFormatP export type BuildOptions = { isSilent: boolean; themeVersion: string; - themeName: string; - themeVariantNames: string[]; + themeNames: string[]; extraThemeProperties: string[] | undefined; groupId: string; artifactId: string; @@ -42,30 +41,32 @@ export function readBuildOptions(params: { reactAppRootDirPath: string; processA const { name, keycloakify = {}, version, homepage } = parsedPackageJson; - const { - extraThemeProperties, - groupId, - artifactId, - doCreateJar, - loginThemeResourcesFromKeycloakVersion, - themeVariantNames = [] - } = keycloakify ?? {}; + const { extraThemeProperties, groupId, artifactId, doCreateJar, loginThemeResourcesFromKeycloakVersion } = keycloakify ?? {}; - const themeName = - keycloakify.themeName ?? - name - .replace(/^@(.*)/, "$1") - .split("/") - .join("-"); + const themeNames = (() => { + if (keycloakify.themeName === undefined) { + return [ + name + .replace(/^@(.*)/, "$1") + .split("/") + .join("-") + ]; + } + + if (typeof keycloakify.themeName === "string") { + return [keycloakify.themeName]; + } + + return keycloakify.themeName; + })(); return { reactAppRootDirPath, - themeName, - themeVariantNames, + themeNames, "doCreateJar": doCreateJar ?? true, - "artifactId": process.env.KEYCLOAKIFY_ARTIFACT_ID ?? artifactId ?? `${themeName}-keycloak-theme`, + "artifactId": process.env.KEYCLOAKIFY_ARTIFACT_ID ?? artifactId ?? `${themeNames[0]}-keycloak-theme`, "groupId": (() => { - const fallbackGroupId = `${themeName}.keycloak`; + const fallbackGroupId = `${themeNames[0]}.keycloak`; return ( process.env.KEYCLOAKIFY_GROUP_ID ?? diff --git a/src/bin/keycloakify/generateFtl/generateFtl.ts b/src/bin/keycloakify/generateFtl/generateFtl.ts index 9da0a312..6455f398 100644 --- a/src/bin/keycloakify/generateFtl/generateFtl.ts +++ b/src/bin/keycloakify/generateFtl/generateFtl.ts @@ -11,7 +11,6 @@ import { assert } from "tsafe/assert"; import type { ThemeType } from "../../constants"; export type BuildOptionsLike = { - themeName: string; themeVersion: string; urlPathname: string | undefined; }; @@ -19,6 +18,7 @@ export type BuildOptionsLike = { assert(); export function generateFtlFilesCodeFactory(params: { + themeName: string; indexHtmlCode: string; //NOTE: Expected to be an empty object if external assets mode is enabled. cssGlobalsToDefine: Record; @@ -27,7 +27,7 @@ export function generateFtlFilesCodeFactory(params: { themeType: ThemeType; fieldNames: string[]; }) { - const { cssGlobalsToDefine, indexHtmlCode, buildOptions, keycloakifyVersion, themeType, fieldNames } = params; + const { themeName, cssGlobalsToDefine, indexHtmlCode, buildOptions, keycloakifyVersion, themeType, fieldNames } = params; const $ = cheerio.load(indexHtmlCode); @@ -101,7 +101,7 @@ export function generateFtlFilesCodeFactory(params: { .replace("KEYCLOAKIFY_VERSION_xEdKd3xEdr", keycloakifyVersion) .replace("KEYCLOAKIFY_THEME_VERSION_sIgKd3xEdr3dx", buildOptions.themeVersion) .replace("KEYCLOAKIFY_THEME_TYPE_dExKd3xEdr", themeType) - .replace("KEYCLOAKIFY_THEME_NAME_cXxKd3xEer", buildOptions.themeName), + .replace("KEYCLOAKIFY_THEME_NAME_cXxKd3xEer", themeName), "": [ "<#if scripts??>", " <#list scripts as script>", diff --git a/src/bin/keycloakify/generateJavaStackFiles/generateJavaStackFiles.ts b/src/bin/keycloakify/generateJavaStackFiles/generateJavaStackFiles.ts index 6d5941d0..7e843d9b 100644 --- a/src/bin/keycloakify/generateJavaStackFiles/generateJavaStackFiles.ts +++ b/src/bin/keycloakify/generateJavaStackFiles/generateJavaStackFiles.ts @@ -7,13 +7,12 @@ import { type ThemeType } from "../../constants"; import { bringInAccountV1, accountV1 } from "./bringInAccountV1"; export type BuildOptionsLike = { - themeName: string; - themeVariantNames: string[]; groupId: string; artifactId: string; themeVersion: string; cacheDirPath: string; keycloakifyBuildDirPath: string; + themeNames: string[]; }; { @@ -178,7 +177,7 @@ export async function generateJavaStackFiles(params: { "types": ["account"] } ]), - ...[buildOptions.themeName, ...buildOptions.themeVariantNames] + ...buildOptions.themeNames .map(themeName => [ { "name": themeName, diff --git a/src/bin/keycloakify/generateTheme/generateTheme.ts b/src/bin/keycloakify/generateTheme/generateTheme.ts index 79b3614b..d96ef376 100644 --- a/src/bin/keycloakify/generateTheme/generateTheme.ts +++ b/src/bin/keycloakify/generateTheme/generateTheme.ts @@ -15,7 +15,6 @@ import { generateMessageProperties } from "./generateMessageProperties"; import { readStaticResourcesUsage } from "./readStaticResourcesUsage"; export type BuildOptionsLike = { - themeName: string; extraThemeProperties: string[] | undefined; themeVersion: string; loginThemeResourcesFromKeycloakVersion: string; @@ -28,15 +27,16 @@ export type BuildOptionsLike = { assert(); export async function generateTheme(params: { + themeName: string; themeSrcDirPath: string; keycloakifySrcDirPath: string; buildOptions: BuildOptionsLike; keycloakifyVersion: string; }): Promise { - const { themeSrcDirPath, keycloakifySrcDirPath, buildOptions, keycloakifyVersion } = params; + const { themeName, themeSrcDirPath, keycloakifySrcDirPath, buildOptions, keycloakifyVersion } = params; const getThemeDirPath = (themeType: ThemeType | "email") => - pathJoin(buildOptions.keycloakifyBuildDirPath, "src", "main", "resources", "theme", buildOptions.themeName, themeType); + pathJoin(buildOptions.keycloakifyBuildDirPath, "src", "main", "resources", "theme", themeName, themeType); let allCssGlobalsToDefine: Record = {}; @@ -106,6 +106,7 @@ export async function generateTheme(params: { generateFtlFilesCode_glob !== undefined ? generateFtlFilesCode_glob : generateFtlFilesCodeFactory({ + themeName, "indexHtmlCode": fs.readFileSync(pathJoin(buildOptions.reactAppBuildDirPath, "index.html")).toString("utf8"), "cssGlobalsToDefine": allCssGlobalsToDefine, buildOptions, diff --git a/src/bin/keycloakify/keycloakify.ts b/src/bin/keycloakify/keycloakify.ts index b55b9e19..eafc254e 100644 --- a/src/bin/keycloakify/keycloakify.ts +++ b/src/bin/keycloakify/keycloakify.ts @@ -26,14 +26,12 @@ export async function main() { const { themeSrcDirPath } = getThemeSrcDirPath({ reactAppRootDirPath }); - for (const themeName of [buildOptions.themeName, ...buildOptions.themeVariantNames]) { + for (const themeName of buildOptions.themeNames) { await generateTheme({ + themeName, themeSrcDirPath, "keycloakifySrcDirPath": pathJoin(keycloakifyDirPath, "src"), - "buildOptions": { - ...buildOptions, - "themeName": themeName - }, + buildOptions, "keycloakifyVersion": (() => { const version = JSON.parse(fs.readFileSync(pathJoin(keycloakifyDirPath, "package.json")).toString("utf8"))["version"]; @@ -124,15 +122,15 @@ export async function main() { "- Log into the admin console 👉 http://localhost:8080/admin username: admin, password: admin 👈", `- Create a realm: Master -> AddRealm -> Name: myrealm`, `- Enable registration: Realm settings -> Login tab -> User registration: on`, - `- Enable the Account theme (optional): Realm settings -> Themes tab -> Account theme: ${buildOptions.themeName}`, - ` Clients -> account -> Login theme: ${buildOptions.themeName}`, - `- Enable the email theme (optional): Realm settings -> Themes tab -> Email theme: ${buildOptions.themeName} (option will appear only if you have ran npx initialize-email-theme)`, + `- Enable the Account theme (optional): Realm settings -> Themes tab -> Account theme: ${buildOptions.themeNames[0]}`, + ` Clients -> account -> Login theme: ${buildOptions.themeNames[0]}`, + `- Enable the email theme (optional): Realm settings -> Themes tab -> Email theme: ${buildOptions.themeNames[0]} (option will appear only if you have ran npx initialize-email-theme)`, `- Create a client Clients -> Create -> Client ID: myclient`, ` Root URL: https://www.keycloak.org/app/`, ` Valid redirect URIs: https://www.keycloak.org/app* http://localhost* (localhost is optional)`, ` Valid post logout redirect URIs: https://www.keycloak.org/app* http://localhost*`, ` Web origins: *`, - ` Login Theme: ${buildOptions.themeName}`, + ` Login Theme: ${buildOptions.themeNames[0]}`, ` Save (button at the bottom of the page)`, ``, `- Go to 👉 https://www.keycloak.org/app/ 👈 Click "Save" then "Sign in". You should see your login page`, diff --git a/src/bin/keycloakify/parsedPackageJson.ts b/src/bin/keycloakify/parsedPackageJson.ts index ed069e11..6e869a5c 100644 --- a/src/bin/keycloakify/parsedPackageJson.ts +++ b/src/bin/keycloakify/parsedPackageJson.ts @@ -17,8 +17,7 @@ export type ParsedPackageJson = { loginThemeResourcesFromKeycloakVersion?: string; reactAppBuildDirPath?: string; keycloakifyBuildDirPath?: string; - themeName?: string; - themeVariantNames?: string[]; + themeName?: string | string[]; }; }; @@ -36,8 +35,7 @@ export const zParsedPackageJson = z.object({ "loginThemeResourcesFromKeycloakVersion": z.string().optional(), "reactAppBuildDirPath": z.string().optional(), "keycloakifyBuildDirPath": z.string().optional(), - "themeName": z.string().optional(), - "themeVariantNames": z.array(z.string()).optional() + "themeName": z.union([z.string(), z.array(z.string())]).optional() }) .optional() });