Avoid loop rebuild in watch mode

This commit is contained in:
Joseph Garrone
2024-06-09 10:28:06 +02:00
parent 379301eb9d
commit 2015882688

@ -3,6 +3,7 @@ import type { BuildContext } from "./buildContext";
import { getThemeSrcDirPath } from "./getThemeSrcDirPath"; import { getThemeSrcDirPath } from "./getThemeSrcDirPath";
import * as fs from "fs/promises"; import * as fs from "fs/promises";
import { join as pathJoin } from "path"; import { join as pathJoin } from "path";
import { existsAsync } from "../tools/fs.existsAsync";
export type BuildContextLike = { export type BuildContextLike = {
projectDirPath: string; projectDirPath: string;
@ -21,41 +22,50 @@ export async function generateKcGenTs(params: {
projectDirPath: buildContext.projectDirPath projectDirPath: buildContext.projectDirPath
}); });
await fs.writeFile( const filePath = pathJoin(themeSrcDirPath, "kc.gen.ts");
pathJoin(themeSrcDirPath, "kc.gen.ts"),
Buffer.from( const currentContent = (await existsAsync(filePath))
[ ? await fs.readFile(filePath)
`/* prettier-ignore-start */`, : undefined;
``,
`/* eslint-disable */`, const newContent = Buffer.from(
``, [
`// @ts-nocheck`, `/* prettier-ignore-start */`,
``, ``,
`// noinspection JSUnusedGlobalSymbols`, `/* eslint-disable */`,
``, ``,
`// This file is auto-generated by Keycloakify`, `// @ts-nocheck`,
``, ``,
`export type ThemeName = ${buildContext.themeNames.map(themeName => `"${themeName}"`).join(" | ")};`, `// noinspection JSUnusedGlobalSymbols`,
``, ``,
`export const themeNames: ThemeName[] = [${buildContext.themeNames.map(themeName => `"${themeName}"`).join(", ")}];`, `// This file is auto-generated by Keycloakify`,
``, ``,
`export type KcEnvName = ${buildContext.environmentVariables.length === 0 ? "never" : buildContext.environmentVariables.map(({ name }) => `"${name}"`).join(" | ")};`, `export type ThemeName = ${buildContext.themeNames.map(themeName => `"${themeName}"`).join(" | ")};`,
``, ``,
`export const KcEnvNames: KcEnvName[] = [${buildContext.environmentVariables.map(({ name }) => `"${name}"`).join(", ")}];`, `export const themeNames: ThemeName[] = [${buildContext.themeNames.map(themeName => `"${themeName}"`).join(", ")}];`,
``, ``,
`export const kcEnvDefaults: Record<KcEnvName, string> = ${JSON.stringify( `export type KcEnvName = ${buildContext.environmentVariables.length === 0 ? "never" : buildContext.environmentVariables.map(({ name }) => `"${name}"`).join(" | ")};`,
Object.fromEntries( ``,
buildContext.environmentVariables.map( `export const KcEnvNames: KcEnvName[] = [${buildContext.environmentVariables.map(({ name }) => `"${name}"`).join(", ")}];`,
({ name, default: defaultValue }) => [name, defaultValue] ``,
) `export const kcEnvDefaults: Record<KcEnvName, string> = ${JSON.stringify(
), Object.fromEntries(
null, buildContext.environmentVariables.map(
2 ({ name, default: defaultValue }) => [name, defaultValue]
)};`, )
``, ),
`/* prettier-ignore-end */` null,
].join("\n"), 2
"utf8" )};`,
) ``,
`/* prettier-ignore-end */`
].join("\n"),
"utf8"
); );
if (currentContent !== undefined && currentContent.equals(newContent)) {
return;
}
await fs.writeFile(filePath, newContent);
} }