2021-06-14 21:24:56 +02:00
|
|
|
import cheerio from "cheerio";
|
2021-10-12 00:26:29 +02:00
|
|
|
import { replaceImportsFromStaticInJsCode, replaceImportsInInlineCssCode, generateCssCodeToDefineGlobals } from "../replaceImportFromStatic";
|
2021-06-14 21:24:56 +02:00
|
|
|
import fs from "fs";
|
|
|
|
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";
|
|
|
|
|
|
|
|
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",
|
2021-06-14 21:24:56 +02:00
|
|
|
] as const;
|
|
|
|
|
|
|
|
export type PageId = typeof pageIds[number];
|
|
|
|
|
2021-10-11 21:35:40 +02:00
|
|
|
export function generateFtlFilesCodeFactory(params: {
|
|
|
|
cssGlobalsToDefine: Record<string, string>;
|
|
|
|
indexHtmlCode: string;
|
|
|
|
urlPathname: string;
|
|
|
|
urlOrigin: undefined | string;
|
|
|
|
}) {
|
2021-10-12 00:26:29 +02:00
|
|
|
const { cssGlobalsToDefine, indexHtmlCode, urlPathname, urlOrigin } = params;
|
2021-06-14 21:24:56 +02:00
|
|
|
|
|
|
|
const $ = cheerio.load(indexHtmlCode);
|
|
|
|
|
|
|
|
$("script:not([src])").each((...[, element]) => {
|
|
|
|
const { fixedJsCode } = replaceImportsFromStaticInJsCode({
|
|
|
|
"jsCode": $(element).html()!,
|
2021-10-11 21:35:40 +02:00
|
|
|
urlOrigin,
|
2021-06-14 21:24:56 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$(element).text(fixedJsCode);
|
|
|
|
});
|
|
|
|
|
|
|
|
$("style").each((...[, element]) => {
|
|
|
|
const { fixedCssCode } = replaceImportsInInlineCssCode({
|
|
|
|
"cssCode": $(element).html()!,
|
|
|
|
"urlPathname": params.urlPathname,
|
2021-10-11 21:35:40 +02:00
|
|
|
urlOrigin,
|
2021-06-14 21:24:56 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$(element).text(fixedCssCode);
|
|
|
|
});
|
|
|
|
|
2021-10-11 21:35:40 +02:00
|
|
|
(
|
|
|
|
[
|
|
|
|
["link", "href"],
|
|
|
|
["script", "src"],
|
|
|
|
] as const
|
|
|
|
).forEach(([selector, attrName]) =>
|
2021-06-14 21:24:56 +02:00
|
|
|
$(selector).each((...[, element]) => {
|
|
|
|
const href = $(element).attr(attrName);
|
|
|
|
|
|
|
|
if (href === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$(element).attr(
|
|
|
|
attrName,
|
2021-10-11 21:35:40 +02:00
|
|
|
urlOrigin !== undefined
|
|
|
|
? href.replace(/^\//, `${urlOrigin}/`)
|
2021-10-12 00:26:29 +02:00
|
|
|
: href.replace(new RegExp(`^${urlPathname.replace(/\//g, "\\/")}`), "${url.resourcesPath}/build/"),
|
2021-06-14 21:24:56 +02:00
|
|
|
);
|
2021-10-11 21:35:40 +02: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>",
|
|
|
|
"</#if>",
|
|
|
|
].join("\n"),
|
2021-06-14 21:24:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
$("head").prepend(
|
|
|
|
[
|
2021-10-11 21:35:40 +02:00
|
|
|
...(Object.keys(cssGlobalsToDefine).length === 0
|
|
|
|
? []
|
|
|
|
: [
|
|
|
|
"",
|
|
|
|
"<style>",
|
|
|
|
generateCssCodeToDefineGlobals({
|
|
|
|
cssGlobalsToDefine,
|
|
|
|
urlPathname,
|
|
|
|
}).cssCodeToPrependInHead,
|
|
|
|
"</style>",
|
|
|
|
"",
|
|
|
|
]),
|
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>",
|
|
|
|
"",
|
2021-12-08 10:30:36 +03:00
|
|
|
objectKeys(replaceValueBySearchValue)[1],
|
2021-06-14 21:24:56 +02:00
|
|
|
].join("\n"),
|
|
|
|
);
|
|
|
|
|
|
|
|
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.
|
|
|
|
"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
|
|
|
}
|