keycloak_theme/src/bin/keycloakify/generateTheme/downloadKeycloakStaticResources.ts

75 lines
2.5 KiB
TypeScript
Raw Normal View History

import { transformCodebase } from "../../tools/transformCodebase";
import * as fs from "fs";
import { join as pathJoin } from "path";
import { downloadBuiltinKeycloakTheme } from "../../download-builtin-keycloak-theme";
import { resources_common, type ThemeType } from "../../constants";
2023-09-03 23:26:34 +02:00
import { BuildOptions } from "../BuildOptions";
2023-10-02 22:49:04 +02:00
import { assert } from "tsafe/assert";
import * as crypto from "crypto";
2023-09-03 23:26:34 +02:00
export type BuildOptionsLike = {
cacheDirPath: string;
};
assert<BuildOptions extends BuildOptionsLike ? true : false>();
export async function downloadKeycloakStaticResources(
// prettier-ignore
params: {
themeType: ThemeType;
themeDirPath: string;
keycloakVersion: string;
2023-08-24 08:58:00 +02:00
usedResources: {
resourcesCommonFilePaths: string[];
resourcesFilePaths: string[];
2023-09-03 23:26:34 +02:00
} | undefined;
buildOptions: BuildOptionsLike;
}
) {
2023-09-03 23:26:34 +02:00
const { themeType, themeDirPath, keycloakVersion, usedResources, buildOptions } = params;
2023-08-24 08:58:00 +02:00
const tmpDirPath = pathJoin(
themeDirPath,
`tmp_suLeKsxId_${crypto.createHash("sha256").update(`${themeType}-${keycloakVersion}`).digest("hex").slice(0, 8)}`
);
await downloadBuiltinKeycloakTheme({
keycloakVersion,
2023-09-03 23:26:34 +02:00
"destDirPath": tmpDirPath,
buildOptions
});
const resourcesPath = pathJoin(themeDirPath, themeType, "resources");
transformCodebase({
"srcDirPath": pathJoin(tmpDirPath, "keycloak", themeType, "resources"),
"destDirPath": resourcesPath,
2023-08-24 08:58:00 +02:00
"transformSourceCode":
usedResources === undefined
? undefined
: ({ fileRelativePath, sourceCode }) => {
if (!usedResources.resourcesFilePaths.includes(fileRelativePath)) {
return undefined;
}
return { "modifiedSourceCode": sourceCode };
}
});
transformCodebase({
"srcDirPath": pathJoin(tmpDirPath, "keycloak", "common", "resources"),
"destDirPath": pathJoin(resourcesPath, resources_common),
2023-08-24 08:58:00 +02:00
"transformSourceCode":
usedResources === undefined
? undefined
: ({ fileRelativePath, sourceCode }) => {
if (!usedResources.resourcesCommonFilePaths.includes(fileRelativePath)) {
return undefined;
}
return { "modifiedSourceCode": sourceCode };
}
});
fs.rmSync(tmpDirPath, { "recursive": true, "force": true });
2023-11-26 14:45:23 +01:00
}