keycloak_theme/src/bin/shared/generateKcGenTs.ts

62 lines
2.3 KiB
TypeScript
Raw Normal View History

import { assert } from "tsafe/assert";
2024-06-09 09:15:16 +02:00
import type { BuildContext } from "./buildContext";
import { getThemeSrcDirPath } from "./getThemeSrcDirPath";
import * as fs from "fs/promises";
import { join as pathJoin } from "path";
2024-06-09 09:15:16 +02:00
export type BuildContextLike = {
projectDirPath: string;
themeNames: string[];
environmentVariables: { name: string; default: string }[];
};
2024-06-09 09:15:16 +02:00
assert<BuildContext extends BuildContextLike ? true : false>();
export async function generateKcGenTs(params: {
2024-06-09 09:15:16 +02:00
buildContext: BuildContextLike;
}): Promise<void> {
2024-06-09 09:15:16 +02:00
const { buildContext } = params;
const { themeSrcDirPath } = getThemeSrcDirPath({
2024-06-09 09:15:16 +02:00
projectDirPath: buildContext.projectDirPath
});
await fs.writeFile(
pathJoin(themeSrcDirPath, "kc.gen.ts"),
Buffer.from(
[
`/* prettier-ignore-start */`,
``,
`/* eslint-disable */`,
``,
`// @ts-nocheck`,
``,
`// noinspection JSUnusedGlobalSymbols`,
``,
`// This file is auto-generated by Keycloakify`,
``,
2024-06-09 09:15:16 +02:00
`export type ThemeName = ${buildContext.themeNames.map(themeName => `"${themeName}"`).join(" | ")};`,
``,
2024-06-09 09:15:16 +02:00
`export const themeNames: ThemeName[] = [${buildContext.themeNames.map(themeName => `"${themeName}"`).join(", ")}];`,
``,
2024-06-09 09:15:16 +02:00
`export type KcEnvName = ${buildContext.environmentVariables.length === 0 ? "never" : buildContext.environmentVariables.map(({ name }) => `"${name}"`).join(" | ")};`,
``,
2024-06-09 09:15:16 +02:00
`export const KcEnvNames: KcEnvName[] = [${buildContext.environmentVariables.map(({ name }) => `"${name}"`).join(", ")}];`,
``,
`export const kcEnvDefaults: Record<KcEnvName, string> = ${JSON.stringify(
Object.fromEntries(
2024-06-09 09:15:16 +02:00
buildContext.environmentVariables.map(
({ name, default: defaultValue }) => [name, defaultValue]
)
),
null,
2
2024-06-08 14:20:56 +02:00
)};`,
``,
`/* prettier-ignore-end */`
].join("\n"),
"utf8"
)
);
}