2022-08-16 14:41:06 +07:00
|
|
|
import { parse as urlParse } from "url";
|
2024-01-30 06:55:26 +01:00
|
|
|
import { readParsedPackageJson } from "./parsedPackageJson";
|
2023-09-03 23:26:34 +02:00
|
|
|
import { join as pathJoin } from "path";
|
2024-01-30 06:37:49 +01:00
|
|
|
import { getAbsoluteAndInOsFormatPath } from "../../tools/getAbsoluteAndInOsFormatPath";
|
2024-05-15 05:14:01 +02:00
|
|
|
import { getResolvedViteConfig } from "./resolvedViteConfig";
|
2024-02-11 16:17:38 +01:00
|
|
|
import { getCacheDirPath } from "./getCacheDirPath";
|
|
|
|
import { getReactAppRootDirPath } from "./getReactAppRootDirPath";
|
2024-02-11 18:28:58 +01:00
|
|
|
import { getNpmWorkspaceRootDirPath } from "./getNpmWorkspaceRootDirPath";
|
2024-05-15 05:14:01 +02:00
|
|
|
import type { CliCommandOptions } from "../../main";
|
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 = {
|
2024-01-30 05:54:36 +01:00
|
|
|
bundler: "vite" | "webpack";
|
2023-08-21 05:54:17 +02:00
|
|
|
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:02:51 +02:00
|
|
|
loginThemeResourcesFromKeycloakVersion: string;
|
2023-09-03 23:26:34 +02:00
|
|
|
reactAppRootDirPath: string;
|
2024-05-16 06:21:54 +02:00
|
|
|
// TODO: Remove from vite type
|
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;
|
2024-01-30 00:06:17 +01:00
|
|
|
assetsDirPath: string;
|
2024-02-11 18:28:58 +01:00
|
|
|
npmWorkspaceRootDirPath: string;
|
2023-08-21 05:54:17 +02:00
|
|
|
};
|
2022-08-16 14:41:06 +07:00
|
|
|
|
2024-05-15 05:14:01 +02:00
|
|
|
export function readBuildOptions(params: { cliCommandOptions: CliCommandOptions }): BuildOptions {
|
|
|
|
const { cliCommandOptions } = params;
|
2024-02-08 00:56:33 +01:00
|
|
|
|
2024-05-15 05:14:01 +02:00
|
|
|
const { reactAppRootDirPath } = getReactAppRootDirPath({ cliCommandOptions });
|
2024-02-08 00:56:33 +01:00
|
|
|
|
2024-02-11 16:17:38 +01:00
|
|
|
const { cacheDirPath } = getCacheDirPath({ reactAppRootDirPath });
|
2024-02-08 00:56:33 +01:00
|
|
|
|
2024-05-15 05:14:01 +02:00
|
|
|
const { resolvedViteConfig } = getResolvedViteConfig({
|
|
|
|
cacheDirPath,
|
|
|
|
reactAppRootDirPath
|
|
|
|
});
|
2023-04-18 03:10:29 +02:00
|
|
|
|
2024-02-23 19:15:59 +01:00
|
|
|
const { keycloakify: userProvidedBuildOptionsFromPackageJson, ...parsedPackageJson } = readParsedPackageJson({ reactAppRootDirPath });
|
|
|
|
|
|
|
|
const userProvidedBuildOptions = {
|
|
|
|
...userProvidedBuildOptionsFromPackageJson,
|
|
|
|
...resolvedViteConfig?.userProvidedBuildOptions
|
|
|
|
};
|
2022-08-16 14:41:06 +07:00
|
|
|
|
2023-09-04 01:19:21 +02:00
|
|
|
const themeNames = (() => {
|
2024-02-23 19:15:59 +01:00
|
|
|
if (userProvidedBuildOptions.themeName === undefined) {
|
2023-09-04 01:19:21 +02:00
|
|
|
return [
|
2024-01-30 05:54:36 +01:00
|
|
|
parsedPackageJson.name
|
2023-09-04 01:19:21 +02:00
|
|
|
.replace(/^@(.*)/, "$1")
|
|
|
|
.split("/")
|
|
|
|
.join("-")
|
|
|
|
];
|
|
|
|
}
|
2022-08-16 14:41:06 +07:00
|
|
|
|
2024-02-23 19:15:59 +01:00
|
|
|
if (typeof userProvidedBuildOptions.themeName === "string") {
|
|
|
|
return [userProvidedBuildOptions.themeName];
|
2023-09-04 01:19:21 +02:00
|
|
|
}
|
|
|
|
|
2024-02-23 19:15:59 +01:00
|
|
|
return userProvidedBuildOptions.themeName;
|
2023-09-04 01:19:21 +02:00
|
|
|
})();
|
2022-08-16 14:41:06 +07:00
|
|
|
|
2024-01-30 06:55:26 +01:00
|
|
|
const reactAppBuildDirPath = (() => {
|
|
|
|
webpack: {
|
|
|
|
if (resolvedViteConfig !== undefined) {
|
|
|
|
break webpack;
|
|
|
|
}
|
|
|
|
|
2024-02-23 19:15:59 +01:00
|
|
|
if (userProvidedBuildOptions.reactAppBuildDirPath !== undefined) {
|
2024-01-30 06:55:26 +01:00
|
|
|
return getAbsoluteAndInOsFormatPath({
|
2024-02-23 19:15:59 +01:00
|
|
|
"pathIsh": userProvidedBuildOptions.reactAppBuildDirPath,
|
2024-01-30 06:55:26 +01:00
|
|
|
"cwd": reactAppRootDirPath
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return pathJoin(reactAppRootDirPath, "build");
|
|
|
|
}
|
|
|
|
|
|
|
|
return pathJoin(reactAppRootDirPath, resolvedViteConfig.buildDir);
|
|
|
|
})();
|
2024-01-30 05:54:36 +01:00
|
|
|
|
2024-02-11 18:28:58 +01:00
|
|
|
const { npmWorkspaceRootDirPath } = getNpmWorkspaceRootDirPath({ reactAppRootDirPath });
|
|
|
|
|
2024-01-30 05:54:36 +01:00
|
|
|
return {
|
2024-01-30 06:04:05 +01:00
|
|
|
"bundler": resolvedViteConfig !== undefined ? "vite" : "webpack",
|
2024-05-15 05:14:01 +02:00
|
|
|
"isSilent": cliCommandOptions.isSilent,
|
2024-01-30 05:54:36 +01:00
|
|
|
"themeVersion": process.env.KEYCLOAKIFY_THEME_VERSION ?? parsedPackageJson.version ?? "0.0.0",
|
2023-09-04 01:19:21 +02:00
|
|
|
themeNames,
|
2024-02-23 19:15:59 +01:00
|
|
|
"extraThemeProperties": userProvidedBuildOptions.extraThemeProperties,
|
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 ??
|
2024-02-23 19:15:59 +01:00
|
|
|
userProvidedBuildOptions.groupId ??
|
2024-01-30 05:54:36 +01:00
|
|
|
(parsedPackageJson.homepage === undefined
|
2023-08-21 05:54:17 +02:00
|
|
|
? fallbackGroupId
|
2024-01-30 05:54:36 +01:00
|
|
|
: urlParse(parsedPackageJson.homepage)
|
2023-08-21 05:54:17 +02:00
|
|
|
.host?.replace(/:[0-9]+$/, "")
|
|
|
|
?.split(".")
|
|
|
|
.reverse()
|
|
|
|
.join(".") ?? fallbackGroupId) + ".keycloak"
|
|
|
|
);
|
|
|
|
})(),
|
2024-02-23 19:15:59 +01:00
|
|
|
"artifactId": process.env.KEYCLOAKIFY_ARTIFACT_ID ?? userProvidedBuildOptions.artifactId ?? `${themeNames[0]}-keycloak-theme`,
|
2024-05-11 23:11:52 +02:00
|
|
|
"loginThemeResourcesFromKeycloakVersion": userProvidedBuildOptions.loginThemeResourcesFromKeycloakVersion ?? "24.0.4",
|
2024-01-30 05:54:36 +01:00
|
|
|
reactAppRootDirPath,
|
2024-01-30 06:55:26 +01:00
|
|
|
reactAppBuildDirPath,
|
2024-02-11 16:17:38 +01:00
|
|
|
"keycloakifyBuildDirPath": (() => {
|
2024-02-23 19:15:59 +01:00
|
|
|
if (userProvidedBuildOptions.keycloakifyBuildDirPath !== undefined) {
|
2024-02-11 16:17:38 +01:00
|
|
|
return getAbsoluteAndInOsFormatPath({
|
2024-02-23 19:15:59 +01:00
|
|
|
"pathIsh": userProvidedBuildOptions.keycloakifyBuildDirPath,
|
2024-02-11 16:17:38 +01:00
|
|
|
"cwd": reactAppRootDirPath
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-02-24 05:38:42 +01:00
|
|
|
return pathJoin(
|
|
|
|
reactAppRootDirPath,
|
|
|
|
resolvedViteConfig?.buildDir === undefined ? "build_keycloak" : `${resolvedViteConfig.buildDir}_keycloak`
|
|
|
|
);
|
2024-02-11 16:17:38 +01:00
|
|
|
})(),
|
2024-01-30 05:54:36 +01:00
|
|
|
"publicDirPath": (() => {
|
2024-01-30 06:55:26 +01:00
|
|
|
webpack: {
|
|
|
|
if (resolvedViteConfig !== undefined) {
|
|
|
|
break webpack;
|
|
|
|
}
|
2024-01-27 18:49:29 +01:00
|
|
|
|
2024-01-30 06:55:26 +01:00
|
|
|
if (process.env.PUBLIC_DIR_PATH !== undefined) {
|
|
|
|
return getAbsoluteAndInOsFormatPath({
|
|
|
|
"pathIsh": process.env.PUBLIC_DIR_PATH,
|
|
|
|
"cwd": reactAppRootDirPath
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return pathJoin(reactAppRootDirPath, "public");
|
2023-08-21 05:54:17 +02:00
|
|
|
}
|
2022-08-16 14:41:06 +07:00
|
|
|
|
2024-01-30 06:55:26 +01:00
|
|
|
return pathJoin(reactAppRootDirPath, resolvedViteConfig.publicDir);
|
2023-08-21 05:54:17 +02:00
|
|
|
})(),
|
2024-02-11 16:17:38 +01:00
|
|
|
cacheDirPath,
|
2023-08-21 05:54:17 +02:00
|
|
|
"urlPathname": (() => {
|
2024-01-30 06:55:26 +01:00
|
|
|
webpack: {
|
|
|
|
if (resolvedViteConfig !== undefined) {
|
|
|
|
break webpack;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { homepage } = parsedPackageJson;
|
|
|
|
|
|
|
|
let url: URL | undefined = undefined;
|
2022-08-16 14:41:06 +07:00
|
|
|
|
2024-01-30 06:55:26 +01:00
|
|
|
if (homepage !== undefined) {
|
|
|
|
url = new URL(homepage);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (url === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2022-08-16 14:41:06 +07:00
|
|
|
|
2024-01-30 06:55:26 +01:00
|
|
|
const out = url.pathname.replace(/([^/])$/, "$1/");
|
|
|
|
return out === "/" ? undefined : out;
|
2023-08-21 05:54:17 +02:00
|
|
|
}
|
2022-08-16 14:41:06 +07:00
|
|
|
|
2024-01-30 06:55:26 +01:00
|
|
|
return resolvedViteConfig.urlPathname;
|
|
|
|
})(),
|
|
|
|
"assetsDirPath": (() => {
|
|
|
|
webpack: {
|
|
|
|
if (resolvedViteConfig !== undefined) {
|
|
|
|
break webpack;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pathJoin(reactAppBuildDirPath, "static");
|
2023-08-21 05:54:17 +02:00
|
|
|
}
|
2022-08-16 14:41:06 +07:00
|
|
|
|
2024-01-30 06:55:26 +01:00
|
|
|
return pathJoin(reactAppBuildDirPath, resolvedViteConfig.assetsDir);
|
2024-02-04 10:25:48 +01:00
|
|
|
})(),
|
2024-02-11 18:28:58 +01:00
|
|
|
npmWorkspaceRootDirPath
|
2023-08-21 05:54:17 +02:00
|
|
|
};
|
2022-08-16 14:41:06 +07:00
|
|
|
}
|