Fix hot reloading when testing in keycloak

This commit is contained in:
Joseph Garrone 2024-05-25 22:36:03 +02:00
parent 74fed835e8
commit c28caaa495

View File

@ -12,12 +12,7 @@ import type { KeycloakVersionRange } from "../shared/KeycloakVersionRange";
import { getJarFileBasename } from "../shared/getJarFileBasename"; import { getJarFileBasename } from "../shared/getJarFileBasename";
import { assert, type Equals } from "tsafe/assert"; import { assert, type Equals } from "tsafe/assert";
import * as fs from "fs"; import * as fs from "fs";
import { import { join as pathJoin, relative as pathRelative, sep as pathSep } from "path";
join as pathJoin,
relative as pathRelative,
sep as pathSep,
posix as pathPosix
} from "path";
import * as child_process from "child_process"; import * as child_process from "child_process";
import chalk from "chalk"; import chalk from "chalk";
import chokidar from "chokidar"; import chokidar from "chokidar";
@ -27,6 +22,7 @@ import { Deferred } from "evt/tools/Deferred";
import { getAbsoluteAndInOsFormatPath } from "../tools/getAbsoluteAndInOsFormatPath"; import { getAbsoluteAndInOsFormatPath } from "../tools/getAbsoluteAndInOsFormatPath";
import cliSelect from "cli-select"; import cliSelect from "cli-select";
import * as runExclusive from "run-exclusive"; import * as runExclusive from "run-exclusive";
import { extractArchive } from "../tools/extractArchive";
export type CliCommandOptions = CliCommandOptions_common & { export type CliCommandOptions = CliCommandOptions_common & {
port: number; port: number;
@ -195,52 +191,6 @@ export async function command(params: { cliCommandOptions: CliCommandOptions })
console.log(`Using Keycloak ${chalk.bold(jarFileBasename)}`); console.log(`Using Keycloak ${chalk.bold(jarFileBasename)}`);
const mountTargets = buildOptions.themeNames
.map(themeName => {
const themeEntry = metaInfKeycloakThemes.themes.find(
({ name }) => name === themeName
);
assert(themeEntry !== undefined);
return themeEntry.types
.map(themeType => {
const localPathDirname = pathJoin(
buildOptions.keycloakifyBuildDirPath,
"src",
"main",
"resources",
"theme",
themeName,
themeType
);
return fs
.readdirSync(localPathDirname)
.filter(
fileOrDirectoryBasename =>
!fileOrDirectoryBasename.endsWith(".properties")
)
.map(fileOrDirectoryBasename => ({
localPath: pathJoin(
localPathDirname,
fileOrDirectoryBasename
),
containerPath: pathPosix.join(
"/",
"opt",
"keycloak",
"themes",
themeName,
themeType,
fileOrDirectoryBasename
)
}));
})
.flat();
})
.flat();
try { try {
child_process.execSync(`docker rm --force ${containerName}`, { child_process.execSync(`docker rm --force ${containerName}`, {
stdio: "ignore" stdio: "ignore"
@ -303,6 +253,43 @@ export async function command(params: { cliCommandOptions: CliCommandOptions })
return pathJoin(dirPath, value); return pathJoin(dirPath, value);
})(); })();
const jarFilePath = pathJoin(buildOptions.keycloakifyBuildDirPath, jarFileBasename);
let doLinkAccountV1Theme = false;
await extractArchive({
archiveFilePath: jarFilePath,
onArchiveFile: async ({ relativeFilePathInArchive, readFile, writeFile }) => {
for (const themeName of buildOptions.themeNames) {
if (
relativeFilePathInArchive ===
pathJoin("theme", themeName, "account", "theme.properties")
) {
if (
(await readFile())
.toString("utf8")
.includes(`parent=${accountV1ThemeName}`)
) {
doLinkAccountV1Theme = true;
}
await writeFile({
filePath: pathJoin(
buildOptions.keycloakifyBuildDirPath,
"src",
"main",
"resources",
"theme",
themeName,
"account",
"theme.properties"
)
});
}
}
}
});
const spawnArgs = [ const spawnArgs = [
"docker", "docker",
[ [
@ -317,20 +304,28 @@ export async function command(params: { cliCommandOptions: CliCommandOptions })
"-v", "-v",
`${realmJsonFilePath}:/opt/keycloak/data/import/myrealm-realm.json` `${realmJsonFilePath}:/opt/keycloak/data/import/myrealm-realm.json`
]), ]),
...[ ...["-v", `${jarFilePath}:/opt/keycloak/providers/keycloak-theme.jar`],
"-v",
`${pathJoin(
buildOptions.keycloakifyBuildDirPath,
jarFileBasename
)}:/opt/keycloak/providers/keycloak-theme.jar`
],
...(keycloakMajorVersionNumber <= 20 ...(keycloakMajorVersionNumber <= 20
? ["-e", "JAVA_OPTS=-Dkeycloak.profile=preview"] ? ["-e", "JAVA_OPTS=-Dkeycloak.profile=preview"]
: []), : []),
...mountTargets ...[
.map(({ localPath, containerPath }) => [ ...buildOptions.themeNames,
...(doLinkAccountV1Theme ? [accountV1ThemeName] : [])
]
.map(themeName => ({
localDirPath: pathJoin(
buildOptions.keycloakifyBuildDirPath,
"src",
"main",
"resources",
"theme",
themeName
),
containerDirPath: `/opt/keycloak/themes/${themeName}`
}))
.map(({ localDirPath, containerDirPath }) => [
"-v", "-v",
`${localPath}:${containerPath}:rw` `${localDirPath}:${containerDirPath}:rw`
]) ])
.flat(), .flat(),
`quay.io/keycloak/keycloak:${keycloakVersion}`, `quay.io/keycloak/keycloak:${keycloakVersion}`,
@ -345,8 +340,6 @@ export async function command(params: { cliCommandOptions: CliCommandOptions })
} }
] as const; ] as const;
console.log(JSON.stringify(spawnArgs, null, 2));
const child = child_process.spawn(...spawnArgs); const child = child_process.spawn(...spawnArgs);
child.stdout.on("data", data => process.stdout.write(data)); child.stdout.on("data", data => process.stdout.write(data));