169 lines
5.2 KiB
TypeScript
Raw Normal View History

2021-02-21 17:38:59 +01:00
import cheerio from "cheerio";
import {
replaceImportFromStaticInJsCode,
generateCssCodeToDefineGlobals
} from "../replaceImportFromStatic";
import fs from "fs";
import { join as pathJoin } from "path";
2021-03-04 21:14:54 +01:00
import { objectKeys } from "evt/tools/typeSafety/objectKeys";
2021-03-06 14:42:56 +01:00
function loadFtlFile(ftlFileBasename: "template.ftl" | "login.ftl" | "register.ftl" | "info.ftl") {
2021-03-04 21:14:54 +01:00
return fs.readFileSync(pathJoin(__dirname, ftlFileBasename))
.toString("utf8")
.match(/^<script>const _=((?:.|\n)+)<\/script>[\n]?$/)![1];
}
2021-02-21 17:38:59 +01:00
export function generateFtlFilesCodeFactory(
params: {
ftlValuesGlobalName: string;
cssGlobalsToDefine: Record<string, string>;
indexHtmlCode: string;
}
) {
const { ftlValuesGlobalName, cssGlobalsToDefine, indexHtmlCode } = params;
const $ = cheerio.load(indexHtmlCode);
$("script:not([src])").each((...[, element]) => {
const { fixedJsCode } = replaceImportFromStaticInJsCode({
ftlValuesGlobalName,
"jsCode": $(element).html()!
});
2021-02-21 18:05:26 +01:00
$(element).text(fixedJsCode);
2021-02-21 17:38:59 +01:00
});
([
["link", "href"],
["script", "src"],
] as const).forEach(([selector, attrName]) =>
$(selector).each((...[, element]) => {
const href = $(element).attr(attrName);
if (!href?.startsWith("/")) {
return;
}
2021-03-02 01:05:15 +01:00
$(element).attr(attrName, "${url.resourcesPath}/build" + href);
2021-02-21 17:38:59 +01:00
})
);
//FTL is no valid html, we can't insert with cheerio, we put placeholder for injecting later.
2021-03-04 21:14:54 +01:00
const ftlCommonPlaceholders = {
'{ "x": "vIdLqMeOed9sdLdIdOxdK0d" }': loadFtlFile("template.ftl"),
'<!-- xIdLqMeOedErIdLsPdNdI9dSlxI -->':
[
'<#if scripts??>',
' <#list scripts as script>',
' <script src="${script}" type="text/javascript"></script>',
' </#list>',
'</#if>',
].join("\n")
};
2021-02-21 17:38:59 +01:00
$("head").prepend(
[
2021-02-23 09:50:24 +01:00
...(Object.keys(cssGlobalsToDefine).length === 0 ? [] : [
'',
'<style>',
generateCssCodeToDefineGlobals(
{ cssGlobalsToDefine }
).cssCodeToPrependInHead,
'</style>',
''
]),
2021-02-21 17:38:59 +01:00
'<script>',
2021-03-04 21:14:54 +01:00
' Object.deepAssign(',
2021-02-21 17:38:59 +01:00
` window.${ftlValuesGlobalName},`,
2021-03-04 21:14:54 +01:00
` ${objectKeys(ftlCommonPlaceholders)[0]}`,
2021-02-23 09:50:24 +01:00
' );',
2021-02-21 17:38:59 +01:00
'</script>',
'',
2021-03-04 21:14:54 +01:00
objectKeys(ftlCommonPlaceholders)[1],
2021-02-21 17:38:59 +01:00
''
].join("\n"),
);
const partiallyFixedIndexHtmlCode = $.html();
function generateFtlFilesCode(
params: {
2021-03-06 14:42:56 +01:00
pageBasename: "login.ftl" | "register.ftl" | "info.ftl"
2021-02-21 17:38:59 +01:00
}
): { ftlCode: string; } {
const { pageBasename } = params;
const $ = cheerio.load(partiallyFixedIndexHtmlCode);
2021-03-04 21:14:54 +01:00
const ftlPlaceholders = {
'{ "x": "kxOlLqMeOed9sdLdIdOxd444" }': loadFtlFile(pageBasename),
...ftlCommonPlaceholders
};
2021-02-21 17:38:59 +01:00
$("head").prepend(
[
'',
'<script>',
2021-03-04 21:14:54 +01:00
'',
` window.${ftlValuesGlobalName} = Object.assign(`,
` { "pageBasename": "${pageBasename}" },`,
` ${objectKeys(ftlPlaceholders)[0]}`,
' );',
'',
' Object.defineProperty(',
' Object,',
' "deepAssign",',
' {',
2021-03-04 21:50:18 +01:00
' "value": function callee(target, source) {',
2021-03-04 21:14:54 +01:00
' Object.keys(source).forEach(function (key) {',
' var value = source[key];',
2021-03-05 00:03:21 +01:00
' if( target[key] === undefined ){',
' target[key]= value;',
' return;',
2021-03-04 21:14:54 +01:00
' }',
2021-03-05 00:03:21 +01:00
' 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;',
2021-03-04 21:14:54 +01:00
' });',
' return target;',
' }',
' }',
' );',
'',
2021-02-21 17:38:59 +01:00
'</script>',
''
].join("\n")
2021-02-21 17:38:59 +01:00
);
let ftlCode = $.html();
objectKeys(ftlPlaceholders)
.forEach(id => ftlCode = ftlCode.replace(id, ftlPlaceholders[id]));
return { ftlCode };
2021-02-21 17:38:59 +01:00
}
return { generateFtlFilesCode };
}