2024-08-13 09:25:06 +02:00
|
|
|
import * as cheerio from "cheerio";
|
2024-06-23 21:23:06 +02:00
|
|
|
import {
|
|
|
|
replaceImportsInJsCode,
|
|
|
|
BuildContextLike as BuildContextLike_replaceImportsInJsCode
|
|
|
|
} from "../replacers/replaceImportsInJsCode";
|
|
|
|
import {
|
|
|
|
replaceImportsInCssCode,
|
|
|
|
BuildContextLike as BuildContextLike_replaceImportsInCssCode
|
|
|
|
} from "../replacers/replaceImportsInCssCode";
|
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";
|
2024-06-09 09:15:16 +02:00
|
|
|
import type { BuildContext } from "../../shared/buildContext";
|
2022-08-16 14:41:06 +07:00
|
|
|
import { assert } from "tsafe/assert";
|
2024-09-08 00:06:47 +02:00
|
|
|
import { type ThemeType, WELL_KNOWN_DIRECTORY_BASE_NAME } from "../../shared/constants";
|
2024-05-19 04:21:35 +02:00
|
|
|
import { getThisCodebaseRootDirPath } from "../../tools/getThisCodebaseRootDirPath";
|
2023-03-16 22:13:46 +01:00
|
|
|
|
2024-06-23 21:23:06 +02:00
|
|
|
export type BuildContextLike = BuildContextLike_replaceImportsInJsCode &
|
|
|
|
BuildContextLike_replaceImportsInCssCode & {
|
|
|
|
urlPathname: string | undefined;
|
|
|
|
themeVersion: string;
|
|
|
|
kcContextExclusionsFtlCode: string | undefined;
|
|
|
|
};
|
2022-08-16 14:41:06 +07:00
|
|
|
|
2024-06-09 09:15:16 +02:00
|
|
|
assert<BuildContext extends BuildContextLike ? true : false>();
|
2022-08-16 14:41:06 +07:00
|
|
|
|
2021-10-11 21:35:40 +02:00
|
|
|
export function generateFtlFilesCodeFactory(params: {
|
2023-09-04 01:19:21 +02:00
|
|
|
themeName: string;
|
2021-10-11 21:35:40 +02:00
|
|
|
indexHtmlCode: string;
|
2024-06-09 09:15:16 +02:00
|
|
|
buildContext: BuildContextLike;
|
2023-04-04 01:40:55 +02:00
|
|
|
keycloakifyVersion: string;
|
2023-04-27 11:52:02 +02:00
|
|
|
themeType: ThemeType;
|
2023-06-19 00:09:21 +02:00
|
|
|
fieldNames: string[];
|
2021-10-11 21:35:40 +02:00
|
|
|
}) {
|
2024-05-20 15:48:51 +02:00
|
|
|
const {
|
|
|
|
themeName,
|
|
|
|
indexHtmlCode,
|
2024-06-09 09:15:16 +02:00
|
|
|
buildContext,
|
2024-05-20 15:48:51 +02:00
|
|
|
keycloakifyVersion,
|
|
|
|
themeType,
|
2024-07-13 18:17:21 +02:00
|
|
|
fieldNames
|
2024-05-20 15:48:51 +02:00
|
|
|
} = params;
|
2021-06-14 21:24:56 +02:00
|
|
|
|
|
|
|
const $ = cheerio.load(indexHtmlCode);
|
|
|
|
|
2022-08-16 14:41:06 +07:00
|
|
|
fix_imports_statements: {
|
|
|
|
$("script:not([src])").each((...[, element]) => {
|
2023-08-21 05:54:17 +02:00
|
|
|
const jsCode = $(element).html();
|
|
|
|
|
|
|
|
assert(jsCode !== null);
|
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
const { fixedJsCode } = replaceImportsInJsCode({
|
|
|
|
jsCode,
|
2024-06-09 09:15:16 +02:00
|
|
|
buildContext
|
2024-05-20 15:48:51 +02: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]) => {
|
2023-08-21 05:54:17 +02:00
|
|
|
const cssCode = $(element).html();
|
|
|
|
|
|
|
|
assert(cssCode !== null);
|
|
|
|
|
2024-06-19 01:41:22 +02:00
|
|
|
const { fixedCssCode } = replaceImportsInCssCode({
|
2023-08-21 05:54:17 +02:00
|
|
|
cssCode,
|
2024-06-19 22:41:25 +02:00
|
|
|
cssFileRelativeDirPath: undefined,
|
2024-07-13 18:17:21 +02:00
|
|
|
buildContext
|
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"],
|
2024-08-06 06:41:25 +02:00
|
|
|
["script", "src"],
|
|
|
|
["script", "data-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,
|
2024-01-30 00:06:17 +01:00
|
|
|
href.replace(
|
2024-05-20 15:48:51 +02:00
|
|
|
new RegExp(
|
2024-06-09 09:15:16 +02:00
|
|
|
`^${(buildContext.urlPathname ?? "/").replace(/\//g, "\\/")}`
|
2024-05-20 15:48:51 +02:00
|
|
|
),
|
2024-09-08 00:06:47 +02:00
|
|
|
`\${xKeycloakify.resourcesPath}/${WELL_KNOWN_DIRECTORY_BASE_NAME.DIST}/`
|
2024-01-30 00:06:17 +01:00
|
|
|
)
|
2022-08-16 14:41:06 +07:00
|
|
|
);
|
2022-08-20 11:44:48 +07: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.
|
2024-06-14 22:24:51 +02:00
|
|
|
const kcContextDeclarationTemplateFtl = fs
|
2024-05-19 04:21:35 +02:00
|
|
|
.readFileSync(
|
2024-05-20 15:48:51 +02:00
|
|
|
pathJoin(
|
|
|
|
getThisCodebaseRootDirPath(),
|
|
|
|
"src",
|
|
|
|
"bin",
|
|
|
|
"keycloakify",
|
|
|
|
"generateFtl",
|
2024-06-14 22:24:51 +02:00
|
|
|
"kcContextDeclarationTemplate.ftl"
|
2024-05-20 15:48:51 +02:00
|
|
|
)
|
2024-05-19 04:21:35 +02:00
|
|
|
)
|
2024-05-07 20:04:27 +02:00
|
|
|
.toString("utf8")
|
2024-07-13 18:17:21 +02:00
|
|
|
.replace("{{themeType}}", themeType)
|
|
|
|
.replace("{{themeName}}", themeName)
|
|
|
|
.replace("{{keycloakifyVersion}}", keycloakifyVersion)
|
|
|
|
.replace("{{themeVersion}}", buildContext.themeVersion)
|
|
|
|
.replace("{{fieldNames}}", fieldNames.map(name => `"${name}"`).join(", "))
|
2024-09-08 00:06:47 +02:00
|
|
|
.replace("{{RESOURCES_COMMON}}", WELL_KNOWN_DIRECTORY_BASE_NAME.RESOURCES_COMMON)
|
2024-05-20 15:48:51 +02:00
|
|
|
.replace(
|
2024-07-13 18:17:21 +02:00
|
|
|
"{{userDefinedExclusions}}",
|
2024-06-09 09:15:16 +02:00
|
|
|
buildContext.kcContextExclusionsFtlCode ?? ""
|
2024-05-27 17:18:06 +02:00
|
|
|
);
|
2024-07-13 18:17:21 +02:00
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
const ftlObjectToJsCodeDeclaringAnObjectPlaceholder =
|
|
|
|
'{ "x": "vIdLqMeOed9sdLdIdOxdK0d" }';
|
2024-05-07 20:04:27 +02:00
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
$("head").prepend(
|
2024-06-14 22:24:51 +02:00
|
|
|
`<script>\n${ftlObjectToJsCodeDeclaringAnObjectPlaceholder}\n</script>`
|
2024-05-20 15:48:51 +02:00
|
|
|
);
|
2021-06-14 21:24:56 +02:00
|
|
|
|
2023-11-19 03:27:40 +01:00
|
|
|
// Remove part of the document marked as ignored.
|
|
|
|
{
|
|
|
|
const startTags = $('meta[name="keycloakify-ignore-start"]');
|
|
|
|
|
|
|
|
startTags.each((...[, startTag]) => {
|
|
|
|
const $startTag = $(startTag);
|
2024-05-20 15:48:51 +02:00
|
|
|
const $endTag = $startTag
|
|
|
|
.nextAll('meta[name="keycloakify-ignore-end"]')
|
|
|
|
.first();
|
2023-11-19 03:27:40 +01:00
|
|
|
|
|
|
|
if ($endTag.length) {
|
|
|
|
let currentNode = $startTag.next();
|
|
|
|
while (currentNode.length && !currentNode.is($endTag)) {
|
|
|
|
currentNode.remove();
|
|
|
|
currentNode = $startTag.next();
|
|
|
|
}
|
|
|
|
|
|
|
|
$startTag.remove();
|
|
|
|
$endTag.remove();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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({
|
2024-05-20 15:48:51 +02:00
|
|
|
[ftlObjectToJsCodeDeclaringAnObjectPlaceholder]:
|
2024-06-14 22:24:51 +02:00
|
|
|
kcContextDeclarationTemplateFtl,
|
2024-07-13 18:17:21 +02:00
|
|
|
"{{pageId}}": pageId,
|
|
|
|
"{{ftlTemplateFileName}}": pageId
|
2024-05-20 15:48:51 +02: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
|
|
|
}
|