keycloak_theme/src/bin/keycloakify/generateStartKeycloakTestingContainer.ts

59 lines
2.2 KiB
TypeScript
Raw Normal View History

2022-04-09 20:17:20 +02:00
import * as fs from "fs";
import { join as pathJoin } from "path";
2022-08-16 14:41:06 +07:00
import { assert } from "tsafe/assert";
import { Reflect } from "tsafe/Reflect";
2022-08-16 14:41:06 +07:00
import type { BuildOptions } from "./BuildOptions";
export type BuildOptionsLike = {
2023-09-04 00:25:36 +02:00
keycloakifyBuildDirPath: string;
2022-08-16 14:41:06 +07:00
};
{
const buildOptions = Reflect<BuildOptions>();
assert<typeof buildOptions extends BuildOptionsLike ? true : false>();
}
2022-04-09 20:17:20 +02:00
generateStartKeycloakTestingContainer.basename = "start_keycloak_testing_container.sh";
const containerName = "keycloak-testing-container";
/** Files for being able to run a hot reload keycloak container */
2023-09-04 00:25:36 +02:00
export function generateStartKeycloakTestingContainer(params: { keycloakVersion: string; buildOptions: BuildOptionsLike }) {
const { keycloakVersion, buildOptions } = params;
const themeRelativeDirPath = pathJoin("src", "main", "resources", "theme");
const themeDirPath = pathJoin(buildOptions.keycloakifyBuildDirPath, themeRelativeDirPath);
2022-04-09 20:17:20 +02:00
fs.writeFileSync(
2023-09-04 00:25:36 +02:00
pathJoin(buildOptions.keycloakifyBuildDirPath, generateStartKeycloakTestingContainer.basename),
2022-04-09 20:17:20 +02:00
Buffer.from(
[
"#!/usr/bin/env bash",
2022-04-09 20:17:20 +02:00
"",
`docker rm ${containerName} || true`,
"",
2023-09-04 00:25:36 +02:00
`cd "${buildOptions.keycloakifyBuildDirPath}"`,
2022-04-09 20:17:20 +02:00
"",
"docker run \\",
" -p 8080:8080 \\",
` --name ${containerName} \\`,
" -e KEYCLOAK_ADMIN=admin \\",
" -e KEYCLOAK_ADMIN_PASSWORD=admin \\",
2023-09-04 00:25:36 +02:00
...fs
.readdirSync(themeDirPath)
.filter(name => fs.lstatSync(pathJoin(themeDirPath, name)).isDirectory())
.map(
themeName =>
` -v "${pathJoin(".", themeRelativeDirPath, themeName).replace(/\\/g, "/")}":"/opt/keycloak/themes/${themeName}":rw \\`
),
2022-04-09 20:17:20 +02:00
` -it quay.io/keycloak/keycloak:${keycloakVersion} \\`,
` start-dev --features=declarative-user-profile`,
""
2022-04-09 20:17:20 +02:00
].join("\n"),
"utf8"
2022-04-09 20:17:20 +02:00
),
{ "mode": 0o755 }
2022-04-09 20:17:20 +02:00
);
}