From 1e500c9344a9386f0650aa1c5f4f311588fdd85a Mon Sep 17 00:00:00 2001 From: Joseph Garrone Date: Wed, 24 Feb 2021 00:27:16 +0100 Subject: [PATCH] Moving on with implementation of the lib --- package.json | 3 +- .../generateFtl/ftl2js.ftl | 113 ++++++++++++++++++ .../{generateFtl.ts => generateFtl/index.ts} | 41 +++++-- src/lib/keycloakFtlValues.ts | 46 ++++++- 4 files changed, 186 insertions(+), 17 deletions(-) create mode 100644 src/bin/build-keycloak-theme/generateFtl/ftl2js.ftl rename src/bin/build-keycloak-theme/{generateFtl.ts => generateFtl/index.ts} (65%) diff --git a/package.json b/package.json index f248e66a..393538e0 100755 --- a/package.json +++ b/package.json @@ -9,8 +9,9 @@ "main": "dist/lib/index.js", "types": "dist/lib/index.d.ts", "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-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", "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" diff --git a/src/bin/build-keycloak-theme/generateFtl/ftl2js.ftl b/src/bin/build-keycloak-theme/generateFtl/ftl2js.ftl new file mode 100644 index 00000000..54486631 --- /dev/null +++ b/src/bin/build-keycloak-theme/generateFtl/ftl2js.ftl @@ -0,0 +1,113 @@ + \ No newline at end of file diff --git a/src/bin/build-keycloak-theme/generateFtl.ts b/src/bin/build-keycloak-theme/generateFtl/index.ts similarity index 65% rename from src/bin/build-keycloak-theme/generateFtl.ts rename to src/bin/build-keycloak-theme/generateFtl/index.ts index 11dc2737..98842d28 100644 --- a/src/bin/build-keycloak-theme/generateFtl.ts +++ b/src/bin/build-keycloak-theme/generateFtl/index.ts @@ -4,7 +4,10 @@ import cheerio from "cheerio"; import { replaceImportFromStaticInJsCode, 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( 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(/^', + ' ', + '', + ].join("\n") + }; + $("head").prepend( [ ...(Object.keys(cssGlobalsToDefine).length === 0 ? [] : [ @@ -58,18 +77,14 @@ export function generateFtlFilesCodeFactory( '', '' ]), - '', + '', + objectKeys(ftlPlaceholders)[1], '' ].join("\n"), ); @@ -94,11 +109,15 @@ export function generateFtlFilesCodeFactory( ` window.${ftlValuesGlobalName} = { "pageBasename": "${pageBasename}" };`, '', '' - ].join("\n"), - + ].join("\n") ); - return { "ftlCode": $.html() }; + let ftlCode = $.html(); + + objectKeys(ftlPlaceholders) + .forEach(id => ftlCode = ftlCode.replace(id, ftlPlaceholders[id])); + + return { ftlCode }; } diff --git a/src/lib/keycloakFtlValues.ts b/src/lib/keycloakFtlValues.ts index 5aa05264..57e1a619 100644 --- a/src/lib/keycloakFtlValues.ts +++ b/src/lib/keycloakFtlValues.ts @@ -3,17 +3,53 @@ import { ftlValuesGlobalName } from "../bin/build-keycloak-theme/generateKeycloakThemeResources"; 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 = { pageBasename: Parameters["generateFtlFilesCode"]>[0]["pageBasename"]; url: { - loginAction: string, - resourcesPath: string - } + loginAction: 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((window as any)[ftlValuesGlobalName]) }; ; \ No newline at end of file