Deprecate the extraPages options, analyze the code to detect extra pages

This commit is contained in:
garronej 2023-06-21 03:54:43 +02:00
parent cf9a7b8c60
commit de390678fd
9 changed files with 73 additions and 68 deletions

View File

@ -30,18 +30,6 @@
"type": "string" "type": "string"
} }
}, },
"extraLoginPages": {
"type": "array",
"items": {
"type": "string"
}
},
"extraAccountPages": {
"type": "array",
"items": {
"type": "string"
}
},
"extraThemeProperties": { "extraThemeProperties": {
"type": "array", "type": "array",
"items": { "items": {

View File

@ -10,7 +10,7 @@ export function getThemeSrcDirPath(params: { projectDirPath: string }) {
const srcDirPath = pathJoin(projectDirPath, "src"); const srcDirPath = pathJoin(projectDirPath, "src");
const themeSrcDirPath: string | undefined = crawl(srcDirPath) const themeSrcDirPath: string | undefined = crawl({ "dirPath": srcDirPath, "returnedPathsType": "relative to dirPath" })
.map(fileRelativePath => { .map(fileRelativePath => {
const split = fileRelativePath.split(themeSrcDirBasename); const split = fileRelativePath.split(themeSrcDirBasename);

View File

@ -17,8 +17,6 @@ export namespace BuildOptions {
themeVersion: string; themeVersion: string;
themeName: string; themeName: string;
extraThemeNames: string[]; extraThemeNames: string[];
extraLoginPages: string[] | undefined;
extraAccountPages: string[] | undefined;
extraThemeProperties: string[] | undefined; extraThemeProperties: string[] | undefined;
groupId: string; groupId: string;
artifactId: string; artifactId: string;
@ -108,17 +106,7 @@ export function readBuildOptions(params: { projectDirPath: string; processArgv:
const common: BuildOptions.Common = (() => { const common: BuildOptions.Common = (() => {
const { name, keycloakify = {}, version, homepage } = parsedPackageJson; const { name, keycloakify = {}, version, homepage } = parsedPackageJson;
const { const { extraThemeProperties, groupId, artifactId, bundler, keycloakVersionDefaultAssets, extraThemeNames = [] } = keycloakify ?? {};
extraPages,
extraLoginPages,
extraAccountPages,
extraThemeProperties,
groupId,
artifactId,
bundler,
keycloakVersionDefaultAssets,
extraThemeNames = []
} = keycloakify ?? {};
const themeName = const themeName =
keycloakify.themeName ?? keycloakify.themeName ??
@ -160,8 +148,6 @@ export function readBuildOptions(params: { projectDirPath: string; processArgv:
); );
})(), })(),
"themeVersion": process.env.KEYCLOAKIFY_THEME_VERSION ?? process.env.KEYCLOAKIFY_VERSION ?? version ?? "0.0.0", "themeVersion": process.env.KEYCLOAKIFY_THEME_VERSION ?? process.env.KEYCLOAKIFY_VERSION ?? version ?? "0.0.0",
"extraLoginPages": [...(extraPages ?? []), ...(extraLoginPages ?? [])],
extraAccountPages,
extraThemeProperties, extraThemeProperties,
"isSilent": isSilentCliParamProvided, "isSilent": isSilentCliParamProvided,
"keycloakVersionDefaultAssets": keycloakVersionDefaultAssets ?? "11.0.3", "keycloakVersionDefaultAssets": keycloakVersionDefaultAssets ?? "11.0.3",

View File

@ -10,14 +10,13 @@ import type { BuildOptions } from "../BuildOptions";
import { assert } from "tsafe/assert"; import { assert } from "tsafe/assert";
import { downloadKeycloakStaticResources } from "./downloadKeycloakStaticResources"; import { downloadKeycloakStaticResources } from "./downloadKeycloakStaticResources";
import { readFieldNameUsage } from "./readFieldNameUsage"; import { readFieldNameUsage } from "./readFieldNameUsage";
import { readExtraPagesNames } from "./readExtraPageNames";
export type BuildOptionsLike = BuildOptionsLike.Standalone | BuildOptionsLike.ExternalAssets; export type BuildOptionsLike = BuildOptionsLike.Standalone | BuildOptionsLike.ExternalAssets;
export namespace BuildOptionsLike { export namespace BuildOptionsLike {
export type Common = { export type Common = {
themeName: string; themeName: string;
extraLoginPages: string[] | undefined;
extraAccountPages: string[] | undefined;
extraThemeProperties: string[] | undefined; extraThemeProperties: string[] | undefined;
isSilent: boolean; isSilent: boolean;
themeVersion: string; themeVersion: string;
@ -163,14 +162,12 @@ export async function generateTheme(params: {
return accountThemePageIds; return accountThemePageIds;
} }
})(), })(),
...((() => { ...(themeSrcDirPath === undefined
switch (themeType) { ? []
case "login": : readExtraPagesNames({
return buildOptions.extraLoginPages; themeType,
case "account": themeSrcDirPath
return buildOptions.extraAccountPages; }))
}
})() ?? [])
].forEach(pageId => { ].forEach(pageId => {
const { ftlCode } = generateFtlFilesCode({ pageId }); const { ftlCode } = generateFtlFilesCode({ pageId });

View File

@ -0,0 +1,38 @@
import { crawl } from "../../tools/crawl";
import { type ThemeType, accountThemePageIds, loginThemePageIds } from "../generateFtl";
import { id } from "tsafe/id";
import { removeDuplicates } from "evt/tools/reducers/removeDuplicates";
import * as fs from "fs";
import { join as pathJoin } from "path";
export function readExtraPagesNames(params: { themeSrcDirPath: string; themeType: ThemeType }): string[] {
const { themeSrcDirPath, themeType } = params;
const filePaths = crawl({
"dirPath": pathJoin(themeSrcDirPath, themeType),
"returnedPathsType": "absolute"
}).filter(filePath => /\.(ts|tsx|js|jsx)$/.test(filePath));
const candidateFilePaths = filePaths.filter(filePath => /kcContext\.[^.]+$/.test(filePath));
if (candidateFilePaths.length === 0) {
candidateFilePaths.push(...filePaths);
}
const extraPages: string[] = [];
for (const candidateFilPath of candidateFilePaths) {
const rawSourceFile = fs.readFileSync(candidateFilPath).toString("utf8");
extraPages.push(...Array.from(rawSourceFile.matchAll(/["']?pageId["']?\s*:\s*["']([^.]+.ftl)["']/g), m => m[1]));
}
return extraPages.reduce(...removeDuplicates<string>()).filter(pageId => {
switch (themeType) {
case "account":
return !id<readonly string[]>(accountThemePageIds).includes(pageId);
case "login":
return !id<readonly string[]>(loginThemePageIds).includes(pageId);
}
});
}

View File

@ -73,9 +73,7 @@ export function readFieldNameUsage(params: {
})() })()
] as const ] as const
).filter(exclude(undefined))) { ).filter(exclude(undefined))) {
const filePaths = crawl(srcDirPath) const filePaths = crawl({ "dirPath": srcDirPath, "returnedPathsType": "absolute" }).filter(filePath => /\.(ts|tsx|js|jsx)$/.test(filePath));
.filter(filePath => /\.(ts|tsx|js|jsx)$/.test(filePath))
.map(filePath => pathJoin(srcDirPath, filePath));
for (const filePath of filePaths) { for (const filePath of filePaths) {
const rawSourceFile = fs.readFileSync(filePath).toString("utf8"); const rawSourceFile = fs.readFileSync(filePath).toString("utf8");

View File

@ -11,10 +11,6 @@ export type ParsedPackageJson = {
version?: string; version?: string;
homepage?: string; homepage?: string;
keycloakify?: { keycloakify?: {
/** @deprecated: use extraLoginPages instead */
extraPages?: string[];
extraLoginPages?: string[];
extraAccountPages?: string[];
extraThemeProperties?: string[]; extraThemeProperties?: string[];
areAppAndKeycloakServerSharingSameDomain?: boolean; areAppAndKeycloakServerSharingSameDomain?: boolean;
artifactId?: string; artifactId?: string;
@ -34,9 +30,6 @@ export const zParsedPackageJson = z.object({
"homepage": z.string().optional(), "homepage": z.string().optional(),
"keycloakify": z "keycloakify": z
.object({ .object({
"extraPages": z.array(z.string()).optional(),
"extraLoginPages": z.array(z.string()).optional(),
"extraAccountPages": z.array(z.string()).optional(),
"extraThemeProperties": z.array(z.string()).optional(), "extraThemeProperties": z.array(z.string()).optional(),
"areAppAndKeycloakServerSharingSameDomain": z.boolean().optional(), "areAppAndKeycloakServerSharingSameDomain": z.boolean().optional(),
"artifactId": z.string().optional(), "artifactId": z.string().optional(),

View File

@ -1,9 +1,7 @@
import * as fs from "fs"; import * as fs from "fs";
import * as path from "path"; import * as path from "path";
/** List all files in a given directory return paths relative to the dir_path */ const crawlRec = (dir_path: string, paths: string[]) => {
export const crawl = (() => {
const crawlRec = (dir_path: string, paths: string[]) => {
for (const file_name of fs.readdirSync(dir_path)) { for (const file_name of fs.readdirSync(dir_path)) {
const file_path = path.join(dir_path, file_name); const file_path = path.join(dir_path, file_name);
@ -15,13 +13,20 @@ export const crawl = (() => {
paths.push(file_path); paths.push(file_path);
} }
}; };
return function crawl(dir_path: string): string[] { /** List all files in a given directory return paths relative to the dir_path */
const paths: string[] = []; export function crawl(params: { dirPath: string; returnedPathsType: "absolute" | "relative to dirPath" }): string[] {
const { dirPath, returnedPathsType } = params;
crawlRec(dir_path, paths); const filePaths: string[] = [];
return paths.map(file_path => path.relative(dir_path, file_path)); crawlRec(dirPath, filePaths);
};
})(); switch (returnedPathsType) {
case "absolute":
return filePaths;
case "relative to dirPath":
return filePaths.map(filePath => path.relative(dirPath, filePath));
}
}

View File

@ -20,12 +20,12 @@ export function transformCodebase(params: { srcDirPath: string; destDirPath: str
})) }))
} = params; } = params;
for (const file_relative_path of crawl(srcDirPath)) { for (const file_relative_path of crawl({ "dirPath": srcDirPath, "returnedPathsType": "relative to dirPath" })) {
const filePath = path.join(srcDirPath, file_relative_path); const filePath = path.join(srcDirPath, file_relative_path);
const transformSourceCodeResult = transformSourceCode({ const transformSourceCodeResult = transformSourceCode({
"sourceCode": fs.readFileSync(filePath), "sourceCode": fs.readFileSync(filePath),
"filePath": path.join(srcDirPath, file_relative_path) filePath
}); });
if (transformSourceCodeResult === undefined) { if (transformSourceCodeResult === undefined) {