Complete build option

This commit is contained in:
Joseph Garrone 2024-01-30 06:55:26 +01:00
parent 5a57bb59e5
commit 7c7ce159fe
6 changed files with 111 additions and 82 deletions

View File

@ -1,9 +1,10 @@
import { parse as urlParse } from "url"; import { parse as urlParse } from "url";
import { getParsedPackageJson } from "./parsedPackageJson"; import { readParsedPackageJson } from "./parsedPackageJson";
import { join as pathJoin } from "path"; import { join as pathJoin } from "path";
import parseArgv from "minimist"; import parseArgv from "minimist";
import { getAbsoluteAndInOsFormatPath } from "../../tools/getAbsoluteAndInOsFormatPath"; import { getAbsoluteAndInOsFormatPath } from "../../tools/getAbsoluteAndInOsFormatPath";
import { readResolvedViteConfig, getKeycloakifyBuildDirPath } from "./resolvedViteConfig"; import { readResolvedViteConfig } from "./resolvedViteConfig";
import { getKeycloakifyBuildDirPath } from "./getKeycloakifyBuildDirPath";
/** Consolidated build option gathered form CLI arguments and config in package.json */ /** Consolidated build option gathered form CLI arguments and config in package.json */
export type BuildOptions = { export type BuildOptions = {
@ -31,7 +32,7 @@ export type BuildOptions = {
export function readBuildOptions(params: { reactAppRootDirPath: string; processArgv: string[] }): BuildOptions { export function readBuildOptions(params: { reactAppRootDirPath: string; processArgv: string[] }): BuildOptions {
const { reactAppRootDirPath, processArgv } = params; const { reactAppRootDirPath, processArgv } = params;
const parsedPackageJson = getParsedPackageJson({ reactAppRootDirPath }); const parsedPackageJson = readParsedPackageJson({ reactAppRootDirPath });
const { resolvedViteConfig } = const { resolvedViteConfig } =
readResolvedViteConfig({ readResolvedViteConfig({
@ -61,7 +62,25 @@ export function readBuildOptions(params: { reactAppRootDirPath: string; processA
reactAppRootDirPath, reactAppRootDirPath,
"bundler": resolvedViteConfig !== undefined ? "vite" : "webpack" "bundler": resolvedViteConfig !== undefined ? "vite" : "webpack"
}); });
//const keycloakifyBuildDirPath = keycloakifyBuildDirPath_vite ?? pathJoin(reactAppRootDirPath, "build_keycloak");
const reactAppBuildDirPath = (() => {
webpack: {
if (resolvedViteConfig !== undefined) {
break webpack;
}
if (parsedPackageJson.keycloakify?.reactAppBuildDirPath !== undefined) {
return getAbsoluteAndInOsFormatPath({
"pathIsh": parsedPackageJson.keycloakify?.reactAppBuildDirPath,
"cwd": reactAppRootDirPath
});
}
return pathJoin(reactAppRootDirPath, "build");
}
return pathJoin(reactAppRootDirPath, resolvedViteConfig.buildDir);
})();
return { return {
"bundler": resolvedViteConfig !== undefined ? "vite" : "webpack", "bundler": resolvedViteConfig !== undefined ? "vite" : "webpack",
@ -92,43 +111,31 @@ export function readBuildOptions(params: { reactAppRootDirPath: string; processA
"doCreateJar": parsedPackageJson.keycloakify?.doCreateJar ?? true, "doCreateJar": parsedPackageJson.keycloakify?.doCreateJar ?? true,
"loginThemeResourcesFromKeycloakVersion": parsedPackageJson.keycloakify?.loginThemeResourcesFromKeycloakVersion ?? "11.0.3", "loginThemeResourcesFromKeycloakVersion": parsedPackageJson.keycloakify?.loginThemeResourcesFromKeycloakVersion ?? "11.0.3",
reactAppRootDirPath, reactAppRootDirPath,
"reactAppBuildDirPath": (() => { reactAppBuildDirPath,
if (resolvedViteConfig !== undefined) {
return pathJoin(reactAppRootDirPath, resolvedViteConfig.buildDir);
}
if (parsedPackageJson.keycloakify?.reactAppBuildDirPath !== undefined) {
return getAbsoluteAndInOsFormatPath({
"pathIsh": parsedPackageJson.keycloakify?.reactAppBuildDirPath,
"cwd": reactAppRootDirPath
});
}
return pathJoin(reactAppRootDirPath, "build");
})(),
"publicDirPath": (() => {
if (resolvedViteConfig !== undefined) {
return resolvedViteConfig.publicDirPath;
}
if (process.env.PUBLIC_DIR_PATH !== undefined) {
return getAbsoluteAndInOsFormatPath({
"pathIsh": process.env.PUBLIC_DIR_PATH,
"cwd": reactAppRootDirPath
});
}
return pathJoin(reactAppRootDirPath, "public");
})(),
keycloakifyBuildDirPath, keycloakifyBuildDirPath,
"publicDirPath": (() => {
webpack: {
if (resolvedViteConfig !== undefined) {
break webpack;
}
if (process.env.PUBLIC_DIR_PATH !== undefined) {
return getAbsoluteAndInOsFormatPath({
"pathIsh": process.env.PUBLIC_DIR_PATH,
"cwd": reactAppRootDirPath
});
}
return pathJoin(reactAppRootDirPath, "public");
}
return pathJoin(reactAppRootDirPath, resolvedViteConfig.publicDir);
})(),
"cacheDirPath": pathJoin( "cacheDirPath": pathJoin(
(() => { (() => {
let { XDG_CACHE_HOME } = process.env; if (process.env.XDG_CACHE_HOME !== undefined) {
if (XDG_CACHE_HOME !== undefined) {
return getAbsoluteAndInOsFormatPath({ return getAbsoluteAndInOsFormatPath({
"pathIsh": XDG_CACHE_HOME, "pathIsh": process.env.XDG_CACHE_HOME,
"cwd": reactAppRootDirPath "cwd": reactAppRootDirPath
}); });
} }
@ -138,20 +145,39 @@ export function readBuildOptions(params: { reactAppRootDirPath: string; processA
"keycloakify" "keycloakify"
), ),
"urlPathname": (() => { "urlPathname": (() => {
const { homepage } = parsedPackageJson; webpack: {
if (resolvedViteConfig !== undefined) {
break webpack;
}
let url: URL | undefined = undefined; const { homepage } = parsedPackageJson;
if (homepage !== undefined) { let url: URL | undefined = undefined;
url = new URL(homepage);
if (homepage !== undefined) {
url = new URL(homepage);
}
if (url === undefined) {
return undefined;
}
const out = url.pathname.replace(/([^/])$/, "$1/");
return out === "/" ? undefined : out;
} }
if (url === undefined) { return resolvedViteConfig.urlPathname;
return undefined; })(),
"assetsDirPath": (() => {
webpack: {
if (resolvedViteConfig !== undefined) {
break webpack;
}
return pathJoin(reactAppBuildDirPath, "static");
} }
const out = url.pathname.replace(/([^/])$/, "$1/"); return pathJoin(reactAppBuildDirPath, resolvedViteConfig.assetsDir);
return out === "/" ? undefined : out;
})() })()
}; };
} }

View File

@ -0,0 +1,33 @@
import { getAbsoluteAndInOsFormatPath } from "../../tools/getAbsoluteAndInOsFormatPath";
import { join as pathJoin } from "path";
export function getKeycloakifyBuildDirPath(params: {
reactAppRootDirPath: string;
parsedPackageJson_keycloakify_keycloakifyBuildDirPath: string | undefined;
bundler: "vite" | "webpack";
}) {
const { reactAppRootDirPath, parsedPackageJson_keycloakify_keycloakifyBuildDirPath, bundler } = params;
const keycloakifyBuildDirPath = (() => {
if (parsedPackageJson_keycloakify_keycloakifyBuildDirPath !== undefined) {
getAbsoluteAndInOsFormatPath({
"pathIsh": parsedPackageJson_keycloakify_keycloakifyBuildDirPath,
"cwd": reactAppRootDirPath
});
}
return pathJoin(
reactAppRootDirPath,
`${(() => {
switch (bundler) {
case "vite":
return "dist";
case "webpack":
return "build";
}
})()}_keycloak`
);
})();
return { keycloakifyBuildDirPath };
}

View File

@ -1 +1 @@
export * from "./buildOptions2"; export * from "./buildOptions";

View File

@ -41,7 +41,7 @@ const zParsedPackageJson = z.object({
assert<Equals<ReturnType<(typeof zParsedPackageJson)["parse"]>, ParsedPackageJson>>(); assert<Equals<ReturnType<(typeof zParsedPackageJson)["parse"]>, ParsedPackageJson>>();
let parsedPackageJson: undefined | ParsedPackageJson; let parsedPackageJson: undefined | ParsedPackageJson;
export function getParsedPackageJson(params: { reactAppRootDirPath: string }) { export function readParsedPackageJson(params: { reactAppRootDirPath: string }) {
const { reactAppRootDirPath } = params; const { reactAppRootDirPath } = params;
if (parsedPackageJson) { if (parsedPackageJson) {
return parsedPackageJson; return parsedPackageJson;

View File

@ -5,7 +5,7 @@ import { z } from "zod";
import { join as pathJoin } from "path"; import { join as pathJoin } from "path";
import { resolvedViteConfigJsonBasename } from "../../constants"; import { resolvedViteConfigJsonBasename } from "../../constants";
import type { OptionalIfCanBeUndefined } from "../../tools/OptionalIfCanBeUndefined"; import type { OptionalIfCanBeUndefined } from "../../tools/OptionalIfCanBeUndefined";
import { getAbsoluteAndInOsFormatPath } from "../../tools/getAbsoluteAndInOsFormatPath"; import { getKeycloakifyBuildDirPath } from "./getKeycloakifyBuildDirPath";
export type ResolvedViteConfig = { export type ResolvedViteConfig = {
buildDir: string; buildDir: string;
@ -83,34 +83,3 @@ export function readResolvedViteConfig(params: {
return { resolvedViteConfig }; return { resolvedViteConfig };
} }
export function getKeycloakifyBuildDirPath(params: {
reactAppRootDirPath: string;
parsedPackageJson_keycloakify_keycloakifyBuildDirPath: string | undefined;
bundler: "vite" | "webpack";
}) {
const { reactAppRootDirPath, parsedPackageJson_keycloakify_keycloakifyBuildDirPath, bundler } = params;
const keycloakifyBuildDirPath = (() => {
if (parsedPackageJson_keycloakify_keycloakifyBuildDirPath !== undefined) {
getAbsoluteAndInOsFormatPath({
"pathIsh": parsedPackageJson_keycloakify_keycloakifyBuildDirPath,
"cwd": reactAppRootDirPath
});
}
return pathJoin(
reactAppRootDirPath,
`${(() => {
switch (bundler) {
case "vite":
return "dist";
case "webpack":
return "build";
}
})()}_keycloak`
);
})();
return { keycloakifyBuildDirPath };
}

View File

@ -1,10 +1,11 @@
import { join as pathJoin, relative as pathRelative, sep as pathSep } from "path"; import { join as pathJoin, relative as pathRelative, sep as pathSep } from "path";
import { getParsedPackageJson } from "../bin/keycloakify/parsedPackageJson"; import { readParsedPackageJson } from "../bin/keycloakify/buildOptions/parsedPackageJson";
import type { Plugin } from "vite"; import type { Plugin } from "vite";
import { assert } from "tsafe/assert"; import { assert } from "tsafe/assert";
import * as fs from "fs"; import * as fs from "fs";
import { resolvedViteConfigJsonBasename, nameOfTheGlobal, basenameOfTheKeycloakifyResourcesDir } from "../bin/constants"; import { resolvedViteConfigJsonBasename, nameOfTheGlobal, basenameOfTheKeycloakifyResourcesDir } from "../bin/constants";
import { type ResolvedViteConfig, getKeycloakifyBuildDirPath } from "../bin/keycloakify/resolvedViteConfig"; import type { ResolvedViteConfig } from "../bin/keycloakify/buildOptions/resolvedViteConfig";
import { getKeycloakifyBuildDirPath } from "../bin/keycloakify/buildOptions/getKeycloakifyBuildDirPath";
import { replaceAll } from "../bin/tools/String.prototype.replaceAll"; import { replaceAll } from "../bin/tools/String.prototype.replaceAll";
import { id } from "tsafe/id"; import { id } from "tsafe/id";
@ -35,7 +36,7 @@ export function keycloakify(): Plugin {
})(); })();
const { keycloakifyBuildDirPath } = getKeycloakifyBuildDirPath({ const { keycloakifyBuildDirPath } = getKeycloakifyBuildDirPath({
"parsedPackageJson_keycloakify_keycloakifyBuildDirPath": getParsedPackageJson({ reactAppRootDirPath }).keycloakify "parsedPackageJson_keycloakify_keycloakifyBuildDirPath": readParsedPackageJson({ reactAppRootDirPath }).keycloakify
?.keycloakifyBuildDirPath, ?.keycloakifyBuildDirPath,
reactAppRootDirPath, reactAppRootDirPath,
"bundler": "vite" "bundler": "vite"