From 4b8aecfe91e4c4744916bc0b279d70a2d06b1ba2 Mon Sep 17 00:00:00 2001 From: garronej Date: Wed, 21 Jun 2023 18:06:12 +0200 Subject: [PATCH] #364 --- src/bin/eject-keycloak-page.ts | 4 -- src/bin/getSrcDirPath.ts | 26 +++++-- src/bin/initialize-email-theme.ts | 6 -- .../generateTheme/generateTheme.ts | 55 +++++++-------- .../generateTheme/readFieldNameUsage.ts | 69 ++----------------- src/bin/keycloakify/keycloakify.ts | 6 -- 6 files changed, 49 insertions(+), 117 deletions(-) diff --git a/src/bin/eject-keycloak-page.ts b/src/bin/eject-keycloak-page.ts index c0e507d8..1093711b 100644 --- a/src/bin/eject-keycloak-page.ts +++ b/src/bin/eject-keycloak-page.ts @@ -51,10 +51,6 @@ import { getThemeSrcDirPath } from "./getSrcDirPath"; const { themeSrcDirPath } = getThemeSrcDirPath({ "projectDirPath": process.cwd() }); - if (themeSrcDirPath === undefined) { - throw new Error("Couldn't locate your theme sources"); - } - const targetFilePath = pathJoin(themeSrcDirPath, themeType, "pages", pageBasename); if (existsSync(targetFilePath)) { diff --git a/src/bin/getSrcDirPath.ts b/src/bin/getSrcDirPath.ts index 15a4cbd2..893e29b1 100644 --- a/src/bin/getSrcDirPath.ts +++ b/src/bin/getSrcDirPath.ts @@ -2,9 +2,11 @@ import * as fs from "fs"; import { exclude } from "tsafe"; import { crawl } from "./tools/crawl"; import { join as pathJoin } from "path"; +import { themeTypes } from "./keycloakify/generateFtl"; const themeSrcDirBasename = "keycloak-theme"; +/** Can't catch error, if the directory isn't found, this function will just exit the process with an error message. */ export function getThemeSrcDirPath(params: { projectDirPath: string }) { const { projectDirPath } = params; @@ -22,12 +24,24 @@ export function getThemeSrcDirPath(params: { projectDirPath: string }) { }) .filter(exclude(undefined))[0]; - if (themeSrcDirPath === undefined) { - if (fs.existsSync(pathJoin(srcDirPath, "login")) || fs.existsSync(pathJoin(srcDirPath, "account"))) { - return { "themeSrcDirPath": srcDirPath }; - } - return { "themeSrcDirPath": undefined }; + if (themeSrcDirPath !== undefined) { + return { themeSrcDirPath }; } - return { themeSrcDirPath }; + for (const themeType of [...themeTypes, "email"]) { + if (!fs.existsSync(pathJoin(srcDirPath, themeType))) { + continue; + } + return { "themeSrcDirPath": srcDirPath }; + } + + console.error( + [ + "Can't locate your theme source directory. It should be either: ", + "src/ or src/keycloak-theme.", + "Example in the starter: https://github.com/keycloakify/keycloakify-starter/tree/main/src/keycloak-theme" + ].join("\n") + ); + + process.exit(-1); } diff --git a/src/bin/initialize-email-theme.ts b/src/bin/initialize-email-theme.ts index 3f972ad7..04388049 100644 --- a/src/bin/initialize-email-theme.ts +++ b/src/bin/initialize-email-theme.ts @@ -21,12 +21,6 @@ export async function main() { "projectDirPath": process.cwd() }); - if (themeSrcDirPath === undefined) { - logger.warn("Couldn't locate your theme source directory"); - - process.exit(-1); - } - const emailThemeSrcDirPath = pathJoin(themeSrcDirPath, "email"); if (fs.existsSync(emailThemeSrcDirPath)) { diff --git a/src/bin/keycloakify/generateTheme/generateTheme.ts b/src/bin/keycloakify/generateTheme/generateTheme.ts index adebfa77..4c0b885c 100644 --- a/src/bin/keycloakify/generateTheme/generateTheme.ts +++ b/src/bin/keycloakify/generateTheme/generateTheme.ts @@ -52,7 +52,7 @@ assert(); export async function generateTheme(params: { reactAppBuildDirPath: string; keycloakThemeBuildingDirPath: string; - themeSrcDirPath: string | undefined; + themeSrcDirPath: string; keycloakifySrcDirPath: string; buildOptions: BuildOptionsLike; keycloakifyVersion: string; @@ -67,6 +67,10 @@ export async function generateTheme(params: { let generateFtlFilesCode_glob: ReturnType["generateFtlFilesCode"] | undefined = undefined; for (const themeType of themeTypes) { + if (!fs.existsSync(pathJoin(themeSrcDirPath, themeType))) { + continue; + } + const themeDirPath = getThemeDirPath(themeType); copy_app_resources_to_theme_path: { @@ -132,26 +136,21 @@ export async function generateTheme(params: { }); } - const generateFtlFilesCode = (() => { - if (generateFtlFilesCode_glob !== undefined) { - return generateFtlFilesCode_glob; - } - - const { generateFtlFilesCode } = generateFtlFilesCodeFactory({ - "indexHtmlCode": fs.readFileSync(pathJoin(reactAppBuildDirPath, "index.html")).toString("utf8"), - "cssGlobalsToDefine": allCssGlobalsToDefine, - buildOptions, - keycloakifyVersion, - themeType, - "fieldNames": readFieldNameUsage({ - keycloakifySrcDirPath, - themeSrcDirPath, - themeType - }) - }); - - return generateFtlFilesCode; - })(); + const generateFtlFilesCode = + generateFtlFilesCode_glob !== undefined + ? generateFtlFilesCode_glob + : generateFtlFilesCodeFactory({ + "indexHtmlCode": fs.readFileSync(pathJoin(reactAppBuildDirPath, "index.html")).toString("utf8"), + "cssGlobalsToDefine": allCssGlobalsToDefine, + buildOptions, + keycloakifyVersion, + themeType, + "fieldNames": readFieldNameUsage({ + keycloakifySrcDirPath, + themeSrcDirPath, + themeType + }) + }).generateFtlFilesCode; [ ...(() => { @@ -162,12 +161,10 @@ export async function generateTheme(params: { return accountThemePageIds; } })(), - ...(themeSrcDirPath === undefined - ? [] - : readExtraPagesNames({ - themeType, - themeSrcDirPath - })) + ...readExtraPagesNames({ + themeType, + themeSrcDirPath + }) ].forEach(pageId => { const { ftlCode } = generateFtlFilesCode({ pageId }); @@ -224,10 +221,6 @@ export async function generateTheme(params: { } email: { - if (themeSrcDirPath === undefined) { - break email; - } - const emailThemeSrcDirPath = pathJoin(themeSrcDirPath, "email"); if (!fs.existsSync(emailThemeSrcDirPath)) { diff --git a/src/bin/keycloakify/generateTheme/readFieldNameUsage.ts b/src/bin/keycloakify/generateTheme/readFieldNameUsage.ts index 9a930a2e..cf0cbae9 100644 --- a/src/bin/keycloakify/generateTheme/readFieldNameUsage.ts +++ b/src/bin/keycloakify/generateTheme/readFieldNameUsage.ts @@ -5,74 +5,15 @@ import * as fs from "fs"; import type { ThemeType } from "../generateFtl"; import { exclude } from "tsafe/exclude"; -export function readFieldNameUsage(params: { - keycloakifySrcDirPath: string; - themeSrcDirPath: string | undefined; - themeType: ThemeType | "email"; -}): string[] { +/** Assumes the theme type exists */ +export function readFieldNameUsage(params: { keycloakifySrcDirPath: string; themeSrcDirPath: string; themeType: ThemeType }): string[] { const { keycloakifySrcDirPath, themeSrcDirPath, themeType } = params; const fieldNames: string[] = []; - if (themeSrcDirPath === undefined) { - //If we can't detect the user theme directory we restore the fieldNames we had previously to prevent errors. - fieldNames.push( - ...[ - "global", - "userLabel", - "username", - "email", - "firstName", - "lastName", - "password", - "password-confirm", - "totp", - "totpSecret", - "SAMLRequest", - "SAMLResponse", - "relayState", - "device_user_code", - "code", - "password-new", - "rememberMe", - "login", - "authenticationExecution", - "cancel-aia", - "clientDataJSON", - "authenticatorData", - "signature", - "credentialId", - "userHandle", - "error", - "authn_use_chk", - "authenticationExecution", - "isSetRetry", - "try-again", - "attestationObject", - "publicKeyCredentialId", - "authenticatorLabel" - ] - ); - } - - for (const srcDirPath of ( - [ - pathJoin(keycloakifySrcDirPath, themeType), - (() => { - if (themeSrcDirPath === undefined) { - return undefined; - } - - const srcDirPath = pathJoin(themeSrcDirPath, themeType); - - if (!fs.existsSync(srcDirPath)) { - return undefined; - } - - return srcDirPath; - })() - ] as const - ).filter(exclude(undefined))) { + for (const srcDirPath of ([pathJoin(keycloakifySrcDirPath, themeType), pathJoin(themeSrcDirPath, themeType)] as const).filter( + exclude(undefined) + )) { const filePaths = crawl({ "dirPath": srcDirPath, "returnedPathsType": "absolute" }).filter(filePath => /\.(ts|tsx|js|jsx)$/.test(filePath)); for (const filePath of filePaths) { diff --git a/src/bin/keycloakify/keycloakify.ts b/src/bin/keycloakify/keycloakify.ts index c24c72e6..b3bac46e 100644 --- a/src/bin/keycloakify/keycloakify.ts +++ b/src/bin/keycloakify/keycloakify.ts @@ -57,12 +57,6 @@ export async function main() { "email": false }; - if (themeSrcDirPath === undefined) { - implementedThemeTypes["login"] = true; - implementedThemeTypes["account"] = true; - return implementedThemeTypes; - } - for (const themeType of objectKeys(implementedThemeTypes)) { if (!fs.existsSync(pathJoin(themeSrcDirPath, themeType))) { continue;