diff --git a/src/bin/copy-keycloak-resources-to-public.ts b/src/bin/copy-keycloak-resources-to-public.ts index b48f9d0e..1925860d 100644 --- a/src/bin/copy-keycloak-resources-to-public.ts +++ b/src/bin/copy-keycloak-resources-to-public.ts @@ -1,16 +1,96 @@ -import { copyKeycloakResourcesToPublic } from "./shared/copyKeycloakResourcesToPublic"; -import type { BuildContext } from "./shared/buildContext"; import { maybeDelegateCommandToCustomHandler } from "./shared/customHandler_delegate"; +import { join as pathJoin, dirname as pathDirname } from "path"; +import { WELL_KNOWN_DIRECTORY_BASE_NAME } from "./shared/constants"; +import { readThisNpmPackageVersion } from "./tools/readThisNpmPackageVersion"; +import * as fs from "fs"; +import { rmSync } from "./tools/fs.rmSync"; +import type { BuildContext } from "./shared/buildContext"; +import { transformCodebase } from "./tools/transformCodebase"; +import { getThisCodebaseRootDirPath } from "./tools/getThisCodebaseRootDirPath"; export async function command(params: { buildContext: BuildContext }) { const { buildContext } = params; - maybeDelegateCommandToCustomHandler({ + const { hasBeenHandled } = maybeDelegateCommandToCustomHandler({ commandName: "copy-keycloak-resources-to-public", buildContext }); - copyKeycloakResourcesToPublic({ - buildContext + if (hasBeenHandled) { + return; + } + + const destDirPath = pathJoin( + buildContext.publicDirPath, + WELL_KNOWN_DIRECTORY_BASE_NAME.KEYCLOAKIFY_DEV_RESOURCES + ); + + const keycloakifyBuildinfoFilePath = pathJoin(destDirPath, "keycloakify.buildinfo"); + + const keycloakifyBuildinfoRaw = JSON.stringify( + { + keycloakifyVersion: readThisNpmPackageVersion() + }, + null, + 2 + ); + + skip_if_already_done: { + if (!fs.existsSync(keycloakifyBuildinfoFilePath)) { + break skip_if_already_done; + } + + const keycloakifyBuildinfoRaw_previousRun = fs + .readFileSync(keycloakifyBuildinfoFilePath) + .toString("utf8"); + + if (keycloakifyBuildinfoRaw_previousRun !== keycloakifyBuildinfoRaw) { + break skip_if_already_done; + } + + return; + } + + rmSync(destDirPath, { force: true, recursive: true }); + + // NOTE: To remove in a while, remove the legacy keycloak-resources directory + rmSync(pathJoin(pathDirname(destDirPath), "keycloak-resources"), { + force: true, + recursive: true }); + rmSync(pathJoin(pathDirname(destDirPath), ".keycloakify"), { + force: true, + recursive: true + }); + + fs.mkdirSync(destDirPath, { recursive: true }); + + fs.writeFileSync(pathJoin(destDirPath, ".gitignore"), Buffer.from("*", "utf8")); + + transformCodebase({ + srcDirPath: pathJoin( + getThisCodebaseRootDirPath(), + "res", + "public", + WELL_KNOWN_DIRECTORY_BASE_NAME.KEYCLOAKIFY_DEV_RESOURCES + ), + destDirPath + }); + + fs.writeFileSync( + pathJoin(destDirPath, "README.txt"), + Buffer.from( + // prettier-ignore + [ + "This directory is only used in dev mode by Keycloakify", + "It won't be included in your final build.", + "Do not modify anything in this directory.", + ].join("\n") + ) + ); + + fs.writeFileSync( + keycloakifyBuildinfoFilePath, + Buffer.from(keycloakifyBuildinfoRaw, "utf8") + ); } diff --git a/src/bin/eject-page.ts b/src/bin/eject-page.ts index ee4a3581..e982c2f7 100644 --- a/src/bin/eject-page.ts +++ b/src/bin/eject-page.ts @@ -27,11 +27,15 @@ import { maybeDelegateCommandToCustomHandler } from "./shared/customHandler_dele export async function command(params: { buildContext: BuildContext }) { const { buildContext } = params; - maybeDelegateCommandToCustomHandler({ + const { hasBeenHandled } = maybeDelegateCommandToCustomHandler({ commandName: "eject-page", buildContext }); + if (hasBeenHandled) { + return; + } + console.log(chalk.cyan("Theme type:")); const themeType = await (async () => { diff --git a/src/bin/initialize-account-theme/initialize-account-theme.ts b/src/bin/initialize-account-theme/initialize-account-theme.ts index e7ae9223..dd7c61c8 100644 --- a/src/bin/initialize-account-theme/initialize-account-theme.ts +++ b/src/bin/initialize-account-theme/initialize-account-theme.ts @@ -11,11 +11,15 @@ import { maybeDelegateCommandToCustomHandler } from "../shared/customHandler_del export async function command(params: { buildContext: BuildContext }) { const { buildContext } = params; - maybeDelegateCommandToCustomHandler({ + const { hasBeenHandled } = maybeDelegateCommandToCustomHandler({ commandName: "initialize-account-theme", buildContext }); + if (hasBeenHandled) { + return; + } + const accountThemeSrcDirPath = pathJoin(buildContext.themeSrcDirPath, "account"); if ( diff --git a/src/bin/initialize-email-theme.ts b/src/bin/initialize-email-theme.ts index d5ce0fd8..7a81205b 100644 --- a/src/bin/initialize-email-theme.ts +++ b/src/bin/initialize-email-theme.ts @@ -9,11 +9,15 @@ import { maybeDelegateCommandToCustomHandler } from "./shared/customHandler_dele export async function command(params: { buildContext: BuildContext }) { const { buildContext } = params; - maybeDelegateCommandToCustomHandler({ + const { hasBeenHandled } = maybeDelegateCommandToCustomHandler({ commandName: "initialize-email-theme", buildContext }); + if (hasBeenHandled) { + return; + } + const emailThemeSrcDirPath = pathJoin(buildContext.themeSrcDirPath, "email"); if ( diff --git a/src/bin/shared/copyKeycloakResourcesToPublic.ts b/src/bin/shared/copyKeycloakResourcesToPublic.ts deleted file mode 100644 index db78d313..00000000 --- a/src/bin/shared/copyKeycloakResourcesToPublic.ts +++ /dev/null @@ -1,95 +0,0 @@ -import { join as pathJoin, dirname as pathDirname } from "path"; -import { WELL_KNOWN_DIRECTORY_BASE_NAME } from "../shared/constants"; -import { readThisNpmPackageVersion } from "../tools/readThisNpmPackageVersion"; -import { assert } from "tsafe/assert"; -import * as fs from "fs"; -import { rmSync } from "../tools/fs.rmSync"; -import type { BuildContext } from "./buildContext"; -import { transformCodebase } from "../tools/transformCodebase"; -import { getThisCodebaseRootDirPath } from "../tools/getThisCodebaseRootDirPath"; - -export type BuildContextLike = { - publicDirPath: string; -}; - -assert(); - -export function copyKeycloakResourcesToPublic(params: { - buildContext: BuildContextLike; -}) { - const { buildContext } = params; - - const destDirPath = pathJoin( - buildContext.publicDirPath, - WELL_KNOWN_DIRECTORY_BASE_NAME.KEYCLOAKIFY_DEV_RESOURCES - ); - - const keycloakifyBuildinfoFilePath = pathJoin(destDirPath, "keycloakify.buildinfo"); - - const keycloakifyBuildinfoRaw = JSON.stringify( - { - keycloakifyVersion: readThisNpmPackageVersion() - }, - null, - 2 - ); - - skip_if_already_done: { - if (!fs.existsSync(keycloakifyBuildinfoFilePath)) { - break skip_if_already_done; - } - - const keycloakifyBuildinfoRaw_previousRun = fs - .readFileSync(keycloakifyBuildinfoFilePath) - .toString("utf8"); - - if (keycloakifyBuildinfoRaw_previousRun !== keycloakifyBuildinfoRaw) { - break skip_if_already_done; - } - - return; - } - - rmSync(destDirPath, { force: true, recursive: true }); - - // NOTE: To remove in a while, remove the legacy keycloak-resources directory - rmSync(pathJoin(pathDirname(destDirPath), "keycloak-resources"), { - force: true, - recursive: true - }); - rmSync(pathJoin(pathDirname(destDirPath), ".keycloakify"), { - force: true, - recursive: true - }); - - fs.mkdirSync(destDirPath, { recursive: true }); - - fs.writeFileSync(pathJoin(destDirPath, ".gitignore"), Buffer.from("*", "utf8")); - - transformCodebase({ - srcDirPath: pathJoin( - getThisCodebaseRootDirPath(), - "res", - "public", - WELL_KNOWN_DIRECTORY_BASE_NAME.KEYCLOAKIFY_DEV_RESOURCES - ), - destDirPath - }); - - fs.writeFileSync( - pathJoin(destDirPath, "README.txt"), - Buffer.from( - // prettier-ignore - [ - "This directory is only used in dev mode by Keycloakify", - "It won't be included in your final build.", - "Do not modify anything in this directory.", - ].join("\n") - ) - ); - - fs.writeFileSync( - keycloakifyBuildinfoFilePath, - Buffer.from(keycloakifyBuildinfoRaw, "utf8") - ); -} diff --git a/src/bin/shared/customHandler_delegate.ts b/src/bin/shared/customHandler_delegate.ts index a41cbc7b..691989df 100644 --- a/src/bin/shared/customHandler_delegate.ts +++ b/src/bin/shared/customHandler_delegate.ts @@ -16,11 +16,11 @@ assert>(); export function maybeDelegateCommandToCustomHandler(params: { commandName: CommandName; buildContext: BuildContext; -}) { +}): { hasBeenHandled: boolean } { const { commandName, buildContext } = params; if (!fs.readdirSync(pathDirname(process.argv[1])).includes(BIN_NAME)) { - return; + return { hasBeenHandled: false }; } try { @@ -36,11 +36,11 @@ export function maybeDelegateCommandToCustomHandler(params: { const status = error.status; if (status === NOT_IMPLEMENTED_EXIT_CODE) { - return; + return { hasBeenHandled: false }; } process.exit(status); } - process.exit(0); + return { hasBeenHandled: true }; } diff --git a/src/bin/update-kc-gen.ts b/src/bin/update-kc-gen.ts index f126bcb1..e1366fd4 100644 --- a/src/bin/update-kc-gen.ts +++ b/src/bin/update-kc-gen.ts @@ -7,11 +7,15 @@ import { maybeDelegateCommandToCustomHandler } from "./shared/customHandler_dele export async function command(params: { buildContext: BuildContext }) { const { buildContext } = params; - maybeDelegateCommandToCustomHandler({ + const { hasBeenHandled } = maybeDelegateCommandToCustomHandler({ commandName: "update-kc-gen", buildContext }); + if (hasBeenHandled) { + return; + } + const filePath = pathJoin(buildContext.themeSrcDirPath, `kc.gen.tsx`); const currentContent = (await existsAsync(filePath)) diff --git a/src/vite-plugin/vite-plugin.ts b/src/vite-plugin/vite-plugin.ts index e6dfb054..d7ec06d5 100644 --- a/src/vite-plugin/vite-plugin.ts +++ b/src/vite-plugin/vite-plugin.ts @@ -6,7 +6,7 @@ import { } from "../bin/shared/constants"; import { id } from "tsafe/id"; import { rm } from "../bin/tools/fs.rm"; -import { copyKeycloakResourcesToPublic } from "../bin/shared/copyKeycloakResourcesToPublic"; +import { command as copyKeycloakResourcesToPublicCommand } from "../bin/copy-keycloak-resources-to-public"; import { assert } from "tsafe/assert"; import { getBuildContext, @@ -125,7 +125,7 @@ export function keycloakify(params: keycloakify.Params) { projectDirPath }); - copyKeycloakResourcesToPublic({ buildContext }); + await copyKeycloakResourcesToPublicCommand({ buildContext }); await updateKcGenCommand({ buildContext }); },