import { assert } from "tsafe/assert"; import type { BuildOptions } from "./buildOptions"; import { getThemeSrcDirPath } from "./getThemeSrcDirPath"; import * as fs from "fs/promises"; import { join as pathJoin } from "path"; export type BuildOptionsLike = { projectDirPath: string; themeNames: string[]; environmentVariables: { name: string; default: string }[]; }; assert(); export async function generateKcGenTs(params: { buildOptions: BuildOptionsLike; }): Promise { const { buildOptions } = params; const { themeSrcDirPath } = getThemeSrcDirPath({ projectDirPath: buildOptions.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`, ``, `export type ThemeName = ${buildOptions.themeNames.map(themeName => `"${themeName}"`).join(" | ")};`, ``, `export const themeNames: ThemeName[] = [${buildOptions.themeNames.map(themeName => `"${themeName}"`).join(", ")}];`, ``, `export type KcEnvName = ${buildOptions.environmentVariables.length === 0 ? "never" : buildOptions.environmentVariables.map(({ name }) => `"${name}"`).join(" | ")};`, ``, `export const KcEnvNames: KcEnvName[] = [${buildOptions.environmentVariables.map(({ name }) => `"${name}"`).join(", ")}];`, ``, `export const kcEnvDefaults: Record = ${JSON.stringify( Object.fromEntries( buildOptions.environmentVariables.map( ({ name, default: defaultValue }) => [name, defaultValue] ) ), null, 2 )};`, ``, `/* prettier-ignore-end */` ].join("\n"), "utf8" ) ); }