keycloak_theme/src/bin/keycloakify/BuildOptions.ts

156 lines
5.5 KiB
TypeScript
Raw Normal View History

2022-08-16 14:41:06 +07:00
import { parse as urlParse } from "url";
import { getParsedPackageJson } from "./parsedPackageJson";
2023-09-03 23:26:34 +02:00
import { join as pathJoin } from "path";
import parseArgv from "minimist";
2023-09-03 23:26:34 +02:00
import { getAbsoluteAndInOsFormatPath } from "../tools/getAbsoluteAndInOsFormatPath";
2022-08-16 14:41:06 +07:00
/** Consolidated build option gathered form CLI arguments and config in package.json */
2023-08-21 05:54:17 +02:00
export type BuildOptions = {
isSilent: boolean;
themeVersion: string;
themeNames: string[];
2023-08-21 05:54:17 +02:00
extraThemeProperties: string[] | undefined;
groupId: string;
artifactId: string;
doCreateJar: boolean;
2023-09-03 21:02:51 +02:00
loginThemeResourcesFromKeycloakVersion: string;
2023-09-03 23:26:34 +02:00
reactAppRootDirPath: string;
2023-08-21 05:54:17 +02:00
/** Directory of your built react project. Defaults to {cwd}/build */
reactAppBuildDirPath: string;
/** Directory that keycloakify outputs to. Defaults to {cwd}/build_keycloak */
keycloakifyBuildDirPath: string;
2023-09-03 23:26:34 +02:00
publicDirPath: string;
cacheDirPath: string;
2023-08-21 05:54:17 +02:00
/** If your app is hosted under a subpath, it's the case in CRA if you have "homepage": "https://example.com/my-app" in your package.json
* In this case the urlPathname will be "/my-app/" */
urlPathname: string | undefined;
};
2022-08-16 14:41:06 +07:00
2023-09-03 23:26:34 +02:00
export function readBuildOptions(params: { reactAppRootDirPath: string; processArgv: string[] }): BuildOptions {
const { reactAppRootDirPath, processArgv } = params;
2023-08-21 05:54:17 +02:00
const { isSilentCliParamProvided } = (() => {
const argv = parseArgv(processArgv);
return {
2023-08-21 05:54:17 +02:00
"isSilentCliParamProvided": typeof argv["silent"] === "boolean" ? argv["silent"] : false
};
})();
2022-08-16 14:41:06 +07:00
2023-09-03 23:26:34 +02:00
const parsedPackageJson = getParsedPackageJson({ reactAppRootDirPath });
2022-08-16 14:41:06 +07:00
2023-08-21 05:54:17 +02:00
const { name, keycloakify = {}, version, homepage } = parsedPackageJson;
2022-08-16 14:41:06 +07:00
const { extraThemeProperties, groupId, artifactId, doCreateJar, loginThemeResourcesFromKeycloakVersion } = keycloakify ?? {};
const themeNames = (() => {
if (keycloakify.themeName === undefined) {
return [
name
.replace(/^@(.*)/, "$1")
.split("/")
.join("-")
];
}
2022-08-16 14:41:06 +07:00
if (typeof keycloakify.themeName === "string") {
return [keycloakify.themeName];
}
return keycloakify.themeName;
})();
2022-08-16 14:41:06 +07:00
2023-08-21 05:54:17 +02:00
return {
2023-09-03 23:26:34 +02:00
reactAppRootDirPath,
themeNames,
"doCreateJar": doCreateJar ?? true,
"artifactId": process.env.KEYCLOAKIFY_ARTIFACT_ID ?? artifactId ?? `${themeNames[0]}-keycloak-theme`,
2023-08-21 05:54:17 +02:00
"groupId": (() => {
const fallbackGroupId = `${themeNames[0]}.keycloak`;
2023-08-21 05:54:17 +02:00
return (
process.env.KEYCLOAKIFY_GROUP_ID ??
groupId ??
(!homepage
? fallbackGroupId
: urlParse(homepage)
.host?.replace(/:[0-9]+$/, "")
?.split(".")
.reverse()
.join(".") ?? fallbackGroupId) + ".keycloak"
);
})(),
"themeVersion": process.env.KEYCLOAKIFY_THEME_VERSION ?? process.env.KEYCLOAKIFY_VERSION ?? version ?? "0.0.0",
extraThemeProperties,
"isSilent": isSilentCliParamProvided,
2023-09-03 21:02:51 +02:00
"loginThemeResourcesFromKeycloakVersion": loginThemeResourcesFromKeycloakVersion ?? "11.0.3",
2023-09-03 23:26:34 +02:00
"publicDirPath": (() => {
let { PUBLIC_DIR_PATH } = process.env;
if (PUBLIC_DIR_PATH !== undefined) {
return getAbsoluteAndInOsFormatPath({
"pathIsh": PUBLIC_DIR_PATH,
"cwd": reactAppRootDirPath
});
2023-04-02 03:10:16 +02:00
}
2023-09-03 23:26:34 +02:00
return pathJoin(reactAppRootDirPath, "public");
})(),
"reactAppBuildDirPath": (() => {
const { reactAppBuildDirPath } = parsedPackageJson.keycloakify ?? {};
2023-04-02 03:10:16 +02:00
2023-09-03 23:26:34 +02:00
if (reactAppBuildDirPath !== undefined) {
return getAbsoluteAndInOsFormatPath({
"pathIsh": reactAppBuildDirPath,
"cwd": reactAppRootDirPath
});
2023-08-21 05:54:17 +02:00
}
2022-08-16 14:41:06 +07:00
2023-09-03 23:26:34 +02:00
return pathJoin(reactAppRootDirPath, "build");
2023-08-21 05:54:17 +02:00
})(),
"keycloakifyBuildDirPath": (() => {
2023-09-03 23:26:34 +02:00
const { keycloakifyBuildDirPath } = parsedPackageJson.keycloakify ?? {};
2022-08-16 14:41:06 +07:00
2023-09-03 23:26:34 +02:00
if (keycloakifyBuildDirPath !== undefined) {
return getAbsoluteAndInOsFormatPath({
"pathIsh": keycloakifyBuildDirPath,
"cwd": reactAppRootDirPath
});
2023-08-21 05:54:17 +02:00
}
2022-08-16 14:41:06 +07:00
2023-09-03 23:26:34 +02:00
return pathJoin(reactAppRootDirPath, "build_keycloak");
2023-08-21 05:54:17 +02:00
})(),
2023-09-03 23:26:34 +02:00
"cacheDirPath": pathJoin(
(() => {
let { XDG_CACHE_HOME } = process.env;
if (XDG_CACHE_HOME !== undefined) {
return getAbsoluteAndInOsFormatPath({
"pathIsh": XDG_CACHE_HOME,
"cwd": reactAppRootDirPath
});
}
return pathJoin(reactAppRootDirPath, "node_modules", ".cache");
})(),
"keycloakify"
),
2023-08-21 05:54:17 +02:00
"urlPathname": (() => {
const { homepage } = parsedPackageJson;
2022-08-16 14:41:06 +07:00
2023-08-21 05:54:17 +02:00
let url: URL | undefined = undefined;
2022-08-16 14:41:06 +07:00
2023-08-21 05:54:17 +02:00
if (homepage !== undefined) {
url = new URL(homepage);
}
2022-08-16 14:41:06 +07:00
2023-08-21 05:54:17 +02:00
if (url === undefined) {
return undefined;
}
2022-08-16 14:41:06 +07:00
2023-08-21 05:54:17 +02:00
const out = url.pathname.replace(/([^/])$/, "$1/");
return out === "/" ? undefined : out;
})()
};
2022-08-16 14:41:06 +07:00
}