This commit is contained in:
garronej
2023-06-21 18:06:12 +02:00
parent 8d5ce21df4
commit 4b8aecfe91
6 changed files with 49 additions and 117 deletions

View File

@ -51,10 +51,6 @@ import { getThemeSrcDirPath } from "./getSrcDirPath";
const { themeSrcDirPath } = getThemeSrcDirPath({ "projectDirPath": process.cwd() }); 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); const targetFilePath = pathJoin(themeSrcDirPath, themeType, "pages", pageBasename);
if (existsSync(targetFilePath)) { if (existsSync(targetFilePath)) {

View File

@ -2,9 +2,11 @@ import * as fs from "fs";
import { exclude } from "tsafe"; import { exclude } from "tsafe";
import { crawl } from "./tools/crawl"; import { crawl } from "./tools/crawl";
import { join as pathJoin } from "path"; import { join as pathJoin } from "path";
import { themeTypes } from "./keycloakify/generateFtl";
const themeSrcDirBasename = "keycloak-theme"; 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 }) { export function getThemeSrcDirPath(params: { projectDirPath: string }) {
const { projectDirPath } = params; const { projectDirPath } = params;
@ -22,12 +24,24 @@ export function getThemeSrcDirPath(params: { projectDirPath: string }) {
}) })
.filter(exclude(undefined))[0]; .filter(exclude(undefined))[0];
if (themeSrcDirPath === undefined) { if (themeSrcDirPath !== undefined) {
if (fs.existsSync(pathJoin(srcDirPath, "login")) || fs.existsSync(pathJoin(srcDirPath, "account"))) { return { themeSrcDirPath };
return { "themeSrcDirPath": srcDirPath };
}
return { "themeSrcDirPath": undefined };
} }
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);
} }

View File

@ -21,12 +21,6 @@ export async function main() {
"projectDirPath": process.cwd() "projectDirPath": process.cwd()
}); });
if (themeSrcDirPath === undefined) {
logger.warn("Couldn't locate your theme source directory");
process.exit(-1);
}
const emailThemeSrcDirPath = pathJoin(themeSrcDirPath, "email"); const emailThemeSrcDirPath = pathJoin(themeSrcDirPath, "email");
if (fs.existsSync(emailThemeSrcDirPath)) { if (fs.existsSync(emailThemeSrcDirPath)) {

View File

@ -52,7 +52,7 @@ assert<BuildOptions extends BuildOptionsLike ? true : false>();
export async function generateTheme(params: { export async function generateTheme(params: {
reactAppBuildDirPath: string; reactAppBuildDirPath: string;
keycloakThemeBuildingDirPath: string; keycloakThemeBuildingDirPath: string;
themeSrcDirPath: string | undefined; themeSrcDirPath: string;
keycloakifySrcDirPath: string; keycloakifySrcDirPath: string;
buildOptions: BuildOptionsLike; buildOptions: BuildOptionsLike;
keycloakifyVersion: string; keycloakifyVersion: string;
@ -67,6 +67,10 @@ export async function generateTheme(params: {
let generateFtlFilesCode_glob: ReturnType<typeof generateFtlFilesCodeFactory>["generateFtlFilesCode"] | undefined = undefined; let generateFtlFilesCode_glob: ReturnType<typeof generateFtlFilesCodeFactory>["generateFtlFilesCode"] | undefined = undefined;
for (const themeType of themeTypes) { for (const themeType of themeTypes) {
if (!fs.existsSync(pathJoin(themeSrcDirPath, themeType))) {
continue;
}
const themeDirPath = getThemeDirPath(themeType); const themeDirPath = getThemeDirPath(themeType);
copy_app_resources_to_theme_path: { copy_app_resources_to_theme_path: {
@ -132,12 +136,10 @@ export async function generateTheme(params: {
}); });
} }
const generateFtlFilesCode = (() => { const generateFtlFilesCode =
if (generateFtlFilesCode_glob !== undefined) { generateFtlFilesCode_glob !== undefined
return generateFtlFilesCode_glob; ? generateFtlFilesCode_glob
} : generateFtlFilesCodeFactory({
const { generateFtlFilesCode } = generateFtlFilesCodeFactory({
"indexHtmlCode": fs.readFileSync(pathJoin(reactAppBuildDirPath, "index.html")).toString("utf8"), "indexHtmlCode": fs.readFileSync(pathJoin(reactAppBuildDirPath, "index.html")).toString("utf8"),
"cssGlobalsToDefine": allCssGlobalsToDefine, "cssGlobalsToDefine": allCssGlobalsToDefine,
buildOptions, buildOptions,
@ -148,10 +150,7 @@ export async function generateTheme(params: {
themeSrcDirPath, themeSrcDirPath,
themeType themeType
}) })
}); }).generateFtlFilesCode;
return generateFtlFilesCode;
})();
[ [
...(() => { ...(() => {
@ -162,12 +161,10 @@ export async function generateTheme(params: {
return accountThemePageIds; return accountThemePageIds;
} }
})(), })(),
...(themeSrcDirPath === undefined ...readExtraPagesNames({
? []
: readExtraPagesNames({
themeType, themeType,
themeSrcDirPath themeSrcDirPath
})) })
].forEach(pageId => { ].forEach(pageId => {
const { ftlCode } = generateFtlFilesCode({ pageId }); const { ftlCode } = generateFtlFilesCode({ pageId });
@ -224,10 +221,6 @@ export async function generateTheme(params: {
} }
email: { email: {
if (themeSrcDirPath === undefined) {
break email;
}
const emailThemeSrcDirPath = pathJoin(themeSrcDirPath, "email"); const emailThemeSrcDirPath = pathJoin(themeSrcDirPath, "email");
if (!fs.existsSync(emailThemeSrcDirPath)) { if (!fs.existsSync(emailThemeSrcDirPath)) {

View File

@ -5,74 +5,15 @@ import * as fs from "fs";
import type { ThemeType } from "../generateFtl"; import type { ThemeType } from "../generateFtl";
import { exclude } from "tsafe/exclude"; import { exclude } from "tsafe/exclude";
export function readFieldNameUsage(params: { /** Assumes the theme type exists */
keycloakifySrcDirPath: string; export function readFieldNameUsage(params: { keycloakifySrcDirPath: string; themeSrcDirPath: string; themeType: ThemeType }): string[] {
themeSrcDirPath: string | undefined;
themeType: ThemeType | "email";
}): string[] {
const { keycloakifySrcDirPath, themeSrcDirPath, themeType } = params; const { keycloakifySrcDirPath, themeSrcDirPath, themeType } = params;
const fieldNames: string[] = []; const fieldNames: string[] = [];
if (themeSrcDirPath === undefined) { for (const srcDirPath of ([pathJoin(keycloakifySrcDirPath, themeType), pathJoin(themeSrcDirPath, themeType)] as const).filter(
//If we can't detect the user theme directory we restore the fieldNames we had previously to prevent errors. exclude(undefined)
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))) {
const filePaths = crawl({ "dirPath": srcDirPath, "returnedPathsType": "absolute" }).filter(filePath => /\.(ts|tsx|js|jsx)$/.test(filePath)); const filePaths = crawl({ "dirPath": srcDirPath, "returnedPathsType": "absolute" }).filter(filePath => /\.(ts|tsx|js|jsx)$/.test(filePath));
for (const filePath of filePaths) { for (const filePath of filePaths) {

View File

@ -57,12 +57,6 @@ export async function main() {
"email": false "email": false
}; };
if (themeSrcDirPath === undefined) {
implementedThemeTypes["login"] = true;
implementedThemeTypes["account"] = true;
return implementedThemeTypes;
}
for (const themeType of objectKeys(implementedThemeTypes)) { for (const themeType of objectKeys(implementedThemeTypes)) {
if (!fs.existsSync(pathJoin(themeSrcDirPath, themeType))) { if (!fs.existsSync(pathJoin(themeSrcDirPath, themeType))) {
continue; continue;