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,131 +49,115 @@ 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 ) {
]; return { modifiedSourceCode: sourceCode };
const transformCodebase_common: Param0<
typeof transformCodebase
>["transformSourceCode"] = ({ fileRelativePath, sourceCode }) => {
if (metaInfKeycloakThemesJsonRelativePath === fileRelativePath) {
return { modifiedSourceCode: sourceCode };
}
for (const themeName of includingAccountV1ThemeNames) {
if (
isInside({
dirPath: pathJoin(
"src",
"main",
"resources",
"theme",
themeName
),
filePath: fileRelativePath
})
) {
return { modifiedSourceCode: sourceCode };
}
}
return undefined;
};
return { transformCodebase_common };
})();
const { transformCodebase_patchForUsingBuiltinAccountV1 } = (() => {
if (keycloakAccountV1Version !== null) {
return {
transformCodebase_patchForUsingBuiltinAccountV1: undefined
};
} }
const accountV1RelativeDirPath = pathJoin( for (const themeName of [...buildOptions.themeNames, accountV1ThemeName]) {
"src",
"main",
"resources",
"theme",
accountV1ThemeName
);
const transformCodebase_patchForUsingBuiltinAccountV1: Param0<
typeof transformCodebase
>["transformSourceCode"] = ({ fileRelativePath, sourceCode }) => {
if ( if (
isInside({ isInside({
dirPath: accountV1RelativeDirPath, dirPath: pathJoin("src", "main", "resources", "theme", themeName),
filePath: fileRelativePath filePath: fileRelativePath
}) })
) { ) {
return undefined; return { modifiedSourceCode: sourceCode };
} }
}
if (fileRelativePath === metaInfKeycloakThemesJsonRelativePath) { return undefined;
const keycloakThemesJsonParsed = JSON.parse( };
sourceCode.toString("utf8")
) as {
themes: { name: string; types: string[] }[];
};
keycloakThemesJsonParsed.themes = const transformCodebase_patchForUsingBuiltinAccountV1 =
keycloakThemesJsonParsed.themes.filter( keycloakAccountV1Version !== null
({ name }) => name !== accountV1ThemeName ? undefined
); : (params: {
fileRelativePath: string;
sourceCode: Buffer;
}): { modifiedSourceCode: Buffer } | undefined => {
const { fileRelativePath, sourceCode } = params;
return { if (
modifiedSourceCode: Buffer.from( isInside({
JSON.stringify(keycloakThemesJsonParsed, null, 2), dirPath: pathJoin(
"utf8" "src",
) "main",
}; "resources",
} "theme",
accountV1ThemeName
),
filePath: fileRelativePath
})
) {
return undefined;
}
for (const themeName of buildOptions.themeNames) { if (
if ( fileRelativePath ===
fileRelativePath === getMetaInfKeycloakThemesJsonFilePath({
pathJoin( keycloakifyBuildDirPath: "."
"src", })
"main", ) {
"resources", const keycloakThemesJsonParsed = JSON.parse(
"theme", sourceCode.toString("utf8")
themeName, ) as {
"account", themes: { name: string; types: string[] }[];
"theme.properties" };
)
) {
const modifiedSourceCode = Buffer.from(
sourceCode
.toString("utf8")
.replace(
`parent=${accountV1ThemeName}`,
"parent=keycloak"
),
"utf8"
);
assert(Buffer.compare(modifiedSourceCode, sourceCode) !== 0); keycloakThemesJsonParsed.themes =
keycloakThemesJsonParsed.themes.filter(
({ name }) => name !== accountV1ThemeName
);
return { modifiedSourceCode }; return {
} modifiedSourceCode: Buffer.from(
} JSON.stringify(keycloakThemesJsonParsed, null, 2),
"utf8"
)
};
}
return { modifiedSourceCode: sourceCode }; for (const themeName of buildOptions.themeNames) {
}; if (
fileRelativePath ===
pathJoin(
"src",
"main",
"resources",
"theme",
themeName,
"account",
"theme.properties"
)
) {
const modifiedSourceCode = Buffer.from(
sourceCode
.toString("utf8")
.replace(
`parent=${accountV1ThemeName}`,
"parent=keycloak"
),
"utf8"
);
return { transformCodebase_patchForUsingBuiltinAccountV1 }; assert(
})(); Buffer.compare(modifiedSourceCode, sourceCode) !== 0
);
console.log("Transforming codebase..."); return { modifiedSourceCode };
const start = Date.now(); }
}
return { modifiedSourceCode: sourceCode };
};
transformCodebase({ transformCodebase({
srcDirPath: buildOptions.keycloakifyBuildDirPath, srcDirPath: buildOptions.keycloakifyBuildDirPath,
@ -182,26 +165,22 @@ export async function buildJar(params: {
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",