Moving on with implementation of the lib

This commit is contained in:
Joseph Garrone
2021-02-24 00:27:16 +01:00
parent de3dce2598
commit 1e500c9344
4 changed files with 186 additions and 17 deletions

View File

@ -9,8 +9,9 @@
"main": "dist/lib/index.js", "main": "dist/lib/index.js",
"types": "dist/lib/index.d.ts", "types": "dist/lib/index.d.ts",
"scripts": { "scripts": {
"build": "tsc && npm run grant-exec-perms && npm run copy-standalone-ha", "build": "tsc && npm run grant-exec-perms && npm run copy-standalone-ha && npm run copy-ftl2js",
"copy-standalone-ha": "cp src/bin/build-keycloak-theme/generateDebugFiles/standalone-ha.xml dist/bin/build-keycloak-theme/generateDebugFiles/", "copy-standalone-ha": "cp src/bin/build-keycloak-theme/generateDebugFiles/standalone-ha.xml dist/bin/build-keycloak-theme/generateDebugFiles/",
"copy-ftl2js": "cp src/bin/build-keycloak-theme/generateFtl/ftl2js.ftl dist/bin/build-keycloak-theme/generateFtl/",
"grant-exec-perms": "cd dist/bin && chmod +x build-keycloak-theme/index.js download-sample-keycloak-themes.js", "grant-exec-perms": "cd dist/bin && chmod +x build-keycloak-theme/index.js download-sample-keycloak-themes.js",
"test": "node dist/test/build-keycloak-theme && node dist/test/download-sample-keycloak-themes", "test": "node dist/test/build-keycloak-theme && node dist/test/download-sample-keycloak-themes",
"enable_short_import_path": "npm run build && denoify_enable_short_npm_import_path" "enable_short_import_path": "npm run build && denoify_enable_short_npm_import_path"

View File

@ -0,0 +1,113 @@
<script>const _=
{
"url": {
"loginAction": "${url.loginAction}",
"resourcesPath": "${url.resourcesPath}",
"resourcesCommonPath": "${url.resourcesCommonPath}",
"loginRestartFlowUrl": "${url.loginRestartFlowUrl}"
},
"realm": {
"displayName": "${realm.displayName!''}" || undefined,
"displayNameHtml": "${realm.displayNameHtml!''}" || undefined,
"internationalizationEnabled": ${realm.internationalizationEnabled?c}
},
"locale": (function (){
<#if realm.internationalizationEnabled>
return {
"supported": (function(){
<#if realm.internationalizationEnabled>
var out= [];
<#list locale.supported as lng>
out.push({
"url": "${lng.url}",
"label": "${lng.label}",
"languageTag": "${lng.languageTag}"
});
</#list>
return out;
</#if>
return undefined;
})(),
"current": "${locale.current}"
};
</#if>
return undefined;
})(),
"auth": (function (){
<#if auth?has_content>
var out= {
"showUsername": ${auth.showUsername()?c},
"showResetCredentials": ${auth.showResetCredentials()?c},
"showTryAnotherWayLink": ${auth.showTryAnotherWayLink()?c}
};
<#if auth.showUsername() && !auth.showResetCredentials()>
Object.assign(
out,
{
"attemptedUsername": "${auth.attemptedUsername}"
}
);
</#if>
return out;
</#if>
return undefined;
})(),
"scripts": (function(){
var out = [];
<#if scripts??>
<#list scripts as script>
out.push("${script}");
</#list>
</#if>
return out;
})(),
"message": (function (){
<#if message?has_content>
return { 
"type": "${message.type}",
"summary": "${kcSanitize(message.summary)?no_esc}"
};
</#if>
return undefined;
})(),
"isAppInitiatedAction": (function (){
<#if isAppInitiatedAction??>
return true;
</#if>
return false;
})()
}
</script>

View File

@ -4,7 +4,10 @@ import cheerio from "cheerio";
import { import {
replaceImportFromStaticInJsCode, replaceImportFromStaticInJsCode,
generateCssCodeToDefineGlobals generateCssCodeToDefineGlobals
} from "./replaceImportFromStatic"; } from "../replaceImportFromStatic";
import fs from "fs";
import { join as pathJoin } from "path";
import { objectKeys } from "evt/tools/typeSafety/objectKeys";
export function generateFtlFilesCodeFactory( export function generateFtlFilesCodeFactory(
params: { params: {
@ -47,6 +50,22 @@ export function generateFtlFilesCodeFactory(
}) })
); );
//FTL is no valid html, we can't insert with cheerio, we put placeholder for injecting later.
const ftlPlaceholders = {
'{ "x": "xIdLqMeOed9sdLdIdOxdK0d" }':
fs.readFileSync(pathJoin(__dirname, "ftl2js.ftl"))
.toString("utf8")
.match(/^<script>const _=((?:.|\n)+)<\/script>[\n]?$/)![1],
'<!-- xIdLqMeOedErIdLsPdNdI9dSlxI -->':
[
'<#if scripts??>',
' <#list scripts as script>',
' <script src="${script}" type="text/javascript"></script>',
' </#list>',
'</#if>',
].join("\n")
};
$("head").prepend( $("head").prepend(
[ [
...(Object.keys(cssGlobalsToDefine).length === 0 ? [] : [ ...(Object.keys(cssGlobalsToDefine).length === 0 ? [] : [
@ -58,18 +77,14 @@ export function generateFtlFilesCodeFactory(
'</style>', '</style>',
'' ''
]), ]),
'<script>', '<script>',
' Object.assign(', ' Object.assign(',
` window.${ftlValuesGlobalName},`, ` window.${ftlValuesGlobalName},`,
' {', ` ${objectKeys(ftlPlaceholders)[0]}`,
' "url": {',
' "loginAction": "${url.loginAction}",',
' "resourcesPath": "${url.resourcesPath}"',
' }',
' }',
' );', ' );',
'</script>', '</script>',
'',
objectKeys(ftlPlaceholders)[1],
'' ''
].join("\n"), ].join("\n"),
); );
@ -94,11 +109,15 @@ export function generateFtlFilesCodeFactory(
` window.${ftlValuesGlobalName} = { "pageBasename": "${pageBasename}" };`, ` window.${ftlValuesGlobalName} = { "pageBasename": "${pageBasename}" };`,
'</script>', '</script>',
'' ''
].join("\n"), ].join("\n")
); );
return { "ftlCode": $.html() }; let ftlCode = $.html();
objectKeys(ftlPlaceholders)
.forEach(id => ftlCode = ftlCode.replace(id, ftlPlaceholders[id]));
return { ftlCode };
} }

View File

@ -3,17 +3,53 @@
import { ftlValuesGlobalName } from "../bin/build-keycloak-theme/generateKeycloakThemeResources"; import { ftlValuesGlobalName } from "../bin/build-keycloak-theme/generateKeycloakThemeResources";
import type { generateFtlFilesCodeFactory } from "../bin/build-keycloak-theme/generateFtl"; import type { generateFtlFilesCodeFactory } from "../bin/build-keycloak-theme/generateFtl";
import { id } from "evt/tools/typeSafety/id"; import { id } from "evt/tools/typeSafety/id";
export type LanguageLabel =
/* spell-checker: disable */
"Deutsch" | "Norsk" | "Русский" | "Svenska" | "Português (Brasil)" | "Lietuvių" |
"English" | "Italiano" | "Français" | "中文简体" | "Español" | "Čeština" | "日本語" |
"Slovenčina" | "Polish" | "Català" | "Nederlands" | "tr";
/* spell-checker: enable */
export type LanguageTag = "de" | "no" | "ru" | "sv" | "pt-BR" | "lt" | "en" | "it" | "fr" | "zh-CN" | "es" | "cs" | "ja" | "sk" | "pl" | "ca" | "nl" | "tr";
export type KeycloakFtlValues = { export type KeycloakFtlValues = {
pageBasename: Parameters<ReturnType<typeof generateFtlFilesCodeFactory>["generateFtlFilesCode"]>[0]["pageBasename"]; pageBasename: Parameters<ReturnType<typeof generateFtlFilesCodeFactory>["generateFtlFilesCode"]>[0]["pageBasename"];
url: { url: {
loginAction: string, loginAction: string;
resourcesPath: string resourcesPath: string;
} resourcesCommonPath: string;
loginRestartFlowUrl: string;
},
realm: {
displayName?: string;
displayNameHtml?: string;
internationalizationEnabled: boolean;
},
//NOTE: Undefined if !realm.internationalizationEnabled
locale?: {
supported: {
url: string;
label: LanguageLabel;
languageTag: LanguageTag;
},
current: LanguageLabel;
},
auth?: {
showUsername: boolean;
showResetCredentials: boolean;
showTryAnotherWayLink: boolean;
attemptedUsername?: boolean;
},
scripts: string[];
message?: {
type: "success" | "warning" | "error" | "info";
summary: string;
},
isAppInitiatedAction: boolean;
}; };
export const { keycloakPagesContext } = export const { keycloakPagesContext } =
{ [ftlValuesGlobalName]: id<KeycloakFtlValues | undefined>((window as any)[ftlValuesGlobalName]) }; { [ftlValuesGlobalName]: id<KeycloakFtlValues | undefined>((window as any)[ftlValuesGlobalName]) };
; ;