From 2a5a4c16ea24e4407aebefabf7e5a2069dbfcc5b Mon Sep 17 00:00:00 2001
From: Joseph Garrone <joseph.garrone.gj@gmail.com>
Date: Wed, 3 Mar 2021 02:31:02 +0100
Subject: [PATCH] fix build

---
 package.json                                  |  2 +-
 .../generateKeycloakThemeResources.ts         | 25 +++++++++--------
 src/bin/download-sample-keycloak-themes.ts    |  2 +-
 src/bin/tools/downloadAndUnzip.ts             | 26 ++++++++++++-----
 src/bin/tools/transformCodebase.ts            | 28 ++++++++++++-------
 src/test/build-keycloak-theme.ts              | 18 ++++--------
 src/test/download-sample-keycloak-themes.ts   | 12 +++-----
 src/test/setupSampleReactProject.ts           | 22 ++++++---------
 8 files changed, 70 insertions(+), 65 deletions(-)

diff --git a/package.json b/package.json
index 13f26b78..9948c769 100755
--- a/package.json
+++ b/package.json
@@ -49,10 +49,10 @@
         "properties-parser": "^0.3.1",
         "react": "^17.0.1",
         "rimraf": "^3.0.2",
-        "scripting-tools": "^0.19.13",
         "typescript": "^4.1.5"
     },
     "dependencies": {
+        "scripting-tools": "^0.19.13",
         "cheerio": "^1.0.0-rc.5",
         "evt": "^1.9.12",
         "minimal-polyfills": "^2.1.6",
diff --git a/src/bin/build-keycloak-theme/generateKeycloakThemeResources.ts b/src/bin/build-keycloak-theme/generateKeycloakThemeResources.ts
index f2f5cc3e..8bfe5ba4 100644
--- a/src/bin/build-keycloak-theme/generateKeycloakThemeResources.ts
+++ b/src/bin/build-keycloak-theme/generateKeycloakThemeResources.ts
@@ -29,7 +29,7 @@ export function generateKeycloakThemeResources(
     transformCodebase({
         "destDirPath": pathJoin(themeDirPath, "resources", "build"),
         "srcDirPath": reactAppBuildDirPath,
-        "transformSourceCodeString": ({ filePath, sourceCode }) => {
+        "transformSourceCode": ({ filePath, sourceCode }) => {
 
             if (/\.css?$/i.test(filePath)) {
 
@@ -83,23 +83,24 @@ export function generateKeycloakThemeResources(
 
     {
 
-        const destDirPath = pathJoin(themeDirPath, "..", "tmp_xxKdLpdIdLd");
+        const tmpDirPath = pathJoin(themeDirPath, "..", "tmp_xxKdLpdIdLd");
 
         downloadAndUnzip({
             "url": keycloakBuiltinThemesAndThirdPartyExamplesThemsUrl,
-            destDirPath
+            "destDirPath": tmpDirPath
         });
 
-        child_process.execSync(
-            [
-                "mv", 
-                pathJoin("keycloak", "common"), 
-                pathJoin("..", "common")
-            ].join(" "),
-            { "cwd": destDirPath }
-        );
+        transformCodebase({
+            "srcDirPath": pathJoin(tmpDirPath, "keycloak", "common"),
+            "destDirPath": pathJoin(tmpDirPath, "..", "common")
+        });
 
-        child_process.execSync(`rm -r ${destDirPath}`);
+        transformCodebase({
+            "srcDirPath": pathJoin(tmpDirPath, "keycloak", "login", "resources"),
+            "destDirPath": pathJoin(themeDirPath, "resources")
+        });
+
+        child_process.execSync(`rm -r ${tmpDirPath}`);
 
     }
 
diff --git a/src/bin/download-sample-keycloak-themes.ts b/src/bin/download-sample-keycloak-themes.ts
index 6a0b7272..7f2043dd 100644
--- a/src/bin/download-sample-keycloak-themes.ts
+++ b/src/bin/download-sample-keycloak-themes.ts
@@ -2,7 +2,7 @@
 
 import { keycloakThemeBuildingDirPath } from "./build-keycloak-theme";
 import { downloadAndUnzip } from "./tools/downloadAndUnzip";
-import { join as pathJoin } from "path";
+import { join as pathJoin } from "path";
 
 export const keycloakBuiltinThemesAndThirdPartyExamplesThemsUrl =
     "https://github.com/garronej/keycloak-react-theming/releases/download/v0.0.1/other_keycloak_thems.zip";
diff --git a/src/bin/tools/downloadAndUnzip.ts b/src/bin/tools/downloadAndUnzip.ts
index 1f3e2003..c0ba581b 100644
--- a/src/bin/tools/downloadAndUnzip.ts
+++ b/src/bin/tools/downloadAndUnzip.ts
@@ -1,8 +1,10 @@
 
-import { basename as pathBasename } from "path";
-import child_process from "child_process";
+import { basename as pathBasename, join as pathJoin } from "path";
+import { execSync } from "child_process";
 import fs from "fs";
+import { transformCodebase } from "../tools/transformCodebase";
 
+/** assert url ends with .zip */
 export function downloadAndUnzip(
     params: {
         url: string;
@@ -12,11 +14,21 @@ export function downloadAndUnzip(
 
     const { url, destDirPath } = params;
 
-    fs.mkdirSync(destDirPath, { "recursive": true });
+    const tmpDirPath = pathJoin(destDirPath, "..", "tmp_xxKdOxnEdx");
 
-    [
-        `wget ${url}`,
-        ...["unzip", "rm"].map(prg => `${prg} ${pathBasename(url)}`),
-    ].forEach(cmd => child_process.execSync(cmd, { "cwd": destDirPath }));
+    execSync(`rm -rf ${tmpDirPath}`);
+
+    fs.mkdirSync(tmpDirPath, { "recursive": true });
+
+    execSync(`wget ${url}`, { "cwd": tmpDirPath })
+    execSync(`unzip ${pathBasename(url)}`, { "cwd": tmpDirPath });
+    execSync(`rm ${pathBasename(url)}`, { "cwd": tmpDirPath });
+
+    transformCodebase({
+        "srcDirPath": tmpDirPath,
+        "destDirPath": destDirPath,
+    });
+
+    execSync(`rm -r ${tmpDirPath}`);
 
 }
\ No newline at end of file
diff --git a/src/bin/tools/transformCodebase.ts b/src/bin/tools/transformCodebase.ts
index 4f4a66bf..1d5c11ab 100644
--- a/src/bin/tools/transformCodebase.ts
+++ b/src/bin/tools/transformCodebase.ts
@@ -3,34 +3,42 @@
 import * as fs from "fs";
 import * as path from "path";
 import { crawl } from "./crawl";
+import { id } from "evt/tools/typeSafety/id";
 
-/** Apply a transformation function to every file of directory */
-export function transformCodebase(
-    params: {
-        srcDirPath: string;
-        destDirPath: string;
-        transformSourceCodeString: (params: {
+type TransformSourceCode = 
+        (params: {
             sourceCode: Buffer;
             filePath: string;
         }) => {
             modifiedSourceCode: Buffer;
             newFileName?: string;
         } | undefined;
+
+/** Apply a transformation function to every file of directory */
+export function transformCodebase(
+    params: {
+        srcDirPath: string;
+        destDirPath: string;
+        transformSourceCode?: TransformSourceCode;
     }
 ) {
 
-    const { srcDirPath, destDirPath, transformSourceCodeString } = params;
+    const { 
+        srcDirPath, 
+        destDirPath, 
+        transformSourceCode = id<TransformSourceCode>(({ sourceCode }) => ({ "modifiedSourceCode": sourceCode }))
+    } = params;
 
     for (const file_relative_path of crawl(srcDirPath)) {
 
         const filePath = path.join(srcDirPath, file_relative_path);
 
-        const transformSourceCodeStringResult = transformSourceCodeString({
+        const transformSourceCodeResult = transformSourceCode({
             "sourceCode": fs.readFileSync(filePath),
             "filePath": path.join(srcDirPath, file_relative_path)
         });
 
-        if (transformSourceCodeStringResult === undefined) {
+        if (transformSourceCodeResult === undefined) {
             continue;
         }
 
@@ -44,7 +52,7 @@ export function transformCodebase(
             { "recursive": true }
         );
 
-        const { newFileName, modifiedSourceCode } = transformSourceCodeStringResult;
+        const { newFileName, modifiedSourceCode } = transformSourceCodeResult;
 
         fs.writeFileSync(
             path.join(
diff --git a/src/test/build-keycloak-theme.ts b/src/test/build-keycloak-theme.ts
index 05ad28cd..108d0a0c 100644
--- a/src/test/build-keycloak-theme.ts
+++ b/src/test/build-keycloak-theme.ts
@@ -1,24 +1,18 @@
 
 import {
-    setupSampleReactProject, 
+    setupSampleReactProject,
     sampleReactProjectDirPath
 } from "./setupSampleReactProject";
 import * as st from "scripting-tools";
 import { join as pathJoin } from "path";
-import { getProjectRoot } from "../bin/tools/getProjectRoot";
-
+import { getProjectRoot } from "../bin/tools/getProjectRoot";
 
 setupSampleReactProject();
 
-
-console.log(`Running main in ${sampleReactProjectDirPath}`);
-
-console.log(
-    st.execSync(
-        `node ${pathJoin(getProjectRoot(), "dist", "bin", "build-keycloak-theme")}`,
-        { "cwd": sampleReactProjectDirPath }
-    )
-);
+st.execSyncTrace(
+    `node ${pathJoin(getProjectRoot(), "dist", "bin", "build-keycloak-theme")}`,
+    { "cwd": sampleReactProjectDirPath }
+)
 
 
 
diff --git a/src/test/download-sample-keycloak-themes.ts b/src/test/download-sample-keycloak-themes.ts
index 492915c5..c4d23342 100644
--- a/src/test/download-sample-keycloak-themes.ts
+++ b/src/test/download-sample-keycloak-themes.ts
@@ -4,13 +4,9 @@ import * as st from "scripting-tools";
 import { join as pathJoin } from "path";
 import { getProjectRoot } from "../bin/tools/getProjectRoot";
 
-console.log(`Running main in ${sampleReactProjectDirPath}`);
-
-console.log(
-    st.execSync(
-        `node ${pathJoin(getProjectRoot(), "dist", "bin", "download-sample-keycloak-themes")}`,
-        { "cwd": sampleReactProjectDirPath }
-    )
-);
+st.execSyncTrace(
+    `node ${pathJoin(getProjectRoot(), "dist", "bin", "download-sample-keycloak-themes")}`,
+    { "cwd": sampleReactProjectDirPath }
+)
 
 
diff --git a/src/test/setupSampleReactProject.ts b/src/test/setupSampleReactProject.ts
index eba37b46..736c40ac 100644
--- a/src/test/setupSampleReactProject.ts
+++ b/src/test/setupSampleReactProject.ts
@@ -1,20 +1,14 @@
 
-import { getProjectRoot } from "../bin/tools/getProjectRoot";
-import * as st from "scripting-tools";
-import { join as pathJoin, basename as pathBasename } from "path";
+import { getProjectRoot } from "../bin/tools/getProjectRoot";
+import { join as pathJoin } from "path";
+import { downloadAndUnzip } from "../bin/tools/downloadAndUnzip";
 
 export const sampleReactProjectDirPath = pathJoin(getProjectRoot(), "sample_react_project");
 
 export function setupSampleReactProject() {
 
-    st.execSync(`rm -rf ${sampleReactProjectDirPath}`);
-    st.execSync(`mkdir ${sampleReactProjectDirPath}`);
-
-    const url = "https://github.com/garronej/keycloak-react-theming/releases/download/v0.0.1/sample_build_dir_and_package_json.zip";
-
-    [
-        `wget ${url}`,
-        ...["unzip", "rm"].map(prg => `${prg} ${pathBasename(url)}`)
-    ].forEach(cmd => st.execSync(cmd, { "cwd": sampleReactProjectDirPath }));
-
-}
\ No newline at end of file
+    downloadAndUnzip({
+        "url": "https://github.com/garronej/keycloak-react-theming/releases/download/v0.0.1/sample_build_dir_and_package_json.zip",
+        "destDirPath": sampleReactProjectDirPath
+    });
+}