diff --git a/src/bin/start-keycloak/realmConfig/realmConfig.ts b/src/bin/start-keycloak/realmConfig/realmConfig.ts index cd7e689e..f704d69d 100644 --- a/src/bin/start-keycloak/realmConfig/realmConfig.ts +++ b/src/bin/start-keycloak/realmConfig/realmConfig.ts @@ -7,13 +7,21 @@ import { type BuildContextLike as BuildContextLike_prepareRealmConfig } from "./prepareRealmConfig"; import * as fs from "fs"; -import { join as pathJoin, dirname as pathDirname } from "path"; +import { + join as pathJoin, + dirname as pathDirname, + relative as pathRelative, + sep as pathSep +} from "path"; import { existsAsync } from "../../tools/fs.existsAsync"; import { readRealmJsonFile, type ParsedRealmJson } from "./ParsedRealmJson"; import { dumpContainerConfig, type BuildContextLike as BuildContextLike_dumpContainerConfig } from "./dumpContainerConfig"; +import * as runExclusive from "run-exclusive"; +import { waitForDebounceFactory } from "powerhooks/tools/waitForDebounce"; +import chalk from "chalk"; export type BuildContextLike = BuildContextLike_dumpContainerConfig & BuildContextLike_prepareRealmConfig & { @@ -89,15 +97,49 @@ export async function getRealmConfig(params: { await writeRealmJsonFile({ parsedRealmJson }); - async function onRealmConfigChange() { - const parsedRealmJson = await dumpContainerConfig({ - buildContext, - realmName, - keycloakMajorVersionNumber + const { onRealmConfigChange } = (() => { + const run = runExclusive.build(async () => { + const start = Date.now(); + + console.log( + chalk.grey(`Changes detected to the '${realmName}' config, backing up...`) + ); + + const parsedRealmJson = await dumpContainerConfig({ + buildContext, + realmName, + keycloakMajorVersionNumber + }); + + await writeRealmJsonFile({ parsedRealmJson }); + + console.log( + [ + chalk.green( + `✓ '${realmName}' config backed up completed in ${Date.now() - start}ms` + ), + chalk.grey( + `Save changed to \`.${pathSep}${pathRelative(buildContext.projectDirPath, realmJsonFilePath)}\`` + ), + chalk.grey( + `Next time you'll be running \`keycloakify start-keycloak\`, the realm '${realmName}' will be restored to this state.` + ) + ].join("\n") + ); }); - await writeRealmJsonFile({ parsedRealmJson }); - } + const { waitForDebounce } = waitForDebounceFactory({ + delay: 1_000 + }); + + async function onRealmConfigChange() { + await waitForDebounce(); + + run(); + } + + return { onRealmConfigChange }; + })(); return { realmJsonFilePath,