2021-06-14 21:24:56 +02:00
|
|
|
import cheerio from "cheerio";
|
2022-08-16 14:41:06 +07:00
|
|
|
import { replaceImportsFromStaticInJsCode } from "../replacers/replaceImportsFromStaticInJsCode";
|
|
|
|
import { generateCssCodeToDefineGlobals } from "../replacers/replaceImportsInCssCode";
|
|
|
|
import { replaceImportsInInlineCssCode } from "../replacers/replaceImportsInInlineCssCode";
|
2022-07-29 23:10:35 +02:00
|
|
|
import * as fs from "fs";
|
2021-06-14 21:24:56 +02:00
|
|
|
import { join as pathJoin } from "path";
|
2021-06-28 04:04:48 +02:00
|
|
|
import { objectKeys } from "tsafe/objectKeys";
|
2021-06-14 21:24:56 +02:00
|
|
|
import { ftlValuesGlobalName } from "../ftlValuesGlobalName";
|
2022-08-16 14:41:06 +07:00
|
|
|
import type { BuildOptions } from "../BuildOptions";
|
|
|
|
import { assert } from "tsafe/assert";
|
|
|
|
import { Reflect } from "tsafe/Reflect";
|
2021-06-14 21:24:56 +02:00
|
|
|
|
2022-06-17 00:47:55 +02:00
|
|
|
// https://github.com/keycloak/keycloak/blob/main/services/src/main/java/org/keycloak/forms/login/freemarker/Templates.java
|
2021-06-14 21:24:56 +02:00
|
|
|
export const pageIds = [
|
2021-10-11 21:35:40 +02:00
|
|
|
"login.ftl",
|
|
|
|
"register.ftl",
|
|
|
|
"register-user-profile.ftl",
|
|
|
|
"info.ftl",
|
|
|
|
"error.ftl",
|
|
|
|
"login-reset-password.ftl",
|
|
|
|
"login-verify-email.ftl",
|
|
|
|
"terms.ftl",
|
|
|
|
"login-otp.ftl",
|
|
|
|
"login-update-profile.ftl",
|
2021-12-28 00:08:25 +03:00
|
|
|
"login-update-password.ftl",
|
2021-10-11 21:35:40 +02:00
|
|
|
"login-idp-link-confirm.ftl",
|
2022-04-22 17:54:47 +03:00
|
|
|
"login-idp-link-email.ftl",
|
2022-01-01 18:44:05 +02:00
|
|
|
"login-page-expired.ftl",
|
2022-06-28 14:37:17 -04:00
|
|
|
"login-config-totp.ftl",
|
2022-08-20 11:44:48 +07:00
|
|
|
"logout-confirm.ftl"
|
2021-06-14 21:24:56 +02:00
|
|
|
] as const;
|
|
|
|
|
2022-08-16 14:41:06 +07:00
|
|
|
export type BuildOptionsLike = BuildOptionsLike.Standalone | BuildOptionsLike.ExternalAssets;
|
|
|
|
|
|
|
|
export namespace BuildOptionsLike {
|
|
|
|
export type Standalone = {
|
|
|
|
isStandalone: true;
|
|
|
|
urlPathname: string | undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type ExternalAssets = ExternalAssets.SameDomain | ExternalAssets.DifferentDomains;
|
|
|
|
|
|
|
|
export namespace ExternalAssets {
|
|
|
|
export type CommonExternalAssets = {
|
|
|
|
isStandalone: false;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type SameDomain = CommonExternalAssets & {
|
|
|
|
isAppAndKeycloakServerSharingSameDomain: true;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type DifferentDomains = CommonExternalAssets & {
|
|
|
|
isAppAndKeycloakServerSharingSameDomain: false;
|
|
|
|
urlOrigin: string;
|
|
|
|
urlPathname: string | undefined;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const buildOptions = Reflect<BuildOptions>();
|
|
|
|
|
|
|
|
assert<typeof buildOptions extends BuildOptionsLike ? true : false>();
|
|
|
|
}
|
|
|
|
|
2021-06-14 21:24:56 +02:00
|
|
|
export type PageId = typeof pageIds[number];
|
|
|
|
|
2021-10-11 21:35:40 +02:00
|
|
|
export function generateFtlFilesCodeFactory(params: {
|
|
|
|
indexHtmlCode: string;
|
2022-08-16 14:41:06 +07:00
|
|
|
//NOTE: Expected to be an empty object if external assets mode is enabled.
|
|
|
|
cssGlobalsToDefine: Record<string, string>;
|
|
|
|
buildOptions: BuildOptionsLike;
|
2021-10-11 21:35:40 +02:00
|
|
|
}) {
|
2022-08-16 14:41:06 +07:00
|
|
|
const { cssGlobalsToDefine, indexHtmlCode, buildOptions } = params;
|
2021-06-14 21:24:56 +02:00
|
|
|
|
|
|
|
const $ = cheerio.load(indexHtmlCode);
|
|
|
|
|
2022-08-16 14:41:06 +07:00
|
|
|
fix_imports_statements: {
|
|
|
|
if (!buildOptions.isStandalone && buildOptions.isAppAndKeycloakServerSharingSameDomain) {
|
|
|
|
break fix_imports_statements;
|
|
|
|
}
|
2021-06-14 21:24:56 +02:00
|
|
|
|
2022-08-16 14:41:06 +07:00
|
|
|
$("script:not([src])").each((...[, element]) => {
|
|
|
|
const { fixedJsCode } = replaceImportsFromStaticInJsCode({
|
|
|
|
"jsCode": $(element).html()!,
|
2022-08-20 11:44:48 +07:00
|
|
|
buildOptions
|
2022-08-16 14:41:06 +07:00
|
|
|
});
|
2021-06-14 21:24:56 +02:00
|
|
|
|
2022-08-16 14:41:06 +07:00
|
|
|
$(element).text(fixedJsCode);
|
2021-06-14 21:24:56 +02:00
|
|
|
});
|
|
|
|
|
2022-08-16 14:41:06 +07:00
|
|
|
$("style").each((...[, element]) => {
|
|
|
|
const { fixedCssCode } = replaceImportsInInlineCssCode({
|
|
|
|
"cssCode": $(element).html()!,
|
2022-08-20 11:44:48 +07:00
|
|
|
buildOptions
|
2022-08-16 14:41:06 +07:00
|
|
|
});
|
2021-06-14 21:24:56 +02:00
|
|
|
|
2022-08-16 14:41:06 +07:00
|
|
|
$(element).text(fixedCssCode);
|
|
|
|
});
|
|
|
|
|
|
|
|
(
|
|
|
|
[
|
|
|
|
["link", "href"],
|
2022-08-20 11:44:48 +07:00
|
|
|
["script", "src"]
|
2022-08-16 14:41:06 +07:00
|
|
|
] as const
|
|
|
|
).forEach(([selector, attrName]) =>
|
|
|
|
$(selector).each((...[, element]) => {
|
|
|
|
const href = $(element).attr(attrName);
|
|
|
|
|
|
|
|
if (href === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$(element).attr(
|
|
|
|
attrName,
|
|
|
|
buildOptions.isStandalone
|
|
|
|
? href.replace(new RegExp(`^${(buildOptions.urlPathname ?? "/").replace(/\//g, "\\/")}`), "${url.resourcesPath}/build/")
|
2022-08-20 11:44:48 +07:00
|
|
|
: href.replace(/^\//, `${buildOptions.urlOrigin}/`)
|
2022-08-16 14:41:06 +07:00
|
|
|
);
|
2022-08-20 11:44:48 +07:00
|
|
|
})
|
2022-08-16 14:41:06 +07:00
|
|
|
);
|
|
|
|
|
|
|
|
if (Object.keys(cssGlobalsToDefine).length !== 0) {
|
|
|
|
$("head").prepend(
|
|
|
|
[
|
|
|
|
"",
|
|
|
|
"<style>",
|
|
|
|
generateCssCodeToDefineGlobals({
|
|
|
|
cssGlobalsToDefine,
|
2022-08-20 11:44:48 +07:00
|
|
|
buildOptions
|
2022-08-16 14:41:06 +07:00
|
|
|
}).cssCodeToPrependInHead,
|
|
|
|
"</style>",
|
2022-08-20 11:44:48 +07:00
|
|
|
""
|
|
|
|
].join("\n")
|
2021-06-14 21:24:56 +02:00
|
|
|
);
|
2022-08-16 14:41:06 +07:00
|
|
|
}
|
|
|
|
}
|
2021-06-14 21:24:56 +02:00
|
|
|
|
|
|
|
//FTL is no valid html, we can't insert with cheerio, we put placeholder for injecting later.
|
2021-12-08 10:30:36 +03:00
|
|
|
const replaceValueBySearchValue = {
|
|
|
|
'{ "x": "vIdLqMeOed9sdLdIdOxdK0d" }': fs
|
|
|
|
.readFileSync(pathJoin(__dirname, "ftl_object_to_js_code_declaring_an_object.ftl"))
|
|
|
|
.toString("utf8")
|
|
|
|
.match(/^<script>const _=((?:.|\n)+)<\/script>[\n]?$/)![1],
|
2021-10-11 21:35:40 +02:00
|
|
|
"<!-- xIdLqMeOedErIdLsPdNdI9dSlxI -->": [
|
|
|
|
"<#if scripts??>",
|
|
|
|
" <#list scripts as script>",
|
|
|
|
' <script src="${script}" type="text/javascript"></script>',
|
|
|
|
" </#list>",
|
2022-08-20 11:44:48 +07:00
|
|
|
"</#if>"
|
|
|
|
].join("\n")
|
2021-06-14 21:24:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
$("head").prepend(
|
|
|
|
[
|
2021-06-22 12:37:21 +02:00
|
|
|
"<script>",
|
2021-12-08 10:30:36 +03:00
|
|
|
` window.${ftlValuesGlobalName}= ${objectKeys(replaceValueBySearchValue)[0]};`,
|
2021-10-11 21:35:40 +02:00
|
|
|
"</script>",
|
|
|
|
"",
|
2022-08-20 11:44:48 +07:00
|
|
|
objectKeys(replaceValueBySearchValue)[1]
|
|
|
|
].join("\n")
|
2021-06-14 21:24:56 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
const partiallyFixedIndexHtmlCode = $.html();
|
|
|
|
|
2021-10-11 21:35:40 +02:00
|
|
|
function generateFtlFilesCode(params: { pageId: string }): {
|
|
|
|
ftlCode: string;
|
|
|
|
} {
|
2021-06-14 21:24:56 +02:00
|
|
|
const { pageId } = params;
|
|
|
|
|
|
|
|
const $ = cheerio.load(partiallyFixedIndexHtmlCode);
|
|
|
|
|
2021-12-08 10:30:36 +03:00
|
|
|
let ftlCode = $.html();
|
|
|
|
|
|
|
|
Object.entries({
|
|
|
|
...replaceValueBySearchValue,
|
2021-12-12 20:17:50 +01:00
|
|
|
//If updated, don't forget to change in the ftl script as well.
|
2022-08-20 11:44:48 +07:00
|
|
|
"PAGE_ID_xIgLsPgGId9D8e": pageId
|
2021-12-08 10:30:36 +03:00
|
|
|
}).map(([searchValue, replaceValue]) => (ftlCode = ftlCode.replace(searchValue, replaceValue)));
|
2021-06-14 21:24:56 +02:00
|
|
|
|
|
|
|
return { ftlCode };
|
|
|
|
}
|
|
|
|
|
|
|
|
return { generateFtlFilesCode };
|
2021-10-11 21:35:40 +02:00
|
|
|
}
|