148 lines
6.8 KiB
TypeScript
Raw Normal View History

import { generateKeycloakThemeResources } from "./generateKeycloakThemeResources";
import { generateJavaStackFiles } from "./generateJavaStackFiles";
2023-03-21 15:16:23 +01:00
import { join as pathJoin, relative as pathRelative, basename as pathBasename, sep as pathSep } from "path";
import * as child_process from "child_process";
2022-04-09 20:17:20 +02:00
import { generateStartKeycloakTestingContainer } from "./generateStartKeycloakTestingContainer";
import * as fs from "fs";
2022-08-16 14:41:06 +07:00
import { readBuildOptions } from "./BuildOptions";
2022-09-08 12:06:26 +03:00
import { getLogger } from "../tools/logger";
import { getCliOptions } from "../tools/cliOptions";
import jar from "../tools/jar";
import { assert } from "tsafe/assert";
import type { Equals } from "tsafe";
const reactProjectDirPath = process.cwd();
export const keycloakThemeBuildingDirPath = pathJoin(reactProjectDirPath, "build_keycloak");
2023-03-20 00:03:15 +01:00
export const keycloakThemeEmailDirPath = pathJoin(reactProjectDirPath, "src", "keycloak-theme", "email");
export async function main() {
2022-09-08 12:06:26 +03:00
const { isSilent, hasExternalAssets } = getCliOptions(process.argv.slice(2));
const logger = getLogger({ isSilent });
logger.log("🔏 Building the keycloak theme...⌚");
2022-08-16 14:41:06 +07:00
const buildOptions = readBuildOptions({
2022-08-20 14:04:47 +07:00
"packageJson": fs.readFileSync(pathJoin(reactProjectDirPath, "package.json")).toString("utf8"),
"CNAME": (() => {
const cnameFilePath = pathJoin(reactProjectDirPath, "public", "CNAME");
if (!fs.existsSync(cnameFilePath)) {
return undefined;
}
return fs.readFileSync(cnameFilePath).toString("utf8");
})(),
2022-09-08 12:06:26 +03:00
"isExternalAssetsCliParamProvided": hasExternalAssets,
"isSilent": isSilent
2022-08-16 14:41:06 +07:00
});
const { doBundlesEmailTemplate } = await generateKeycloakThemeResources({
keycloakThemeBuildingDirPath,
2022-04-20 00:39:40 +02:00
keycloakThemeEmailDirPath,
"reactAppBuildDirPath": pathJoin(reactProjectDirPath, "build"),
2022-08-16 14:41:06 +07:00
buildOptions,
//We have to leave it at that otherwise we break our default theme.
2023-03-21 15:16:23 +01:00
//Problem is that we can`t guarantee that the the old resources
//will still be available on the newer keycloak version.
"keycloakVersion": "11.0.3"
});
const { jarFilePath } = generateJavaStackFiles({
keycloakThemeBuildingDirPath,
2022-08-16 14:41:06 +07:00
doBundlesEmailTemplate,
buildOptions
});
switch (buildOptions.bundler) {
case "none":
logger.log("😱 Skipping bundling step, there will be no jar");
break;
case "keycloakify":
logger.log("🫶 Let keycloakify do its thang");
await jar({
2023-02-05 13:36:52 +01:00
"rootPath": pathJoin(keycloakThemeBuildingDirPath, "src", "main", "resources"),
"version": buildOptions.version,
"groupId": buildOptions.groupId,
2023-02-05 13:36:52 +01:00
"artifactId": buildOptions.artifactId,
"targetPath": jarFilePath
});
break;
case "mvn":
logger.log("🫙 Run maven to deliver a jar");
child_process.execSync("mvn package", { "cwd": keycloakThemeBuildingDirPath });
break;
default:
assert<Equals<typeof buildOptions.bundler, never>>(false);
}
// We want, however, to test in a container running the latest Keycloak version
const containerKeycloakVersion = "20.0.1";
2022-04-09 20:17:20 +02:00
generateStartKeycloakTestingContainer({
keycloakThemeBuildingDirPath,
"keycloakVersion": containerKeycloakVersion,
buildOptions
});
2022-09-08 12:06:26 +03:00
logger.log(
[
"",
`✅ Your keycloak theme has been generated and bundled into ./${pathRelative(reactProjectDirPath, jarFilePath)} 🚀`,
`It is to be placed in "/opt/keycloak/providers" in the container running a quay.io/keycloak/keycloak Docker image.`,
"",
2022-04-29 18:35:07 +02:00
//TODO: Restore when we find a good Helm chart for Keycloak.
//"Using Helm (https://github.com/codecentric/helm-charts), edit to reflect:",
"",
"value.yaml: ",
" extraInitContainers: |",
" - name: realm-ext-provider",
" image: curlimages/curl",
" imagePullPolicy: IfNotPresent",
" command:",
" - sh",
" args:",
" - -c",
` - curl -L -f -S -o /extensions/${pathBasename(jarFilePath)} https://AN.URL.FOR/${pathBasename(jarFilePath)}`,
" volumeMounts:",
" - name: extensions",
" mountPath: /extensions",
" ",
" extraVolumeMounts: |",
" - name: extensions",
" mountPath: /opt/keycloak/providers",
" extraEnv: |",
" - name: KEYCLOAK_USER",
" value: admin",
" - name: KEYCLOAK_PASSWORD",
" value: xxxxxxxxx",
" - name: JAVA_OPTS",
" value: -Dkeycloak.profile=preview",
"",
"",
`To test your theme locally you can spin up a Keycloak ${containerKeycloakVersion} container image with the theme pre loaded by running:`,
"",
2023-03-21 15:16:23 +01:00
`👉 $ .${pathSep}${pathRelative(
reactProjectDirPath,
pathJoin(keycloakThemeBuildingDirPath, generateStartKeycloakTestingContainer.basename)
)} 👈`,
"",
2023-03-21 15:16:23 +01:00
`Test with different Keycloak versions by editing the .sh file. see available versions here: https://quay.io/repository/keycloak/keycloak?tab=tags`,
``,
`Once your container is up and running: `,
2022-04-09 20:17:20 +02:00
"- Log into the admin console 👉 http://localhost:8080/admin username: admin, password: admin 👈",
2023-03-21 15:16:23 +01:00
`- Create a realm named "myrealm"`,
2023-03-21 19:44:01 +01:00
`- Enable the Account theme: Realm settings -> tab Themes -> Account theme, select ${buildOptions.themeName} `,
2023-03-21 15:16:23 +01:00
`- Create a client with ID: "myclient"`,
` Root URL: https://www.keycloak.org/app/`,
` Valid redirect URIs: https://www.keycloak.org/app* http://localhost* (localhost isn't necessary here but it will be once you'll want to test the integration with your app)`,
` Valid post logout redirect URIs: https://www.keycloak.org/app* http://localhost*`,
` Web origins: *`,
` Login Theme: ${buildOptions.themeName}`,
` Click save at the bottom of the page.`,
`- Go to 👉 https://www.keycloak.org/app/ 👈 Click "Save" then "Sign in". You should see your login page`,
2023-03-21 19:44:01 +01:00
`- Got to 👉 http://localhost:8080/realms/myrealm/account 👈 to see your account theme`,
2023-03-21 15:16:23 +01:00
``
].join("\n")
);
}