Readability improvement

This commit is contained in:
Joseph Garrone 2024-05-26 12:38:00 +02:00
parent 6603852355
commit 9a5ac5f13f
2 changed files with 97 additions and 118 deletions

View File

@ -17,7 +17,6 @@ import { isInside } from "../../tools/isInside";
import child_process from "child_process"; import child_process from "child_process";
import { rmSync } from "../../tools/fs.rmSync"; import { rmSync } from "../../tools/fs.rmSync";
import { getMetaInfKeycloakThemesJsonFilePath } from "../../shared/metaInfKeycloakThemes"; import { getMetaInfKeycloakThemesJsonFilePath } from "../../shared/metaInfKeycloakThemes";
import type { Param0 } from "tsafe";
export type BuildOptionsLike = BuildOptionsLike_generatePom & { export type BuildOptionsLike = BuildOptionsLike_generatePom & {
keycloakifyBuildDirPath: string; keycloakifyBuildDirPath: string;
@ -50,25 +49,42 @@ export async function buildJar(params: {
rmSync(keycloakifyBuildTmpDirPath, { recursive: true, force: true }); rmSync(keycloakifyBuildTmpDirPath, { recursive: true, force: true });
{ {
const metaInfKeycloakThemesJsonRelativePath = const transformCodebase_common = (params: {
getMetaInfKeycloakThemesJsonFilePath({ fileRelativePath: string;
keycloakifyBuildDirPath: "" sourceCode: Buffer;
}); }): { modifiedSourceCode: Buffer } | undefined => {
const { fileRelativePath, sourceCode } = params;
const { transformCodebase_common } = (() => { if (
const includingAccountV1ThemeNames = [ fileRelativePath ===
...buildOptions.themeNames, getMetaInfKeycloakThemesJsonFilePath({ keycloakifyBuildDirPath: "." })
accountV1ThemeName ) {
];
const transformCodebase_common: Param0<
typeof transformCodebase
>["transformSourceCode"] = ({ fileRelativePath, sourceCode }) => {
if (metaInfKeycloakThemesJsonRelativePath === fileRelativePath) {
return { modifiedSourceCode: sourceCode }; return { modifiedSourceCode: sourceCode };
} }
for (const themeName of includingAccountV1ThemeNames) { for (const themeName of [...buildOptions.themeNames, accountV1ThemeName]) {
if (
isInside({
dirPath: pathJoin("src", "main", "resources", "theme", themeName),
filePath: fileRelativePath
})
) {
return { modifiedSourceCode: sourceCode };
}
}
return undefined;
};
const transformCodebase_patchForUsingBuiltinAccountV1 =
keycloakAccountV1Version !== null
? undefined
: (params: {
fileRelativePath: string;
sourceCode: Buffer;
}): { modifiedSourceCode: Buffer } | undefined => {
const { fileRelativePath, sourceCode } = params;
if ( if (
isInside({ isInside({
dirPath: pathJoin( dirPath: pathJoin(
@ -76,49 +92,20 @@ export async function buildJar(params: {
"main", "main",
"resources", "resources",
"theme", "theme",
themeName accountV1ThemeName
), ),
filePath: fileRelativePath filePath: fileRelativePath
}) })
) { ) {
return { modifiedSourceCode: sourceCode };
}
}
return undefined; return undefined;
};
return { transformCodebase_common };
})();
const { transformCodebase_patchForUsingBuiltinAccountV1 } = (() => {
if (keycloakAccountV1Version !== null) {
return {
transformCodebase_patchForUsingBuiltinAccountV1: undefined
};
} }
const accountV1RelativeDirPath = pathJoin(
"src",
"main",
"resources",
"theme",
accountV1ThemeName
);
const transformCodebase_patchForUsingBuiltinAccountV1: Param0<
typeof transformCodebase
>["transformSourceCode"] = ({ fileRelativePath, sourceCode }) => {
if ( if (
isInside({ fileRelativePath ===
dirPath: accountV1RelativeDirPath, getMetaInfKeycloakThemesJsonFilePath({
filePath: fileRelativePath keycloakifyBuildDirPath: "."
}) })
) { ) {
return undefined;
}
if (fileRelativePath === metaInfKeycloakThemesJsonRelativePath) {
const keycloakThemesJsonParsed = JSON.parse( const keycloakThemesJsonParsed = JSON.parse(
sourceCode.toString("utf8") sourceCode.toString("utf8")
) as { ) as {
@ -161,7 +148,9 @@ export async function buildJar(params: {
"utf8" "utf8"
); );
assert(Buffer.compare(modifiedSourceCode, sourceCode) !== 0); assert(
Buffer.compare(modifiedSourceCode, sourceCode) !== 0
);
return { modifiedSourceCode }; return { modifiedSourceCode };
} }
@ -170,38 +159,28 @@ export async function buildJar(params: {
return { modifiedSourceCode: sourceCode }; return { modifiedSourceCode: sourceCode };
}; };
return { transformCodebase_patchForUsingBuiltinAccountV1 };
})();
console.log("Transforming codebase...");
const start = Date.now();
transformCodebase({ transformCodebase({
srcDirPath: buildOptions.keycloakifyBuildDirPath, srcDirPath: buildOptions.keycloakifyBuildDirPath,
destDirPath: keycloakifyBuildTmpDirPath, destDirPath: keycloakifyBuildTmpDirPath,
transformSourceCode: params => { transformSourceCode: params => {
const resultCommon = transformCodebase_common(params); const resultCommon = transformCodebase_common(params);
if (resultCommon === undefined) {
return undefined;
}
if (transformCodebase_patchForUsingBuiltinAccountV1 === undefined) { if (transformCodebase_patchForUsingBuiltinAccountV1 === undefined) {
return resultCommon; return resultCommon;
} }
const { modifiedSourceCode, newFileName } = resultCommon; if (resultCommon === undefined) {
return undefined;
}
assert(newFileName === undefined); const { modifiedSourceCode } = resultCommon;
return transformCodebase_patchForUsingBuiltinAccountV1?.({ return transformCodebase_patchForUsingBuiltinAccountV1({
...params, ...params,
sourceCode: modifiedSourceCode sourceCode: modifiedSourceCode
}); });
} }
}); });
console.log(`Transforming codebase done in ${Date.now() - start}ms`);
} }
route_legacy_pages: { route_legacy_pages: {

View File

@ -12,7 +12,7 @@ export function getMetaInfKeycloakThemesJsonFilePath(params: {
const { keycloakifyBuildDirPath } = params; const { keycloakifyBuildDirPath } = params;
return pathJoin( return pathJoin(
keycloakifyBuildDirPath, keycloakifyBuildDirPath === "." ? "" : keycloakifyBuildDirPath,
"src", "src",
"main", "main",
"resources", "resources",