Add users to provided realm configuration if none exists

This commit is contained in:
Joseph Garrone 2024-08-25 19:02:00 +02:00
parent dcaee9cb7f
commit b70c0af0a9

View File

@ -10,7 +10,8 @@ import {
join as pathJoin, join as pathJoin,
relative as pathRelative, relative as pathRelative,
sep as pathSep, sep as pathSep,
basename as pathBasename basename as pathBasename,
dirname as pathDirname
} from "path"; } from "path";
import * as child_process from "child_process"; import * as child_process from "child_process";
import chalk from "chalk"; import chalk from "chalk";
@ -211,6 +212,17 @@ export async function command(params: { cliCommandOptions: CliCommandOptions })
}) })
); );
const getRealmJsonFilePath_defaultForKeycloakMajor = (
keycloakMajorVersionNumber: number
) =>
pathJoin(
getThisCodebaseRootDirPath(),
"src",
"bin",
"start-keycloak",
`myrealm-realm-${keycloakMajorVersionNumber}.json`
);
const realmJsonFilePath = await (async () => { const realmJsonFilePath = await (async () => {
if (cliCommandOptions.realmJsonFilePath !== undefined) { if (cliCommandOptions.realmJsonFilePath !== undefined) {
if (cliCommandOptions.realmJsonFilePath === "none") { if (cliCommandOptions.realmJsonFilePath === "none") {
@ -231,20 +243,12 @@ export async function command(params: { cliCommandOptions: CliCommandOptions })
} }
const internalFilePath = await (async () => { const internalFilePath = await (async () => {
const dirPath = pathJoin( const defaultFilePath = getRealmJsonFilePath_defaultForKeycloakMajor(
getThisCodebaseRootDirPath(), keycloakMajorVersionNumber
"src",
"bin",
"start-keycloak"
); );
const filePath = pathJoin( if (fs.existsSync(defaultFilePath)) {
dirPath, return defaultFilePath;
`myrealm-realm-${keycloakMajorVersionNumber}.json`
);
if (fs.existsSync(filePath)) {
return filePath;
} }
console.log( console.log(
@ -255,6 +259,8 @@ export async function command(params: { cliCommandOptions: CliCommandOptions })
console.log(chalk.cyan("Select what configuration to use:")); console.log(chalk.cyan("Select what configuration to use:"));
const dirPath = pathDirname(defaultFilePath);
const { value } = await cliSelect<string>({ const { value } = await cliSelect<string>({
values: [ values: [
...fs ...fs
@ -296,6 +302,40 @@ export async function command(params: { cliCommandOptions: CliCommandOptions })
return filePath; return filePath;
})(); })();
add_test_user_if_missing: {
if (realmJsonFilePath === undefined) {
break add_test_user_if_missing;
}
const realm: Record<string, unknown> = JSON.parse(
fs.readFileSync(realmJsonFilePath).toString("utf8")
);
if (realm.users !== undefined) {
break add_test_user_if_missing;
}
const realmJsonFilePath_internal = (() => {
const filePath = getRealmJsonFilePath_defaultForKeycloakMajor(
keycloakMajorVersionNumber
);
if (!fs.existsSync(filePath)) {
return getRealmJsonFilePath_defaultForKeycloakMajor(25);
}
return filePath;
})();
const users = JSON.parse(
fs.readFileSync(realmJsonFilePath_internal).toString("utf8")
).users;
realm.users = users;
fs.writeFileSync(realmJsonFilePath, JSON.stringify(realm, null, 2), "utf8");
}
async function extractThemeResourcesFromJar() { async function extractThemeResourcesFromJar() {
await extractArchive({ await extractArchive({
archiveFilePath: jarFilePath, archiveFilePath: jarFilePath,