keycloak_theme/src/bin/shared/buildOptions.ts

311 lines
10 KiB
TypeScript
Raw Normal View History

2022-08-16 14:41:06 +07:00
import { parse as urlParse } from "url";
2023-09-03 23:26:34 +02:00
import { join as pathJoin } from "path";
2024-05-16 08:23:37 +02:00
import { getAbsoluteAndInOsFormatPath } from "../tools/getAbsoluteAndInOsFormatPath";
import { getNpmWorkspaceRootDirPath } from "../tools/getNpmWorkspaceRootDirPath";
import type { CliCommandOptions } from "../main";
import { z } from "zod";
import * as fs from "fs";
import { assert } from "tsafe";
import * as child_process from "child_process";
import { vitePluginSubScriptEnvNames } from "./constants";
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
themeVersion: string;
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;
// 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;
2024-06-06 01:31:00 +02:00
kcContextExclusionsFtlCode: string | undefined;
2023-08-21 05:54:17 +02:00
};
2022-08-16 14:41:06 +07:00
2024-05-16 08:23:37 +02:00
export type UserProvidedBuildOptions = {
extraThemeProperties?: string[];
artifactId?: string;
groupId?: string;
loginThemeResourcesFromKeycloakVersion?: string;
keycloakifyBuildDirPath?: string;
themeName?: string | string[];
2024-06-06 01:31:00 +02:00
kcContextExclusionsFtlCode?: string;
2024-05-16 08:23:37 +02:00
};
export type ResolvedViteConfig = {
buildDir: string;
publicDir: string;
assetsDir: string;
urlPathname: string | undefined;
userProvidedBuildOptions: UserProvidedBuildOptions;
};
2024-05-20 15:48:51 +02:00
export function readBuildOptions(params: {
cliCommandOptions: CliCommandOptions;
}): BuildOptions {
const { cliCommandOptions } = params;
const reactAppRootDirPath = (() => {
if (cliCommandOptions.reactAppRootDirPath === undefined) {
return process.cwd();
}
return getAbsoluteAndInOsFormatPath({
2024-05-20 15:48:51 +02:00
pathIsh: cliCommandOptions.reactAppRootDirPath,
cwd: process.cwd()
});
})();
2024-05-16 08:23:37 +02:00
const { resolvedViteConfig } = (() => {
2024-05-20 15:48:51 +02:00
if (
fs
.readdirSync(reactAppRootDirPath)
.find(fileBasename => fileBasename.startsWith("vite.config")) ===
undefined
) {
return { resolvedViteConfig: undefined };
2024-05-16 08:23:37 +02:00
}
const output = child_process
.execSync("npx vite", {
2024-05-20 15:48:51 +02:00
cwd: reactAppRootDirPath,
env: {
2024-05-16 08:23:37 +02:00
...process.env,
[vitePluginSubScriptEnvNames.resolveViteConfig]: "true"
}
})
.toString("utf8");
2024-05-20 15:48:51 +02:00
assert(
output.includes(vitePluginSubScriptEnvNames.resolveViteConfig),
"Seems like the Keycloakify's Vite plugin is not installed."
);
2024-05-16 08:23:37 +02:00
2024-05-20 15:48:51 +02:00
const resolvedViteConfigStr = output
.split(vitePluginSubScriptEnvNames.resolveViteConfig)
.reverse()[0];
2024-05-16 08:23:37 +02:00
const resolvedViteConfig: ResolvedViteConfig = JSON.parse(resolvedViteConfigStr);
return { resolvedViteConfig };
})();
const parsedPackageJson = (() => {
type ParsedPackageJson = {
name: string;
version?: string;
homepage?: string;
2024-05-20 15:48:51 +02:00
keycloakify?: UserProvidedBuildOptions & {
reactAppBuildDirPath?: string;
};
2024-05-16 08:23:37 +02:00
};
const zParsedPackageJson = z.object({
2024-05-20 15:48:51 +02:00
name: z.string(),
version: z.string().optional(),
homepage: z.string().optional(),
keycloakify: z
2024-05-16 08:23:37 +02:00
.object({
2024-05-20 15:48:51 +02:00
extraThemeProperties: z.array(z.string()).optional(),
artifactId: z.string().optional(),
groupId: z.string().optional(),
loginThemeResourcesFromKeycloakVersion: z.string().optional(),
reactAppBuildDirPath: z.string().optional(),
keycloakifyBuildDirPath: z.string().optional(),
themeName: z.union([z.string(), z.array(z.string())]).optional()
2024-05-16 08:23:37 +02:00
})
.optional()
});
{
type Got = ReturnType<(typeof zParsedPackageJson)["parse"]>;
type Expected = ParsedPackageJson;
assert<Got extends Expected ? true : false>();
assert<Expected extends Got ? true : false>();
}
2024-05-20 15:48:51 +02:00
return zParsedPackageJson.parse(
JSON.parse(
fs
.readFileSync(pathJoin(reactAppRootDirPath, "package.json"))
.toString("utf8")
)
);
2024-05-16 08:23:37 +02:00
})();
2024-05-16 08:23:37 +02:00
const userProvidedBuildOptions: UserProvidedBuildOptions = {
...parsedPackageJson.keycloakify,
...resolvedViteConfig?.userProvidedBuildOptions
};
2022-08-16 14:41:06 +07:00
const themeNames = (() => {
if (userProvidedBuildOptions.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
if (typeof userProvidedBuildOptions.themeName === "string") {
return [userProvidedBuildOptions.themeName];
}
return userProvidedBuildOptions.themeName;
})();
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-05-16 08:23:37 +02:00
if (parsedPackageJson.keycloakify?.reactAppBuildDirPath !== undefined) {
2024-01-30 06:55:26 +01:00
return getAbsoluteAndInOsFormatPath({
2024-05-20 15:48:51 +02:00
pathIsh: parsedPackageJson.keycloakify.reactAppBuildDirPath,
cwd: reactAppRootDirPath
2024-01-30 06:55:26 +01:00
});
}
return pathJoin(reactAppRootDirPath, "build");
}
return pathJoin(reactAppRootDirPath, resolvedViteConfig.buildDir);
})();
2024-01-30 05:54:36 +01:00
const { npmWorkspaceRootDirPath } = getNpmWorkspaceRootDirPath({
reactAppRootDirPath,
2024-05-20 15:48:51 +02:00
dependencyExpected: "keycloakify"
});
2024-02-11 18:28:58 +01:00
2024-01-30 05:54:36 +01:00
return {
2024-05-20 15:48:51 +02:00
bundler: resolvedViteConfig !== undefined ? "vite" : "webpack",
themeVersion:
process.env.KEYCLOAKIFY_THEME_VERSION ?? parsedPackageJson.version ?? "0.0.0",
themeNames,
2024-05-20 15:48:51 +02:00
extraThemeProperties: userProvidedBuildOptions.extraThemeProperties,
groupId: (() => {
const fallbackGroupId = `${themeNames[0]}.keycloak`;
2023-08-21 05:54:17 +02:00
return (
process.env.KEYCLOAKIFY_GROUP_ID ??
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-05-20 15:48:51 +02:00
artifactId:
process.env.KEYCLOAKIFY_ARTIFACT_ID ??
userProvidedBuildOptions.artifactId ??
`${themeNames[0]}-keycloak-theme`,
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-05-20 15:48:51 +02:00
keycloakifyBuildDirPath: (() => {
if (userProvidedBuildOptions.keycloakifyBuildDirPath !== undefined) {
return getAbsoluteAndInOsFormatPath({
2024-05-20 15:48:51 +02:00
pathIsh: userProvidedBuildOptions.keycloakifyBuildDirPath,
cwd: reactAppRootDirPath
});
}
return pathJoin(
reactAppRootDirPath,
2024-05-20 15:48:51 +02:00
resolvedViteConfig?.buildDir === undefined
? "build_keycloak"
: `${resolvedViteConfig.buildDir}_keycloak`
);
})(),
2024-05-20 15:48:51 +02: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({
2024-05-20 15:48:51 +02:00
pathIsh: process.env.PUBLIC_DIR_PATH,
cwd: reactAppRootDirPath
2024-01-30 06:55:26 +01:00
});
}
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-05-20 15:48:51 +02:00
cacheDirPath: (() => {
const cacheDirPath = pathJoin(
(() => {
if (process.env.XDG_CACHE_HOME !== undefined) {
return getAbsoluteAndInOsFormatPath({
2024-05-20 15:48:51 +02:00
pathIsh: process.env.XDG_CACHE_HOME,
cwd: process.cwd()
});
}
return pathJoin(npmWorkspaceRootDirPath, "node_modules", ".cache");
})(),
"keycloakify"
);
return cacheDirPath;
})(),
2024-05-20 15:48:51 +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;
})(),
2024-05-20 15:48:51 +02:00
assetsDirPath: (() => {
2024-01-30 06:55:26 +01:00
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-06-06 01:31:00 +02:00
npmWorkspaceRootDirPath,
kcContextExclusionsFtlCode: userProvidedBuildOptions.kcContextExclusionsFtlCode
2023-08-21 05:54:17 +02:00
};
2022-08-16 14:41:06 +07:00
}