From c52b8cc98e4c02c92b24ea969b29e3c5ef9d033e Mon Sep 17 00:00:00 2001 From: garronej Date: Thu, 7 Oct 2021 21:00:53 +0200 Subject: [PATCH] Feat scrip: download-builtin-keycloak-theme for downloading any version of the builtin themes --- .gitignore | 1 + package.json | 5 +- .../build-keycloak-theme.ts | 10 +- .../generateKeycloakThemeResources.ts | 2 +- ....ts => download-builtin-keycloak-theme.ts} | 32 +++-- src/bin/generate-i18n-messages.ts | 2 +- src/bin/link_in_test_app.ts | 131 ++++++++++++++++++ 7 files changed, 161 insertions(+), 22 deletions(-) rename src/bin/{install-builtin-keycloak-themes.ts => download-builtin-keycloak-theme.ts} (59%) create mode 100644 src/bin/link_in_test_app.ts diff --git a/.gitignore b/.gitignore index 097e3067..6ab01048 100644 --- a/.gitignore +++ b/.gitignore @@ -43,3 +43,4 @@ jspm_packages /dist /sample_react_project/ +/.yarn_home/ diff --git a/package.json b/package.json index 94793340..134cdfcb 100755 --- a/package.json +++ b/package.json @@ -14,11 +14,12 @@ "grant-exec-perms": "node dist/bin/tools/grant-exec-perms.js", "test": "node dist/test/bin/main && node dist/test/lib", "copy-files": "copyfiles -u 1 src/**/*.ftl src/**/*.xml src/**/*.js dist/", - "generate-messages": "node dist/bin/generate-i18n-messages.js" + "generate-messages": "node dist/bin/generate-i18n-messages.js", + "link_in_test_app": "node dist/bin/link_in_test_app.js" }, "bin": { "build-keycloak-theme": "dist/bin/build-keycloak-theme/index.js", - "install-builtin-keycloak-themes": "dist/bin/install-builtin-keycloak-themes.js" + "download-builtin-keycloak-theme": "dist/bin/download-builtin-keycloak-theme.js" }, "author": "u/garronej", "license": "MIT", diff --git a/src/bin/build-keycloak-theme/build-keycloak-theme.ts b/src/bin/build-keycloak-theme/build-keycloak-theme.ts index 2d401957..4ce76dc8 100644 --- a/src/bin/build-keycloak-theme/build-keycloak-theme.ts +++ b/src/bin/build-keycloak-theme/build-keycloak-theme.ts @@ -4,7 +4,6 @@ import { join as pathJoin, relative as pathRelative, basename as pathBasename } import * as child_process from "child_process"; import { generateDebugFiles, containerLaunchScriptBasename } from "./generateDebugFiles"; import { URL } from "url"; -import type { KeycloakVersion } from "../KeycloakVersion"; type ParsedPackageJson = { name: string; @@ -12,8 +11,6 @@ type ParsedPackageJson = { homepage?: string; }; -const keycloakVersion: KeycloakVersion = "15.0.2"; - const reactProjectDirPath = process.cwd(); const doUseExternalAssets = process.argv[2]?.toLowerCase() === "--external-assets"; @@ -70,7 +67,10 @@ export function main() { })(), extraPagesId, extraThemeProperties, - keycloakVersion + //We have to leave it at that otherwise we break our default theme. + //Problem is that we can't guarantee that the the old resources common + //will still be available on the newer keycloak version. + "keycloakVersion": "11.0.3" }); const { jarFilePath } = generateJavaStackFiles({ @@ -88,7 +88,7 @@ export function main() { generateDebugFiles({ keycloakThemeBuildingDirPath, themeName, - keycloakVersion + "keycloakVersion": "15.0.2" }); console.log([ diff --git a/src/bin/build-keycloak-theme/generateKeycloakThemeResources.ts b/src/bin/build-keycloak-theme/generateKeycloakThemeResources.ts index baf99979..95d201be 100644 --- a/src/bin/build-keycloak-theme/generateKeycloakThemeResources.ts +++ b/src/bin/build-keycloak-theme/generateKeycloakThemeResources.ts @@ -7,7 +7,7 @@ import { replaceImportsFromStaticInJsCode } from "./replaceImportFromStatic"; import { generateFtlFilesCodeFactory, pageIds } from "./generateFtl"; -import { downloadBuiltinKeycloakTheme } from "../install-builtin-keycloak-themes"; +import { downloadBuiltinKeycloakTheme } from "../download-builtin-keycloak-theme"; import * as child_process from "child_process"; import { resourcesCommonPath, resourcesPath, subDirOfPublicDirBasename } from "../../lib/getKcContext/kcContextMocks/urlResourcesPath"; import { isInside } from "../tools/isInside"; diff --git a/src/bin/install-builtin-keycloak-themes.ts b/src/bin/download-builtin-keycloak-theme.ts similarity index 59% rename from src/bin/install-builtin-keycloak-themes.ts rename to src/bin/download-builtin-keycloak-theme.ts index 22026116..511b295a 100644 --- a/src/bin/install-builtin-keycloak-themes.ts +++ b/src/bin/download-builtin-keycloak-theme.ts @@ -3,7 +3,7 @@ import { keycloakThemeBuildingDirPath } from "./build-keycloak-theme"; import { join as pathJoin } from "path"; import { downloadAndUnzip } from "./tools/downloadAndUnzip" -import type { KeycloakVersion } from "../bin/KeycloakVersion"; +import type { KeycloakVersion } from "./KeycloakVersion"; export function downloadBuiltinKeycloakTheme( params: { @@ -28,19 +28,25 @@ export function downloadBuiltinKeycloakTheme( if (require.main === module) { + const keycloakVersion = (() => { + + const keycloakVersion = process.argv[2] as (KeycloakVersion | undefined); + + if (keycloakVersion === undefined) { + return "15.0.2"; + } + + return keycloakVersion; + + })(); + + const destDirPath = pathJoin(keycloakThemeBuildingDirPath, "src", "main", "resources", "theme"); + + console.log(`Downloading builtins theme of Keycloak ${keycloakVersion} here ${destDirPath}`); + downloadBuiltinKeycloakTheme({ - "keycloakVersion": (() => { - - const keycloakVersion = process.argv[2] as (KeycloakVersion | undefined); - - if (keycloakVersion === undefined) { - return "15.0.2"; - } - - return keycloakVersion; - - })(), - "destDirPath": pathJoin(keycloakThemeBuildingDirPath, "src", "main", "resources", "theme") + keycloakVersion, + destDirPath }); } diff --git a/src/bin/generate-i18n-messages.ts b/src/bin/generate-i18n-messages.ts index 4395ecf6..1d399c0f 100644 --- a/src/bin/generate-i18n-messages.ts +++ b/src/bin/generate-i18n-messages.ts @@ -2,7 +2,7 @@ import "minimal-polyfills/Object.fromEntries"; import * as fs from "fs"; import { join as pathJoin, relative as pathRelative } from "path"; import { crawl } from "./tools/crawl"; -import { downloadBuiltinKeycloakTheme } from "./install-builtin-keycloak-themes"; +import { downloadBuiltinKeycloakTheme } from "./download-builtin-keycloak-theme"; import { getProjectRoot } from "./tools/getProjectRoot"; import { rm_rf, rm_r } from "./tools/rm"; import { keycloakVersions } from "./KeycloakVersion"; diff --git a/src/bin/link_in_test_app.ts b/src/bin/link_in_test_app.ts new file mode 100644 index 00000000..38177f0d --- /dev/null +++ b/src/bin/link_in_test_app.ts @@ -0,0 +1,131 @@ + +import { execSync } from "child_process"; +import { join as pathJoin, relative as pathRelative } from "path"; +import * as fs from "fs"; + +const keycloakifyDirPath = pathJoin(__dirname, "..", ".."); + +fs.writeFileSync( + pathJoin(keycloakifyDirPath, "dist", "package.json"), + Buffer.from( + JSON.stringify( + (() => { + const packageJsonParsed = JSON.parse( + fs + .readFileSync(pathJoin(keycloakifyDirPath, "package.json")) + .toString("utf8"), + ); + + return { + ...packageJsonParsed, + "main": packageJsonParsed["main"].replace(/^dist\//, ""), + "types": packageJsonParsed["types"].replace(/^dist\//, ""), + }; + })(), + null, + 2, + ), + "utf8", + ), +); + +const commonThirdPartyDeps = (() => { + const namespaceModuleNames = ["@emotion"]; + const standaloneModuleNames = ["react", "@types/react", "powerhooks", "tss-react", "evt"]; + + return [ + ...namespaceModuleNames + .map(namespaceModuleName => + fs + .readdirSync( + pathJoin( + keycloakifyDirPath, + "node_modules", + namespaceModuleName, + ), + ) + .map( + submoduleName => + `${namespaceModuleName}/${submoduleName}`, + ), + ) + .reduce((prev, curr) => [...prev, ...curr], []), + ...standaloneModuleNames, + ]; +})(); + +const yarnHomeDirPath = pathJoin(keycloakifyDirPath, ".yarn_home"); + +execSync( + ["rm -rf", "mkdir"].map(cmd => `${cmd} ${yarnHomeDirPath}`).join(" && "), +); + +const execYarnLink = (params: { targetModuleName?: string; cwd: string }) => { + const { targetModuleName, cwd } = params; + + const cmd = [ + "yarn", + "link", + ...(targetModuleName !== undefined ? [targetModuleName] : []), + ].join(" "); + + console.log(`$ cd ${pathRelative(keycloakifyDirPath, cwd) || "."} && ${cmd}`); + + execSync(cmd, { + cwd, + "env": { + ...process.env, + "HOME": yarnHomeDirPath, + }, + }); +}; + +const testAppNames = ["keycloakify-demo-app"] as const; + +const getTestAppPath = (testAppName: typeof testAppNames[number]) => + pathJoin(keycloakifyDirPath, "..", testAppName); + +testAppNames.forEach(testAppName => + execSync("yarn install", { "cwd": getTestAppPath(testAppName) }), +); + +console.log("=== Linking common dependencies ==="); + +const total = commonThirdPartyDeps.length; +let current = 0; + +commonThirdPartyDeps.forEach(commonThirdPartyDep => { + current++; + + console.log(`${current}/${total} ${commonThirdPartyDep}`); + + const localInstallPath = pathJoin( + ...[ + keycloakifyDirPath, + "node_modules", + ...(commonThirdPartyDep.startsWith("@") + ? commonThirdPartyDep.split("/") + : [commonThirdPartyDep]), + ], + ); + + execYarnLink({ "cwd": localInstallPath }); + + testAppNames.forEach(testAppName => + execYarnLink({ + "cwd": getTestAppPath(testAppName), + "targetModuleName": commonThirdPartyDep, + }), + ); +}); + +console.log("=== Linking in house dependencies ==="); + +execYarnLink({ "cwd": pathJoin(keycloakifyDirPath, "dist") }); + +testAppNames.forEach(testAppName => + execYarnLink({ + "cwd": getTestAppPath(testAppName), + "targetModuleName": "keycloakify", + }), +); \ No newline at end of file