2024-07-25 18:16:43 +02:00
|
|
|
import { join as pathJoin } from "path";
|
|
|
|
import { assert, type Equals } from "tsafe/assert";
|
|
|
|
import type { BuildContext } from "../shared/buildContext";
|
|
|
|
import * as fs from "fs";
|
|
|
|
import chalk from "chalk";
|
|
|
|
import { z } from "zod";
|
|
|
|
import { id } from "tsafe/id";
|
2024-10-11 23:55:04 +02:00
|
|
|
import { is } from "tsafe/is";
|
2024-07-25 18:16:43 +02:00
|
|
|
|
|
|
|
export type BuildContextLike = {
|
|
|
|
bundler: BuildContext["bundler"];
|
2024-10-06 12:44:46 +02:00
|
|
|
projectDirPath: string;
|
|
|
|
packageJsonFilePath: string;
|
2024-07-25 18:16:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
assert<BuildContext extends BuildContextLike ? true : false>();
|
|
|
|
|
|
|
|
export function updateAccountThemeImplementationInConfig(params: {
|
2024-10-06 12:44:46 +02:00
|
|
|
buildContext: BuildContextLike;
|
2024-07-25 18:16:43 +02:00
|
|
|
accountThemeType: "Single-Page" | "Multi-Page";
|
|
|
|
}) {
|
|
|
|
const { buildContext, accountThemeType } = params;
|
|
|
|
|
|
|
|
switch (buildContext.bundler) {
|
|
|
|
case "vite":
|
|
|
|
{
|
|
|
|
const viteConfigPath = pathJoin(
|
|
|
|
buildContext.projectDirPath,
|
|
|
|
"vite.config.ts"
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!fs.existsSync(viteConfigPath)) {
|
|
|
|
console.log(
|
|
|
|
chalk.bold(
|
|
|
|
`You must manually set the accountThemeImplementation to "${accountThemeType}" in your vite config`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
const viteConfigContent = fs
|
|
|
|
.readFileSync(viteConfigPath)
|
|
|
|
.toString("utf8");
|
|
|
|
|
|
|
|
const modifiedViteConfigContent = viteConfigContent.replace(
|
|
|
|
/accountThemeImplementation\s*:\s*"none"/,
|
|
|
|
`accountThemeImplementation: "${accountThemeType}"`
|
|
|
|
);
|
|
|
|
|
|
|
|
if (modifiedViteConfigContent === viteConfigContent) {
|
|
|
|
console.log(
|
|
|
|
chalk.bold(
|
|
|
|
`You must manually set the accountThemeImplementation to "${accountThemeType}" in your vite.config.ts`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.writeFileSync(viteConfigPath, modifiedViteConfigContent);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "webpack":
|
|
|
|
{
|
|
|
|
const parsedPackageJson = (() => {
|
|
|
|
type ParsedPackageJson = {
|
2024-08-28 14:43:25 +02:00
|
|
|
keycloakify: Record<string, unknown>;
|
2024-07-25 18:16:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const zParsedPackageJson = (() => {
|
|
|
|
type TargetType = ParsedPackageJson;
|
|
|
|
|
|
|
|
const zTargetType = z.object({
|
2024-08-28 14:43:25 +02:00
|
|
|
keycloakify: z.record(z.unknown())
|
2024-07-25 18:16:43 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
assert<Equals<z.infer<typeof zTargetType>, TargetType>>();
|
|
|
|
|
|
|
|
return id<z.ZodType<TargetType>>(zTargetType);
|
|
|
|
})();
|
|
|
|
|
2024-08-28 17:08:11 +02:00
|
|
|
const parsedPackageJson = JSON.parse(
|
|
|
|
fs.readFileSync(buildContext.packageJsonFilePath).toString("utf8")
|
2024-07-25 18:16:43 +02:00
|
|
|
);
|
2024-08-28 17:08:11 +02:00
|
|
|
|
|
|
|
zParsedPackageJson.parse(parsedPackageJson);
|
|
|
|
|
2024-10-11 23:55:04 +02:00
|
|
|
assert(is<ParsedPackageJson>(parsedPackageJson));
|
|
|
|
|
2024-08-28 17:08:11 +02:00
|
|
|
return parsedPackageJson;
|
2024-07-25 18:16:43 +02:00
|
|
|
})();
|
|
|
|
|
|
|
|
parsedPackageJson.keycloakify.accountThemeImplementation =
|
|
|
|
accountThemeType;
|
2024-08-28 17:08:11 +02:00
|
|
|
|
|
|
|
fs.writeFileSync(
|
|
|
|
buildContext.packageJsonFilePath,
|
|
|
|
Buffer.from(JSON.stringify(parsedPackageJson, undefined, 4), "utf8")
|
|
|
|
);
|
2024-07-25 18:16:43 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|