keycloak_theme/src/bin/shared/copyKeycloakResourcesToPublic.ts

102 lines
3.2 KiB
TypeScript
Raw Normal View History

import {
downloadKeycloakStaticResources,
2024-06-09 09:15:16 +02:00
type BuildContextLike as BuildContextLike_downloadKeycloakStaticResources
} from "./downloadKeycloakStaticResources";
import { join as pathJoin, relative as pathRelative } from "path";
2024-05-20 15:48:51 +02:00
import {
2024-07-13 19:33:59 +02:00
THEME_TYPES,
KEYCLOAK_RESOURCES,
LAST_KEYCLOAK_VERSION_WITH_ACCOUNT_V1
2024-05-20 15:48:51 +02:00
} from "../shared/constants";
2024-05-18 10:24:55 +02:00
import { readThisNpmPackageVersion } from "../tools/readThisNpmPackageVersion";
import { assert } from "tsafe/assert";
import * as fs from "fs";
import { rmSync } from "../tools/fs.rmSync";
2024-06-09 09:15:16 +02:00
import type { BuildContext } from "./buildContext";
2024-06-09 09:15:16 +02:00
export type BuildContextLike = BuildContextLike_downloadKeycloakStaticResources & {
loginThemeResourcesFromKeycloakVersion: string;
publicDirPath: string;
};
2024-06-09 09:15:16 +02:00
assert<BuildContext extends BuildContextLike ? true : false>();
2024-05-20 15:48:51 +02:00
export async function copyKeycloakResourcesToPublic(params: {
2024-06-09 09:15:16 +02:00
buildContext: BuildContextLike;
2024-05-20 15:48:51 +02:00
}) {
2024-06-09 09:15:16 +02:00
const { buildContext } = params;
2024-07-13 19:33:59 +02:00
const destDirPath = pathJoin(buildContext.publicDirPath, KEYCLOAK_RESOURCES);
const keycloakifyBuildinfoFilePath = pathJoin(destDirPath, "keycloakify.buildinfo");
const keycloakifyBuildinfoRaw = JSON.stringify(
{
destDirPath,
2024-05-20 15:48:51 +02:00
keycloakifyVersion: readThisNpmPackageVersion(),
2024-06-09 09:15:16 +02:00
buildContext: {
2024-05-20 15:48:51 +02:00
loginThemeResourcesFromKeycloakVersion: readThisNpmPackageVersion(),
2024-06-09 09:15:16 +02:00
cacheDirPath: pathRelative(destDirPath, buildContext.cacheDirPath),
fetchOptions: buildContext.fetchOptions
}
},
null,
2
);
skip_if_already_done: {
if (!fs.existsSync(keycloakifyBuildinfoFilePath)) {
break skip_if_already_done;
}
2024-05-20 15:48:51 +02:00
const keycloakifyBuildinfoRaw_previousRun = fs
.readFileSync(keycloakifyBuildinfoFilePath)
.toString("utf8");
if (keycloakifyBuildinfoRaw_previousRun !== keycloakifyBuildinfoRaw) {
break skip_if_already_done;
}
return;
}
2024-05-20 15:48:51 +02:00
rmSync(destDirPath, { force: true, recursive: true });
2024-05-20 15:48:51 +02:00
fs.mkdirSync(destDirPath, { recursive: true });
2024-05-18 04:48:04 +02:00
fs.writeFileSync(pathJoin(destDirPath, ".gitignore"), Buffer.from("*", "utf8"));
2024-07-13 19:33:59 +02:00
for (const themeType of THEME_TYPES) {
await downloadKeycloakStaticResources({
2024-05-20 15:48:51 +02:00
keycloakVersion: (() => {
switch (themeType) {
case "login":
2024-06-09 09:15:16 +02:00
return buildContext.loginThemeResourcesFromKeycloakVersion;
case "account":
2024-07-13 19:33:59 +02:00
return LAST_KEYCLOAK_VERSION_WITH_ACCOUNT_V1;
}
})(),
themeType,
2024-05-20 15:48:51 +02:00
themeDirPath: destDirPath,
2024-06-09 09:15:16 +02:00
buildContext
});
}
fs.writeFileSync(
pathJoin(destDirPath, "README.txt"),
Buffer.from(
// prettier-ignore
[
"This is just a test folder that helps develop",
2024-05-18 04:48:04 +02:00
"the login and register page without having to run a Keycloak container\n",
"This directory will be automatically excluded from the final build."
].join(" ")
)
);
2024-05-20 15:48:51 +02:00
fs.writeFileSync(
keycloakifyBuildinfoFilePath,
Buffer.from(keycloakifyBuildinfoRaw, "utf8")
);
}