Fix build

This commit is contained in:
Joseph Garrone
2021-03-07 01:47:03 +01:00
parent de312c60b1
commit ad78221025
7 changed files with 110 additions and 66 deletions

View File

@ -0,0 +1,28 @@
Object.defineProperty(
Object,
"deepAssign",
{
"value": function callee(target, source) {
Object.keys(source).forEach(function (key) {
var value = source[key];
if (target[key] === undefined) {
target[key] = value;
return;
}
if (value instanceof Object) {
if (value instanceof Array) {
value.forEach(function (entry) {
target[key].push(entry);
});
return;
}
callee(target[key], value);
return;
}
target[key] = value;
});
return target;
}
}
);

View File

@ -0,0 +1,26 @@
var es = /&(?:amp|#38|lt|#60|gt|#62|apos|#39|quot|#34);/g;
var unes = {
'&': '&',
'&': '&',
'&lt;': '<',
'&#60;': '<',
'&gt;': '>',
'&#62;': '>',
'&apos;': "'",
'&#39;': "'",
'&quot;': '"',
'&#34;': '"'
};
var cape = function (m) { return unes[m]; };
Object.defineProperty(
String,
"htmlUnescape",
{
"value": function (un) {
return String.prototype.replace.call(un, es, cape);
}
}
);

View File

@ -9,15 +9,20 @@ import fs from "fs";
import { join as pathJoin } from "path";
import { objectKeys } from "evt/tools/typeSafety/objectKeys";
export type PageId = "login.ftl" | "register.ftl" | "info.ftl" | "error.ftl";
export const pageIds= [ "login.ftl", "register.ftl", "info.ftl", "error.ftl"] as const;
export type PageId = typeof pageIds[number];
function loadAdjacentFile(fileBasename: string){
return fs.readFileSync(pathJoin(__dirname, fileBasename))
.toString("utf8");
};
function loadFtlFile(ftlFileBasename: PageId | "template.ftl") {
return fs.readFileSync(pathJoin(__dirname, ftlFileBasename))
.toString("utf8")
return loadAdjacentFile(ftlFileBasename)
.match(/^<script>const _=((?:.|\n)+)<\/script>[\n]?$/)![1];
}
export function generateFtlFilesCodeFactory(
params: {
ftlValuesGlobalName: string;
@ -41,7 +46,6 @@ export function generateFtlFilesCodeFactory(
});
([
["link", "href"],
["script", "src"],
@ -68,10 +72,12 @@ export function generateFtlFilesCodeFactory(
' <#list scripts as script>',
' <script src="${script}" type="text/javascript"></script>',
' </#list>',
'</#if>',
'</#if>'
].join("\n")
};
const pageSpecificCodePlaceholder = "<!-- dIddLqMeOedErIdLsPdNdI9dSl42sw -->";
$("head").prepend(
[
...(Object.keys(cssGlobalsToDefine).length === 0 ? [] : [
@ -83,19 +89,26 @@ export function generateFtlFilesCodeFactory(
'</style>',
''
]),
...["Object.deepAssign.js", "String.htmlUnescape.js"].map(
fileBasename => [
"<script>",
loadAdjacentFile(fileBasename),
"</script>"
].join("\n")
),
'<script>',
' Object.deepAssign(',
` window.${ftlValuesGlobalName},`,
` window.${ftlValuesGlobalName}= Object.assign(`,
` {},`,
` ${objectKeys(ftlCommonPlaceholders)[0]}`,
' );',
'</script>',
'',
objectKeys(ftlCommonPlaceholders)[1],
''
pageSpecificCodePlaceholder,
'',
objectKeys(ftlCommonPlaceholders)[1]
].join("\n"),
);
const partiallyFixedIndexHtmlCode = $.html();
function generateFtlFilesCode(
@ -113,50 +126,22 @@ export function generateFtlFilesCodeFactory(
...ftlCommonPlaceholders
};
$("head").prepend(
[
'',
'<script>',
'',
` window.${ftlValuesGlobalName} = Object.assign(`,
` { "pageId": "${pageId}" },`,
` ${objectKeys(ftlPlaceholders)[0]}`,
' );',
'',
' Object.defineProperty(',
' Object,',
' "deepAssign",',
' {',
' "value": function callee(target, source) {',
' Object.keys(source).forEach(function (key) {',
' var value = source[key];',
' if( target[key] === undefined ){',
' target[key]= value;',
' return;',
' }',
' if( value instanceof Object ){',
' if( value instanceof Array ){',
' value.forEach(function (entry){',
' target[key].push(entry);',
' });',
' return;',
' }',
' callee(target[key], value);',
' return;',
' }',
' target[key]= value;',
' });',
' return target;',
' }',
' }',
' );',
'',
'</script>',
''
].join("\n")
);
let ftlCode = $.html();
let ftlCode = $.html()
.replace(
pageSpecificCodePlaceholder,
[
'<script>',
` Object.assign(`,
` window.${ftlValuesGlobalName},`,
` { "pageId": "${pageId}" }`,
' );',
` Object.assign(`,
` window.${ftlValuesGlobalName},`,
` ${objectKeys(ftlPlaceholders)[0]}`,
' );',
'</script>'
].join("\n")
);
objectKeys(ftlPlaceholders)
.forEach(id => ftlCode = ftlCode.replace(id, ftlPlaceholders[id]));

View File

@ -94,7 +94,7 @@
return { 
"type": "${message.type}",
"summary": "${kcSanitize(message.summary)?no_esc}"
"summary": String.htmlUnescape("${message.summary}")
};
</#if>

View File

@ -6,7 +6,7 @@ import {
replaceImportFromStaticInCssCode,
replaceImportFromStaticInJsCode
} from "./replaceImportFromStatic";
import { generateFtlFilesCodeFactory } from "./generateFtl";
import { generateFtlFilesCodeFactory, pageIds } from "./generateFtl";
import { builtinThemesUrl } from "../install-builtin-keycloak-themes";
import { downloadAndUnzip } from "../tools/downloadAndUnzip";
import * as child_process from "child_process";
@ -70,14 +70,14 @@ export function generateKeycloakThemeResources(
).toString("utf8")
});
(["login.ftl", "register.ftl"] as const).forEach(pageId => {
pageIds.forEach(pageId => {
const { ftlCode } = generateFtlFilesCode({ pageId });
fs.writeFileSync(
pathJoin(themeDirPath, pageId),
Buffer.from(ftlCode, "utf8")
)
);
});