197 lines
7.1 KiB
TypeScript
Raw Normal View History

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";
import parseArgv from "minimist";
2024-01-30 06:37:49 +01:00
import { getAbsoluteAndInOsFormatPath } from "../../tools/getAbsoluteAndInOsFormatPath";
2024-01-30 06:55:26 +01:00
import { readResolvedViteConfig } from "./resolvedViteConfig";
import { getKeycloakifyBuildDirPath } from "./getKeycloakifyBuildDirPath";
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;
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
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;
doBuildRetrocompatAccountTheme: boolean;
2023-08-21 05:54:17 +02:00
};
2022-08-16 14:41:06 +07:00
export function readBuildOptions(params: { processArgv: string[] }): BuildOptions {
const { processArgv } = params;
const argv = parseArgv(processArgv);
const reactAppRootDirPath = (() => {
const arg = argv["project"] ?? argv["p"];
if (typeof arg !== "string") {
return process.cwd();
}
return getAbsoluteAndInOsFormatPath({
"pathIsh": arg,
"cwd": process.cwd()
});
})();
2024-01-30 06:55:26 +01:00
const parsedPackageJson = readParsedPackageJson({ reactAppRootDirPath });
2022-08-16 14:41:06 +07:00
2024-01-30 06:04:05 +01:00
const { resolvedViteConfig } =
readResolvedViteConfig({
2024-01-30 05:54:36 +01:00
"parsedPackageJson_keycloakify_keycloakifyBuildDirPath": parsedPackageJson.keycloakify?.keycloakifyBuildDirPath,
reactAppRootDirPath
}) ?? {};
const themeNames = (() => {
2024-01-30 05:54:36 +01:00
if (parsedPackageJson.keycloakify?.themeName === undefined) {
return [
2024-01-30 05:54:36 +01:00
parsedPackageJson.name
.replace(/^@(.*)/, "$1")
.split("/")
.join("-")
];
}
2022-08-16 14:41:06 +07:00
2024-01-30 05:54:36 +01:00
if (typeof parsedPackageJson.keycloakify.themeName === "string") {
return [parsedPackageJson.keycloakify.themeName];
}
2024-01-30 05:54:36 +01:00
return parsedPackageJson.keycloakify.themeName;
})();
2022-08-16 14:41:06 +07:00
2024-01-30 05:54:36 +01:00
const { keycloakifyBuildDirPath } = getKeycloakifyBuildDirPath({
"parsedPackageJson_keycloakify_keycloakifyBuildDirPath": parsedPackageJson.keycloakify?.keycloakifyBuildDirPath,
2023-09-03 23:26:34 +02:00
reactAppRootDirPath,
2024-01-30 06:04:05 +01:00
"bundler": resolvedViteConfig !== undefined ? "vite" : "webpack"
2024-01-30 05:54:36 +01:00
});
2024-01-30 06:55:26 +01:00
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);
})();
2024-01-30 05:54:36 +01:00
return {
2024-01-30 06:04:05 +01:00
"bundler": resolvedViteConfig !== undefined ? "vite" : "webpack",
"isSilent": typeof argv["silent"] === "boolean" ? argv["silent"] : false,
2024-01-30 05:54:36 +01:00
"themeVersion": process.env.KEYCLOAKIFY_THEME_VERSION ?? parsedPackageJson.version ?? "0.0.0",
themeNames,
2024-01-30 05:54:36 +01:00
"extraThemeProperties": parsedPackageJson.keycloakify?.extraThemeProperties,
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 ??
2024-01-30 05:54:36 +01:00
parsedPackageJson.keycloakify?.groupId ??
(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-01-30 05:54:36 +01:00
"artifactId": process.env.KEYCLOAKIFY_ARTIFACT_ID ?? parsedPackageJson.keycloakify?.artifactId ?? `${themeNames[0]}-keycloak-theme`,
"doCreateJar": parsedPackageJson.keycloakify?.doCreateJar ?? true,
"loginThemeResourcesFromKeycloakVersion": parsedPackageJson.keycloakify?.loginThemeResourcesFromKeycloakVersion ?? "11.0.3",
reactAppRootDirPath,
2024-01-30 06:55:26 +01:00
reactAppBuildDirPath,
keycloakifyBuildDirPath,
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
})(),
2023-09-03 23:26:34 +02:00
"cacheDirPath": pathJoin(
(() => {
2024-01-30 06:55:26 +01:00
if (process.env.XDG_CACHE_HOME !== undefined) {
2023-09-03 23:26:34 +02:00
return getAbsoluteAndInOsFormatPath({
2024-01-30 06:55:26 +01:00
"pathIsh": process.env.XDG_CACHE_HOME,
2023-09-03 23:26:34 +02:00
"cwd": reactAppRootDirPath
});
}
return pathJoin(reactAppRootDirPath, "node_modules", ".cache");
})(),
"keycloakify"
),
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);
})(),
"doBuildRetrocompatAccountTheme": parsedPackageJson.keycloakify?.doBuildRetrocompatAccountTheme ?? true
2023-08-21 05:54:17 +02:00
};
2022-08-16 14:41:06 +07:00
}