From 1e863672cbdbdc94c8cf8d56c113afd65baa6588 Mon Sep 17 00:00:00 2001 From: garronej Date: Fri, 24 Mar 2023 05:43:34 +0100 Subject: [PATCH] Find the email theme in src --- src/bin/initialize-email-theme.ts | 60 +++++++++++++++---- .../generateKeycloakThemeResources.ts | 17 ++---- src/bin/keycloakify/keycloakify.ts | 6 +- 3 files changed, 56 insertions(+), 27 deletions(-) diff --git a/src/bin/initialize-email-theme.ts b/src/bin/initialize-email-theme.ts index 7086d5f8..e6f24761 100644 --- a/src/bin/initialize-email-theme.ts +++ b/src/bin/initialize-email-theme.ts @@ -1,27 +1,63 @@ #!/usr/bin/env node import { downloadBuiltinKeycloakTheme } from "./download-builtin-keycloak-theme"; -import { keycloakThemeEmailDirPath } from "./keycloakify"; import { join as pathJoin, relative as pathRelative } from "path"; import { transformCodebase } from "./tools/transformCodebase"; import { promptKeycloakVersion } from "./promptKeycloakVersion"; import * as fs from "fs"; import { getCliOptions } from "./tools/cliOptions"; import { getLogger } from "./tools/logger"; +import { crawl } from "./tools/crawl"; +import { exclude } from "tsafe/exclude"; -(async () => { +const reactProjectDirPath = process.cwd(); + +const themeSrcDirBasename = "keycloak-theme"; + +function getThemeSrcDirPath() { + const themeSrcDirPath: string | undefined = crawl(pathJoin(reactProjectDirPath, "src")) + .map(fileRelativePath => { + const split = fileRelativePath.split(themeSrcDirBasename); + + if (split.length !== 2) { + return undefined; + } + + return split[0] + themeSrcDirBasename; + }) + .filter(exclude(undefined))[0]; + return { themeSrcDirPath }; +} + +export function getEmailThemeSrcDirPath() { + const { themeSrcDirPath } = getThemeSrcDirPath(); + + const emailThemeSrcDirPath = themeSrcDirPath === undefined ? undefined : pathJoin(themeSrcDirPath, "email"); + + return { emailThemeSrcDirPath }; +} + +async function main() { const { isSilent } = getCliOptions(process.argv.slice(2)); const logger = getLogger({ isSilent }); - if (fs.existsSync(keycloakThemeEmailDirPath)) { - logger.warn(`There is already a ${pathRelative(process.cwd(), keycloakThemeEmailDirPath)} directory in your project. Aborting.`); + const { emailThemeSrcDirPath } = getEmailThemeSrcDirPath(); + + if (emailThemeSrcDirPath === undefined) { + logger.warn(`Couldn't locate you ${themeSrcDirBasename} directory`); + + process.exit(-1); + } + + if (fs.existsSync(emailThemeSrcDirPath)) { + logger.warn(`There is already a ${pathRelative(process.cwd(), emailThemeSrcDirPath)} directory in your project. Aborting.`); process.exit(-1); } const { keycloakVersion } = await promptKeycloakVersion(); - const builtinKeycloakThemeTmpDirPath = pathJoin(keycloakThemeEmailDirPath, "..", "tmp_xIdP3_builtin_keycloak_theme"); + const builtinKeycloakThemeTmpDirPath = pathJoin(emailThemeSrcDirPath, "..", "tmp_xIdP3_builtin_keycloak_theme"); await downloadBuiltinKeycloakTheme({ keycloakVersion, @@ -31,18 +67,20 @@ import { getLogger } from "./tools/logger"; transformCodebase({ "srcDirPath": pathJoin(builtinKeycloakThemeTmpDirPath, "base", "email"), - "destDirPath": keycloakThemeEmailDirPath + "destDirPath": emailThemeSrcDirPath }); { - const themePropertyFilePath = pathJoin(keycloakThemeEmailDirPath, "theme.properties"); + const themePropertyFilePath = pathJoin(emailThemeSrcDirPath, "theme.properties"); fs.writeFileSync(themePropertyFilePath, Buffer.from(`parent=base\n${fs.readFileSync(themePropertyFilePath).toString("utf8")}`, "utf8")); } - logger.log( - `${pathRelative(process.cwd(), keycloakThemeEmailDirPath)} ready to be customized, feel free to remove every file you do not customize` - ); + logger.log(`${pathRelative(process.cwd(), emailThemeSrcDirPath)} ready to be customized, feel free to remove every file you do not customize`); fs.rmSync(builtinKeycloakThemeTmpDirPath, { "recursive": true, "force": true }); -})(); +} + +if (require.main === module) { + main(); +} diff --git a/src/bin/keycloakify/generateKeycloakThemeResources.ts b/src/bin/keycloakify/generateKeycloakThemeResources.ts index 5095496b..60f330c4 100644 --- a/src/bin/keycloakify/generateKeycloakThemeResources.ts +++ b/src/bin/keycloakify/generateKeycloakThemeResources.ts @@ -10,7 +10,6 @@ import { isInside } from "../tools/isInside"; import type { BuildOptions } from "./BuildOptions"; import { assert } from "tsafe/assert"; import { Reflect } from "tsafe/Reflect"; -import { getLogger } from "../tools/logger"; export type BuildOptionsLike = BuildOptionsLike.Standalone | BuildOptionsLike.ExternalAssets; @@ -56,13 +55,11 @@ export namespace BuildOptionsLike { export async function generateKeycloakThemeResources(params: { reactAppBuildDirPath: string; keycloakThemeBuildingDirPath: string; - keycloakThemeEmailDirPath: string; + emailThemeSrcDirPath: string | undefined; keycloakVersion: string; buildOptions: BuildOptionsLike; }): Promise<{ doBundlesEmailTemplate: boolean }> { - const { reactAppBuildDirPath, keycloakThemeBuildingDirPath, keycloakThemeEmailDirPath, keycloakVersion, buildOptions } = params; - - const logger = getLogger({ isSilent: buildOptions.isSilent }); + const { reactAppBuildDirPath, keycloakThemeBuildingDirPath, emailThemeSrcDirPath, keycloakVersion, buildOptions } = params; const getThemeDirPath = (themeType: ThemeType | "email") => pathJoin(keycloakThemeBuildingDirPath, "src", "main", "resources", "theme", buildOptions.themeName, themeType); @@ -228,13 +225,7 @@ export async function generateKeycloakThemeResources(params: { let doBundlesEmailTemplate: boolean; email: { - if (!fs.existsSync(keycloakThemeEmailDirPath)) { - logger.log( - [ - `Not bundling email template because ${pathBasename(keycloakThemeEmailDirPath)} does not exist`, - `To start customizing the email template, run: 👉 npx create-keycloak-email-directory 👈` - ].join("\n") - ); + if (emailThemeSrcDirPath === undefined) { doBundlesEmailTemplate = false; break email; } @@ -242,7 +233,7 @@ export async function generateKeycloakThemeResources(params: { doBundlesEmailTemplate = true; transformCodebase({ - "srcDirPath": keycloakThemeEmailDirPath, + "srcDirPath": emailThemeSrcDirPath, "destDirPath": getThemeDirPath("email") }); } diff --git a/src/bin/keycloakify/keycloakify.ts b/src/bin/keycloakify/keycloakify.ts index 357d3582..5f376198 100644 --- a/src/bin/keycloakify/keycloakify.ts +++ b/src/bin/keycloakify/keycloakify.ts @@ -9,12 +9,12 @@ import { getLogger } from "../tools/logger"; import { getCliOptions } from "../tools/cliOptions"; import jar from "../tools/jar"; import { assert } from "tsafe/assert"; -import type { Equals } from "tsafe"; +import { Equals } from "tsafe"; +import { getEmailThemeSrcDirPath } from "../initialize-email-theme"; const reactProjectDirPath = process.cwd(); export const keycloakThemeBuildingDirPath = pathJoin(reactProjectDirPath, "build_keycloak"); -export const keycloakThemeEmailDirPath = pathJoin(reactProjectDirPath, "src", "keycloak-theme", "email"); export async function main() { const { isSilent, hasExternalAssets } = getCliOptions(process.argv.slice(2)); @@ -38,7 +38,7 @@ export async function main() { const { doBundlesEmailTemplate } = await generateKeycloakThemeResources({ keycloakThemeBuildingDirPath, - keycloakThemeEmailDirPath, + "emailThemeSrcDirPath": getEmailThemeSrcDirPath().emailThemeSrcDirPath, "reactAppBuildDirPath": pathJoin(reactProjectDirPath, "build"), buildOptions, //We have to leave it at that otherwise we break our default theme.