From ee3fd977c1cee183809d4ef746a224b92e7d62ee Mon Sep 17 00:00:00 2001 From: Joseph Garrone Date: Sun, 21 Feb 2021 20:54:33 +0100 Subject: [PATCH] Almoste there --- .gitignore | 2 +- package.json | 2 +- src/bin/generateJavaStackFiles.ts | 95 ++++++++++++++++++++++ src/bin/main.ts | 22 +++++ src/test/generateKeycloakThemeResources.ts | 19 +---- src/test/main.ts | 11 +++ src/test/setupSampleReactProject.ts | 20 +++++ 7 files changed, 154 insertions(+), 17 deletions(-) create mode 100644 src/bin/generateJavaStackFiles.ts create mode 100644 src/bin/main.ts create mode 100644 src/test/main.ts create mode 100644 src/test/setupSampleReactProject.ts diff --git a/.gitignore b/.gitignore index 8e858076..097e3067 100644 --- a/.gitignore +++ b/.gitignore @@ -42,4 +42,4 @@ jspm_packages /dist -/etc_tmp/ +/sample_react_project/ diff --git a/package.json b/package.json index 556138ed..105fe210 100755 --- a/package.json +++ b/package.json @@ -33,10 +33,10 @@ "@types/node": "^10.0.0", "denoify": "^0.6.4", "evt": "beta", - "scripting-tools": "^0.19.13", "typescript": "^4.1.5" }, "dependencies": { + "scripting-tools": "^0.19.13", "cheerio": "^1.0.0-rc.5", "node-html-parser": "^2.1.0" } diff --git a/src/bin/generateJavaStackFiles.ts b/src/bin/generateJavaStackFiles.ts new file mode 100644 index 00000000..c8b8258c --- /dev/null +++ b/src/bin/generateJavaStackFiles.ts @@ -0,0 +1,95 @@ + +import * as url from "url"; +import * as fs from "fs"; +import { join as pathJoin, dirname as pathDirname } from "path"; + +export type ParsedPackageJson = { + name: string; + version: string; + homepage?: string; +}; + +export function generateJavaStackFiles( + params: { + parsedPackageJson: ParsedPackageJson; + keycloakThemeBuildingDirPath: string; + } +): void { + + const { parsedPackageJson, keycloakThemeBuildingDirPath } = params; + + { + + const { pomFileCode } = (function generatePomFileCode(): { pomFileCode: string; } { + + const { name, version, homepage } = parsedPackageJson; + + const groupId = (() => { + + const fallbackGroupId = `there.was.no.homepage.field.in.the.package.json.${name}.keycloak`; + + return !homepage ? + fallbackGroupId : + url.parse(homepage).host?.split(".").reverse().join(".") ?? fallbackGroupId; + + })(); + + const artefactId = `${name}-keycloak-theme`; + + const pomFileCode = [ + ``, + ``, + ` 4.0.0`, + ` ${groupId}`, + ` ${artefactId}`, + ` ${version}`, + ` ${artefactId}`, + ` `, + `` + ].join("\n"); + + return { pomFileCode }; + + })(); + + fs.writeFileSync( + keycloakThemeBuildingDirPath, + Buffer.from(pomFileCode, "utf8") + ); + + } + + { + + const themeManifestFilePath = pathJoin( + keycloakThemeBuildingDirPath, "src", "main", + "resources", "META-INF", "keycloak-themes.json" + ); + + try { + + fs.mkdirSync(pathDirname(themeManifestFilePath)); + + } catch { } + + fs.writeFileSync( + themeManifestFilePath, + Buffer.from( + JSON.stringify({ + "themes": [ + { + "name": "onyxia", + "types": ["login", "email", "account", "welcome"] + } + ] + }, null, 2), + "utf8" + ) + ); + + } + +} + diff --git a/src/bin/main.ts b/src/bin/main.ts new file mode 100644 index 00000000..e85bfbb4 --- /dev/null +++ b/src/bin/main.ts @@ -0,0 +1,22 @@ +import { generateKeycloakThemeResources } from "./generateKeycloakThemeResources"; +import { generateJavaStackFiles } from "./generateJavaStackFiles"; +import type { ParsedPackageJson } from "./generateJavaStackFiles"; +import { join as pathJoin } from "path"; + +const reactProjectDirPath = process.cwd(); + +const parsedPackageJson: ParsedPackageJson = require(pathJoin(reactProjectDirPath, "package.json")); + +const keycloakThemeBuildingDirPath = pathJoin(reactProjectDirPath, "build_keycloak"); + +generateKeycloakThemeResources({ + keycloakThemeBuildingDirPath, + "reactAppBuildDirPath": pathJoin(reactProjectDirPath, "build"), + "themeName": parsedPackageJson.name +}); + +generateJavaStackFiles({ + parsedPackageJson, + keycloakThemeBuildingDirPath +}); + diff --git a/src/test/generateKeycloakThemeResources.ts b/src/test/generateKeycloakThemeResources.ts index cb819daf..d0265dcd 100644 --- a/src/test/generateKeycloakThemeResources.ts +++ b/src/test/generateKeycloakThemeResources.ts @@ -1,24 +1,13 @@ -import * as st from "scripting-tools"; import { join as pathJoin } from "path"; import { generateKeycloakThemeResources } from "../bin/generateKeycloakThemeResources"; +import { setupSampleReactProject } from "./setupSampleReactProject"; -const cwd= pathJoin(__dirname, "..", "..", "etc_tmp"); - -st.execSync(`rm -rf ${cwd}`); -st.execSync(`mkdir ${cwd}`); - -process.chdir(cwd); - -st.execSync("wget https://github.com/garronej/keycloak-react-theming/releases/download/v0.0.1/build.zip"); - -st.execSync("unzip build.zip"); - -st.execSync("rm build.zip"); +const { sampleReactProjectDirPath } = setupSampleReactProject(); generateKeycloakThemeResources({ "themeName": "onyxia-ui", - "reactAppBuildDirPath": pathJoin(process.cwd(), "build"), - "keycloakThemeBuildingDirPath": pathJoin(process.cwd(), "keycloak_build") + "reactAppBuildDirPath": pathJoin(sampleReactProjectDirPath, "build"), + "keycloakThemeBuildingDirPath": pathJoin(sampleReactProjectDirPath, "build_keycloak_theme") }); diff --git a/src/test/main.ts b/src/test/main.ts new file mode 100644 index 00000000..c4448949 --- /dev/null +++ b/src/test/main.ts @@ -0,0 +1,11 @@ + +import { setupSampleReactProject } from "./setupSampleReactProject"; + +const { sampleReactProjectDirPath } = setupSampleReactProject(); + +process.chdir(sampleReactProjectDirPath); + +console.log(`Running main in ${sampleReactProjectDirPath}`); + +import("../bin/main"); + diff --git a/src/test/setupSampleReactProject.ts b/src/test/setupSampleReactProject.ts new file mode 100644 index 00000000..afd45102 --- /dev/null +++ b/src/test/setupSampleReactProject.ts @@ -0,0 +1,20 @@ + +import * as st from "scripting-tools"; +import { join as pathJoin } from "path"; + +export function setupSampleReactProject() { + + const sampleReactProjectDirPath = pathJoin(__dirname, "..", "..", "sample_react_project"); + + st.execSync(`rm -rf ${sampleReactProjectDirPath}`); + st.execSync(`mkdir ${sampleReactProjectDirPath}`); + + [ + "wget https://github.com/garronej/keycloak-react-theming/releases/download/v0.0.1/sample_build_dir_and_package_json.zip", + "unzip build.zip", + "rm build.zip" + ].forEach(cmd => st.execSync(cmd, { "cwd": sampleReactProjectDirPath })); + + return { sampleReactProjectDirPath }; + +} \ No newline at end of file