Things are starting to take form

This commit is contained in:
Joseph Garrone
2021-02-21 18:55:06 +01:00
parent 236ab9dd2b
commit 00f15537a3
10 changed files with 125 additions and 139 deletions

View File

@ -0,0 +1,97 @@
import { transformCodebase } from "../tools/transformCodebase";
import * as fs from "fs";
import { join as pathJoin } from "path";
import {
replaceImportFromStaticInCssCode,
replaceImportFromStaticInJsCode
} from "./replaceImportFromStatic";
import { generateFtlFilesCodeFactory } from "./generateFtl";
/*
const reactAppBuildDirPath = pathJoin(process.cwd(), "build");
assert(
fs.existsSync(reactAppBuildDirPath),
"Run 'react-script build' first (the build dir should be present)"
);
const keycloakDir = pathJoin(reactAppBuildDirPath, "..", "keycloak_build");
*/
const ftlValuesGlobalName = "keycloakFtlValues";
export function generateKeycloakThemeResources(
params: {
themeName: string;
reactAppBuildDirPath: string;
keycloakThemeBuildingDirPath: string;
}
) {
const { themeName, reactAppBuildDirPath, keycloakThemeBuildingDirPath } = params;
const themeDirPath = pathJoin(keycloakThemeBuildingDirPath, "src", "main", "resources", "theme", themeName, "login");
let allCssGlobalsToDefine: Record<string, string> = {};
transformCodebase({
"destDirPath": pathJoin(themeDirPath, "resources"),
"srcDirPath": reactAppBuildDirPath,
"transformSourceCodeString": ({ filePath, sourceCode }) => {
if (/\.css?$/i.test(filePath)) {
const { cssGlobalsToDefine, fixedCssCode } = replaceImportFromStaticInCssCode(
{ "cssCode": sourceCode.toString("utf8") }
);
allCssGlobalsToDefine = {
...allCssGlobalsToDefine,
...cssGlobalsToDefine
};
return { "modifiedSourceCode": Buffer.from(fixedCssCode, "utf8") };
}
if (/\.js?$/i.test(filePath)) {
const { fixedJsCode } = replaceImportFromStaticInJsCode({
"jsCode": sourceCode.toString("utf8"),
ftlValuesGlobalName
});
return { "modifiedSourceCode": Buffer.from(fixedJsCode, "utf8") };
}
return { "modifiedSourceCode": sourceCode };
}
});
const { generateFtlFilesCode } = generateFtlFilesCodeFactory({
"cssGlobalsToDefine": allCssGlobalsToDefine,
ftlValuesGlobalName,
"indexHtmlCode": fs.readFileSync(
pathJoin(reactAppBuildDirPath, "index.html")
).toString("utf8")
});
(["login.ftl", "register.ftl"] as const).forEach(pageBasename => {
const { ftlCode } = generateFtlFilesCode({ pageBasename });
fs.writeFileSync(
pathJoin(themeDirPath, pageBasename),
Buffer.from(ftlCode, "utf8")
)
});
}

View File

@ -1,80 +0,0 @@
import { transformCodebase } from "../tools/transformCodebase";
import * as fs from "fs";
import { join as pathJoin } from "path";
import { assert } from "evt/tools/typeSafety/assert";
import {
replaceImportFromStaticInCssCode,
replaceImportFromStaticInJsCode
} from "./replaceImportFromStatic";
import { generateFtlFilesCodeFactory } from "./generateFtl";
const reactAppBuildDirPath = pathJoin(process.cwd(), "build");
assert(
fs.existsSync(reactAppBuildDirPath),
"Run 'react-script build' first (the build dir should be present)"
);
const keycloakDir = pathJoin(reactAppBuildDirPath, "..", "keycloak_build");
let allCssGlobalsToDefine: Record<string, string> = {};
const ftlValuesGlobalName = "keycloakFtlValues";
transformCodebase({
"destDirPath": pathJoin(keycloakDir, "login", "resources"),
"srcDirPath": reactAppBuildDirPath,
"transformSourceCodeString": ({ filePath, sourceCode }) => {
if (/\.css?$/i.test(filePath)) {
const { cssGlobalsToDefine, fixedCssCode } = replaceImportFromStaticInCssCode(
{ "cssCode": sourceCode.toString("utf8") }
);
allCssGlobalsToDefine = {
...allCssGlobalsToDefine,
...cssGlobalsToDefine
};
return { "modifiedSourceCode": Buffer.from(fixedCssCode, "utf8") };
}
if (/\.js?$/i.test(filePath)) {
const { fixedJsCode } = replaceImportFromStaticInJsCode({
"jsCode": sourceCode.toString("utf8"),
ftlValuesGlobalName
});
return { "modifiedSourceCode": Buffer.from(fixedJsCode, "utf8") };
}
return { "modifiedSourceCode": sourceCode };
}
});
const { generateFtlFilesCode } = generateFtlFilesCodeFactory({
"cssGlobalsToDefine": allCssGlobalsToDefine,
ftlValuesGlobalName,
"indexHtmlCode": fs.readFileSync(
pathJoin(reactAppBuildDirPath, "index.html")
).toString("utf8")
});
(["login.ftl", "register.ftl"] as const).forEach(pageBasename => {
const { ftlCode } = generateFtlFilesCode({ pageBasename });
fs.writeFileSync(
pathJoin(keycloakDir, "login", pageBasename),
Buffer.from(ftlCode, "utf8")
)
});

View File

@ -0,0 +1,24 @@
import * as st from "scripting-tools";
import { join as pathJoin } from "path";
import { generateKeycloakThemeResources } from "../bin/generateKeycloakThemeResources";
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");
generateKeycloakThemeResources({
"themeName": "onyxia-ui",
"reactAppBuildDirPath": pathJoin(process.cwd(), "build"),
"keycloakThemeBuildingDirPath": pathJoin(process.cwd(), "keycloak_build")
});