2022-08-16 14:41:06 +07:00
|
|
|
import { parse as urlParse } from "url";
|
2023-09-03 21:10:20 +02:00
|
|
|
import { getParsedPackageJson } from "./parsedPackageJson";
|
2023-09-03 23:26:34 +02:00
|
|
|
import { join as pathJoin } from "path";
|
2023-04-19 05:04:11 +02:00
|
|
|
import parseArgv from "minimist";
|
2023-09-03 23:26:34 +02:00
|
|
|
import { getAbsoluteAndInOsFormatPath } from "../tools/getAbsoluteAndInOsFormatPath";
|
2024-01-27 18:49:29 +01:00
|
|
|
import * as fs from "fs";
|
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;
|
2023-09-04 01:19:21 +02:00
|
|
|
themeNames: string[];
|
2023-08-21 05:54:17 +02:00
|
|
|
extraThemeProperties: string[] | undefined;
|
|
|
|
groupId: string;
|
|
|
|
artifactId: string;
|
2023-09-03 21:10:20 +02:00
|
|
|
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
|
|
|
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-04-18 03:10:29 +02:00
|
|
|
|
2023-08-21 05:54:17 +02:00
|
|
|
const { isSilentCliParamProvided } = (() => {
|
2023-04-19 05:04:11 +02:00
|
|
|
const argv = parseArgv(processArgv);
|
|
|
|
|
|
|
|
return {
|
2023-08-21 05:54:17 +02:00
|
|
|
"isSilentCliParamProvided": typeof argv["silent"] === "boolean" ? argv["silent"] : false
|
2023-04-19 05:04:11 +02:00
|
|
|
};
|
|
|
|
})();
|
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
|
|
|
|
2023-09-04 01:19:21 +02: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
|
|
|
|
2023-09-04 01:19:21 +02: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,
|
2023-09-04 01:19:21 +02:00
|
|
|
themeNames,
|
2023-09-03 21:10:20 +02:00
|
|
|
"doCreateJar": doCreateJar ?? true,
|
2023-09-04 01:19:21 +02:00
|
|
|
"artifactId": process.env.KEYCLOAKIFY_ARTIFACT_ID ?? artifactId ?? `${themeNames[0]}-keycloak-theme`,
|
2023-08-21 05:54:17 +02:00
|
|
|
"groupId": (() => {
|
2023-09-04 01:19:21 +02:00
|
|
|
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
|
|
|
|
2024-01-27 18:49:29 +01:00
|
|
|
for (const name of ["build", "dist"]) {
|
|
|
|
const out = pathJoin(reactAppRootDirPath, name);
|
|
|
|
|
|
|
|
if (!fs.existsSync(out)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error("Please use the reactAppBuildDirPath option to specify the build directory of your react app");
|
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;
|
2024-01-26 03:39:48 +01:00
|
|
|
})()
|
2023-08-21 05:54:17 +02:00
|
|
|
};
|
2022-08-16 14:41:06 +07:00
|
|
|
}
|