keycloak_theme/src/bin/initialize-email-theme.ts

121 lines
3.7 KiB
TypeScript
Raw Normal View History

2023-03-20 01:30:42 +01:00
import { join as pathJoin, relative as pathRelative } from "path";
import { transformCodebase } from "./tools/transformCodebase";
import { promptKeycloakVersion } from "./shared/promptKeycloakVersion";
2024-10-05 20:30:09 +02:00
import type { BuildContext } from "./shared/buildContext";
2023-03-20 01:30:42 +01:00
import * as fs from "fs";
2024-09-08 12:00:07 +02:00
import { downloadAndExtractArchive } from "./tools/downloadAndExtractArchive";
import { maybeDelegateCommandToCustomHandler } from "./shared/customHandler_delegate";
2024-10-17 23:23:26 +02:00
import fetch from "make-fetch-happen";
import { SemVer } from "./tools/SemVer";
import { assert } from "tsafe/assert";
2023-03-24 05:43:34 +01:00
2024-10-05 20:30:09 +02:00
export async function command(params: { buildContext: BuildContext }) {
const { buildContext } = params;
const { hasBeenHandled } = maybeDelegateCommandToCustomHandler({
commandName: "initialize-email-theme",
buildContext
});
if (hasBeenHandled) {
return;
}
const emailThemeSrcDirPath = pathJoin(buildContext.themeSrcDirPath, "email");
2024-09-08 12:00:07 +02:00
if (
fs.existsSync(emailThemeSrcDirPath) &&
fs.readdirSync(emailThemeSrcDirPath).length > 0
) {
2024-05-20 15:48:51 +02:00
console.warn(
2024-09-08 12:00:07 +02:00
`There is already a non empty ${pathRelative(
2024-05-20 15:48:51 +02:00
process.cwd(),
emailThemeSrcDirPath
)} directory in your project. Aborting.`
);
2023-03-20 01:30:42 +01:00
process.exit(-1);
}
console.log("Initialize with the base email theme from which version of Keycloak?");
2024-10-17 23:23:26 +02:00
let { keycloakVersion } = await promptKeycloakVersion({
// NOTE: This is arbitrary
2024-05-20 15:48:51 +02:00
startingFromMajor: 17,
2024-06-11 19:08:36 +02:00
excludeMajorVersions: [],
doOmitPatch: false,
2024-08-13 00:10:34 +02:00
buildContext
2024-05-17 05:13:41 +02:00
});
2023-03-20 01:30:42 +01:00
2024-10-17 23:23:26 +02:00
const getUrl = (keycloakVersion: string) => {
return `https://repo1.maven.org/maven2/org/keycloak/keycloak-themes/${keycloakVersion}/keycloak-themes-${keycloakVersion}.jar`;
};
keycloakVersion = await (async () => {
const keycloakVersionParsed = SemVer.parse(keycloakVersion);
while (true) {
const url = getUrl(SemVer.stringify(keycloakVersionParsed));
const response = await fetch(url, buildContext.fetchOptions);
if (response.ok) {
break;
}
assert(keycloakVersionParsed.patch !== 0);
keycloakVersionParsed.patch--;
}
return SemVer.stringify(keycloakVersionParsed);
})();
2024-09-08 12:00:07 +02:00
const { extractedDirPath } = await downloadAndExtractArchive({
2024-10-17 23:23:26 +02:00
url: getUrl(keycloakVersion),
2024-09-08 12:00:07 +02:00
cacheDirPath: buildContext.cacheDirPath,
fetchOptions: buildContext.fetchOptions,
uniqueIdOfOnArchiveFile: "extractOnlyEmailTheme",
onArchiveFile: async ({ fileRelativePath, writeFile }) => {
const fileRelativePath_target = pathRelative(
pathJoin("theme", "base", "email"),
fileRelativePath
);
if (fileRelativePath_target.startsWith("..")) {
return;
}
await writeFile({ fileRelativePath: fileRelativePath_target });
}
2023-03-20 01:30:42 +01:00
});
transformCodebase({
2024-09-08 12:00:07 +02:00
srcDirPath: extractedDirPath,
2024-05-20 15:48:51 +02:00
destDirPath: emailThemeSrcDirPath
2023-03-20 01:30:42 +01:00
});
2023-03-21 01:50:21 +01:00
{
2023-03-24 05:43:34 +01:00
const themePropertyFilePath = pathJoin(emailThemeSrcDirPath, "theme.properties");
2023-03-21 01:50:21 +01:00
2024-05-20 15:48:51 +02:00
fs.writeFileSync(
themePropertyFilePath,
Buffer.from(
[
`parent=base`,
fs.readFileSync(themePropertyFilePath).toString("utf8")
].join("\n"),
2024-05-20 15:48:51 +02:00
"utf8"
)
);
2023-03-21 01:50:21 +01:00
}
2024-05-20 15:48:51 +02:00
console.log(
`The \`${pathJoin(
".",
pathRelative(process.cwd(), emailThemeSrcDirPath)
)}\` directory have been created.`
);
2024-05-18 10:24:55 +02:00
console.log("You can delete any file you don't modify.");
2023-03-24 05:43:34 +01:00
}