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

90 lines
2.8 KiB
TypeScript
Raw Normal View History

import { transformCodebase } from "../../tools/transformCodebase";
import * as fs from "fs";
2023-11-26 15:07:27 +01:00
import { join as pathJoin, dirname as pathDirname } from "path";
import { downloadBuiltinKeycloakTheme } from "../../download-builtin-keycloak-theme";
import { resources_common, type ThemeType } from "../../constants";
2024-01-30 06:38:26 +01: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[];
2023-09-03 23:26:34 +02:00
} | undefined;
buildOptions: BuildOptionsLike;
}
) {
2023-11-26 15:07:27 +01:00
const { themeType, themeDirPath, keycloakVersion, buildOptions } = params;
// NOTE: Hack for 427
const usedResources = (() => {
const { usedResources } = params;
if (usedResources === undefined) {
return undefined;
}
assert(usedResources !== undefined);
return {
"resourcesCommonDirPaths": usedResources.resourcesCommonFilePaths.map(filePath => {
{
const splitArg = "/dist/";
if (filePath.includes(splitArg)) {
return filePath.split(splitArg)[0] + splitArg;
}
}
return pathDirname(filePath);
})
};
})();
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"),
2023-11-26 15:07:27 +01:00
"destDirPath": resourcesPath
});
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 }) => {
2023-11-26 15:07:27 +01:00
if (usedResources.resourcesCommonDirPaths.find(dirPath => fileRelativePath.startsWith(dirPath)) === undefined) {
2023-08-24 08:58:00 +02:00
return undefined;
}
return { "modifiedSourceCode": sourceCode };
}
});
fs.rmSync(tmpDirPath, { "recursive": true, "force": true });
2023-11-26 15:07:27 +01:00
}