2024-05-15 05:14:01 +02:00
|
|
|
import { join as pathJoin } from "path";
|
|
|
|
import { downloadAndUnzip } from "./downloadAndUnzip";
|
|
|
|
import { type BuildOptions } from "./buildOptions";
|
|
|
|
import { assert } from "tsafe/assert";
|
|
|
|
import * as child_process from "child_process";
|
|
|
|
import * as fs from "fs";
|
|
|
|
import { rmSync } from "../tools/fs.rmSync";
|
2024-05-19 10:46:26 +02:00
|
|
|
import { lastKeycloakVersionWithAccountV1 } from "./constants";
|
2024-05-15 05:14:01 +02:00
|
|
|
import { transformCodebase } from "../tools/transformCodebase";
|
|
|
|
|
|
|
|
export type BuildOptionsLike = {
|
|
|
|
cacheDirPath: string;
|
|
|
|
npmWorkspaceRootDirPath: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
assert<BuildOptions extends BuildOptionsLike ? true : false>();
|
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
export async function downloadKeycloakDefaultTheme(params: {
|
|
|
|
keycloakVersion: string;
|
|
|
|
destDirPath: string;
|
|
|
|
buildOptions: BuildOptionsLike;
|
|
|
|
}) {
|
2024-05-15 05:14:01 +02:00
|
|
|
const { keycloakVersion, destDirPath, buildOptions } = params;
|
|
|
|
|
|
|
|
await downloadAndUnzip({
|
|
|
|
destDirPath,
|
2024-05-20 15:48:51 +02:00
|
|
|
url: `https://github.com/keycloak/keycloak/archive/refs/tags/${keycloakVersion}.zip`,
|
|
|
|
specificDirsToExtract: ["", "-community"].map(
|
|
|
|
ext => `keycloak-${keycloakVersion}/themes/src/main/resources${ext}/theme`
|
|
|
|
),
|
2024-05-15 05:14:01 +02:00
|
|
|
buildOptions,
|
2024-05-20 15:48:51 +02:00
|
|
|
preCacheTransform: {
|
|
|
|
actionCacheId: "npm install and build",
|
|
|
|
action: async ({ destDirPath }) => {
|
2024-05-15 05:14:01 +02:00
|
|
|
install_common_node_modules: {
|
2024-05-20 15:48:51 +02:00
|
|
|
const commonResourcesDirPath = pathJoin(
|
|
|
|
destDirPath,
|
|
|
|
"keycloak",
|
|
|
|
"common",
|
|
|
|
"resources"
|
|
|
|
);
|
2024-05-15 05:14:01 +02:00
|
|
|
|
|
|
|
if (!fs.existsSync(commonResourcesDirPath)) {
|
|
|
|
break install_common_node_modules;
|
|
|
|
}
|
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
if (
|
|
|
|
!fs.existsSync(pathJoin(commonResourcesDirPath, "package.json"))
|
|
|
|
) {
|
2024-05-15 05:14:01 +02:00
|
|
|
break install_common_node_modules;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fs.existsSync(pathJoin(commonResourcesDirPath, "node_modules"))) {
|
|
|
|
break install_common_node_modules;
|
|
|
|
}
|
|
|
|
|
|
|
|
child_process.execSync("npm install --omit=dev", {
|
2024-05-20 15:48:51 +02:00
|
|
|
cwd: commonResourcesDirPath,
|
|
|
|
stdio: "ignore"
|
2024-05-15 05:14:01 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
repatriate_common_resources_from_base_login_theme: {
|
2024-05-20 15:48:51 +02:00
|
|
|
const baseLoginThemeResourceDir = pathJoin(
|
|
|
|
destDirPath,
|
|
|
|
"base",
|
|
|
|
"login",
|
|
|
|
"resources"
|
|
|
|
);
|
2024-05-15 05:14:01 +02:00
|
|
|
|
|
|
|
if (!fs.existsSync(baseLoginThemeResourceDir)) {
|
|
|
|
break repatriate_common_resources_from_base_login_theme;
|
|
|
|
}
|
|
|
|
|
|
|
|
transformCodebase({
|
2024-05-20 15:48:51 +02:00
|
|
|
srcDirPath: baseLoginThemeResourceDir,
|
|
|
|
destDirPath: pathJoin(
|
|
|
|
destDirPath,
|
|
|
|
"keycloak",
|
|
|
|
"login",
|
|
|
|
"resources"
|
|
|
|
)
|
2024-05-15 05:14:01 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
install_and_move_to_common_resources_generated_in_keycloak_v2: {
|
2024-05-20 15:48:51 +02:00
|
|
|
if (
|
|
|
|
!fs
|
|
|
|
.readFileSync(
|
|
|
|
pathJoin(
|
|
|
|
destDirPath,
|
|
|
|
"keycloak",
|
|
|
|
"login",
|
|
|
|
"theme.properties"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.toString("utf8")
|
|
|
|
.includes("web_modules")
|
|
|
|
) {
|
2024-05-15 05:14:01 +02:00
|
|
|
break install_and_move_to_common_resources_generated_in_keycloak_v2;
|
|
|
|
}
|
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
const accountV2DirSrcDirPath = pathJoin(
|
|
|
|
destDirPath,
|
|
|
|
"keycloak.v2",
|
|
|
|
"account",
|
|
|
|
"src"
|
|
|
|
);
|
2024-05-15 05:14:01 +02:00
|
|
|
|
|
|
|
if (!fs.existsSync(accountV2DirSrcDirPath)) {
|
|
|
|
break install_and_move_to_common_resources_generated_in_keycloak_v2;
|
|
|
|
}
|
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
const packageManager = fs.existsSync(
|
|
|
|
pathJoin(accountV2DirSrcDirPath, "pnpm-lock.yaml")
|
|
|
|
)
|
|
|
|
? "pnpm"
|
|
|
|
: "npm";
|
2024-05-15 05:14:01 +02:00
|
|
|
|
|
|
|
if (packageManager === "pnpm") {
|
|
|
|
try {
|
|
|
|
child_process.execSync(`which pnpm`);
|
|
|
|
} catch {
|
|
|
|
console.log(`Installing pnpm globally`);
|
|
|
|
child_process.execSync(`npm install -g pnpm`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
child_process.execSync(`${packageManager} install`, {
|
|
|
|
cwd: accountV2DirSrcDirPath,
|
|
|
|
stdio: "ignore"
|
|
|
|
});
|
2024-05-15 05:14:01 +02:00
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
const packageJsonFilePath = pathJoin(
|
|
|
|
accountV2DirSrcDirPath,
|
|
|
|
"package.json"
|
|
|
|
);
|
2024-05-15 05:14:01 +02:00
|
|
|
|
|
|
|
const packageJsonRaw = fs.readFileSync(packageJsonFilePath);
|
|
|
|
|
|
|
|
const parsedPackageJson = JSON.parse(packageJsonRaw.toString("utf8"));
|
|
|
|
|
|
|
|
parsedPackageJson.scripts.build = parsedPackageJson.scripts.build
|
|
|
|
.replace(`${packageManager} run check-types`, "true")
|
|
|
|
.replace(`${packageManager} run babel`, "true");
|
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
fs.writeFileSync(
|
|
|
|
packageJsonFilePath,
|
|
|
|
Buffer.from(JSON.stringify(parsedPackageJson, null, 2), "utf8")
|
|
|
|
);
|
2024-05-15 05:14:01 +02:00
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
child_process.execSync(`${packageManager} run build`, {
|
|
|
|
cwd: accountV2DirSrcDirPath,
|
|
|
|
stdio: "ignore"
|
|
|
|
});
|
2024-05-15 05:14:01 +02:00
|
|
|
|
|
|
|
fs.writeFileSync(packageJsonFilePath, packageJsonRaw);
|
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
fs.rmSync(pathJoin(accountV2DirSrcDirPath, "node_modules"), {
|
|
|
|
recursive: true
|
|
|
|
});
|
2024-05-15 05:14:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
remove_keycloak_v2: {
|
|
|
|
const keycloakV2DirPath = pathJoin(destDirPath, "keycloak.v2");
|
|
|
|
|
|
|
|
if (!fs.existsSync(keycloakV2DirPath)) {
|
|
|
|
break remove_keycloak_v2;
|
|
|
|
}
|
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
rmSync(keycloakV2DirPath, { recursive: true });
|
2024-05-15 05:14:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Note, this is an optimization for reducing the size of the jar
|
|
|
|
remove_unused_node_modules: {
|
2024-05-20 15:48:51 +02:00
|
|
|
const nodeModuleDirPath = pathJoin(
|
|
|
|
destDirPath,
|
|
|
|
"keycloak",
|
|
|
|
"common",
|
|
|
|
"resources",
|
|
|
|
"node_modules"
|
|
|
|
);
|
2024-05-15 05:14:01 +02:00
|
|
|
|
|
|
|
if (!fs.existsSync(nodeModuleDirPath)) {
|
|
|
|
break remove_unused_node_modules;
|
|
|
|
}
|
|
|
|
|
|
|
|
const toDeletePerfixes = [
|
|
|
|
"angular",
|
|
|
|
"bootstrap",
|
|
|
|
"rcue",
|
|
|
|
"font-awesome",
|
|
|
|
"ng-file-upload",
|
|
|
|
pathJoin("patternfly", "dist", "sass"),
|
|
|
|
pathJoin("patternfly", "dist", "less"),
|
|
|
|
pathJoin("patternfly", "dist", "js"),
|
|
|
|
"d3",
|
|
|
|
pathJoin("jquery", "src"),
|
|
|
|
"c3",
|
|
|
|
"core-js",
|
|
|
|
"eonasdan-bootstrap-datetimepicker",
|
|
|
|
"moment",
|
|
|
|
"react",
|
|
|
|
"patternfly-bootstrap-treeview",
|
|
|
|
"popper.js",
|
|
|
|
"tippy.js",
|
|
|
|
"jquery-match-height",
|
|
|
|
"google-code-prettify",
|
|
|
|
"patternfly-bootstrap-combobox",
|
|
|
|
"focus-trap",
|
|
|
|
"tabbable",
|
|
|
|
"scheduler",
|
|
|
|
"@types",
|
|
|
|
"datatables.net",
|
|
|
|
"datatables.net-colreorder",
|
|
|
|
"tslib",
|
|
|
|
"prop-types",
|
|
|
|
"file-selector",
|
|
|
|
"datatables.net-colreorder-bs",
|
|
|
|
"object-assign",
|
|
|
|
"warning",
|
|
|
|
"js-tokens",
|
|
|
|
"loose-envify",
|
|
|
|
"prop-types-extra",
|
|
|
|
"attr-accept",
|
|
|
|
"datatables.net-select",
|
|
|
|
"drmonty-datatables-colvis",
|
|
|
|
"datatables.net-bs",
|
|
|
|
pathJoin("@patternfly", "react"),
|
|
|
|
pathJoin("@patternfly", "patternfly", "docs")
|
|
|
|
];
|
|
|
|
|
|
|
|
transformCodebase({
|
2024-05-20 15:48:51 +02:00
|
|
|
srcDirPath: nodeModuleDirPath,
|
|
|
|
destDirPath: nodeModuleDirPath,
|
|
|
|
transformSourceCode: ({ sourceCode, fileRelativePath }) => {
|
2024-05-15 05:14:01 +02:00
|
|
|
if (fileRelativePath.endsWith(".map")) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
if (
|
|
|
|
toDeletePerfixes.find(prefix =>
|
|
|
|
fileRelativePath.startsWith(prefix)
|
|
|
|
) !== undefined
|
|
|
|
) {
|
2024-05-15 05:14:01 +02:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
if (
|
|
|
|
fileRelativePath.startsWith(
|
|
|
|
pathJoin("patternfly", "dist", "fonts")
|
|
|
|
)
|
|
|
|
) {
|
2024-05-15 05:14:01 +02:00
|
|
|
if (
|
|
|
|
!fileRelativePath.endsWith(".woff2") &&
|
|
|
|
!fileRelativePath.endsWith(".woff") &&
|
|
|
|
!fileRelativePath.endsWith(".ttf")
|
|
|
|
) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
return { modifiedSourceCode: sourceCode };
|
2024-05-15 05:14:01 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just like node_modules
|
|
|
|
remove_unused_lib: {
|
2024-05-20 15:48:51 +02:00
|
|
|
const libDirPath = pathJoin(
|
|
|
|
destDirPath,
|
|
|
|
"keycloak",
|
|
|
|
"common",
|
|
|
|
"resources",
|
|
|
|
"lib"
|
|
|
|
);
|
2024-05-15 05:14:01 +02:00
|
|
|
|
|
|
|
if (!fs.existsSync(libDirPath)) {
|
|
|
|
break remove_unused_lib;
|
|
|
|
}
|
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
const toDeletePerfixes = [
|
|
|
|
"ui-ace",
|
|
|
|
"filesaver",
|
|
|
|
"fileupload",
|
|
|
|
"angular",
|
|
|
|
"ui-ace"
|
|
|
|
];
|
2024-05-15 05:14:01 +02:00
|
|
|
|
|
|
|
transformCodebase({
|
2024-05-20 15:48:51 +02:00
|
|
|
srcDirPath: libDirPath,
|
|
|
|
destDirPath: libDirPath,
|
|
|
|
transformSourceCode: ({ sourceCode, fileRelativePath }) => {
|
2024-05-15 05:14:01 +02:00
|
|
|
if (fileRelativePath.endsWith(".map")) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
if (
|
|
|
|
toDeletePerfixes.find(prefix =>
|
|
|
|
fileRelativePath.startsWith(prefix)
|
|
|
|
) !== undefined
|
|
|
|
) {
|
2024-05-15 05:14:01 +02:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
return { modifiedSourceCode: sourceCode };
|
2024-05-15 05:14:01 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
last_account_v1_transformations: {
|
|
|
|
if (lastKeycloakVersionWithAccountV1 !== keycloakVersion) {
|
|
|
|
break last_account_v1_transformations;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2024-05-20 15:48:51 +02:00
|
|
|
const accountCssFilePath = pathJoin(
|
|
|
|
destDirPath,
|
|
|
|
"keycloak",
|
|
|
|
"account",
|
|
|
|
"resources",
|
|
|
|
"css",
|
|
|
|
"account.css"
|
|
|
|
);
|
2024-05-15 05:14:01 +02:00
|
|
|
|
|
|
|
fs.writeFileSync(
|
|
|
|
accountCssFilePath,
|
2024-05-20 15:48:51 +02:00
|
|
|
Buffer.from(
|
|
|
|
fs
|
|
|
|
.readFileSync(accountCssFilePath)
|
|
|
|
.toString("utf8")
|
|
|
|
.replace("top: -34px;", "top: -34px !important;"),
|
|
|
|
"utf8"
|
|
|
|
)
|
2024-05-15 05:14:01 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note, this is an optimization for reducing the size of the jar,
|
|
|
|
// For this version we know exactly which resources are used.
|
|
|
|
{
|
2024-05-20 15:48:51 +02:00
|
|
|
const nodeModulesDirPath = pathJoin(
|
|
|
|
destDirPath,
|
|
|
|
"keycloak",
|
|
|
|
"common",
|
|
|
|
"resources",
|
|
|
|
"node_modules"
|
|
|
|
);
|
2024-05-15 05:14:01 +02:00
|
|
|
|
|
|
|
const toKeepPrefixes = [
|
2024-05-20 15:48:51 +02:00
|
|
|
...[
|
|
|
|
"patternfly.min.css",
|
|
|
|
"patternfly-additions.min.css",
|
|
|
|
"patternfly-additions.min.css"
|
|
|
|
].map(fileBasename =>
|
2024-05-15 05:14:01 +02:00
|
|
|
pathJoin("patternfly", "dist", "css", fileBasename)
|
|
|
|
),
|
|
|
|
pathJoin("patternfly", "dist", "fonts")
|
|
|
|
];
|
|
|
|
|
|
|
|
transformCodebase({
|
2024-05-20 15:48:51 +02:00
|
|
|
srcDirPath: nodeModulesDirPath,
|
|
|
|
destDirPath: nodeModulesDirPath,
|
|
|
|
transformSourceCode: ({ sourceCode, fileRelativePath }) => {
|
|
|
|
if (
|
|
|
|
toKeepPrefixes.find(prefix =>
|
|
|
|
fileRelativePath.startsWith(prefix)
|
|
|
|
) === undefined
|
|
|
|
) {
|
2024-05-15 05:14:01 +02:00
|
|
|
return undefined;
|
|
|
|
}
|
2024-05-20 15:48:51 +02:00
|
|
|
return { modifiedSourceCode: sourceCode };
|
2024-05-15 05:14:01 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|