keycloak_theme/src/bin/keycloakify/generateJavaStackFiles.ts

85 lines
2.8 KiB
TypeScript
Raw Normal View History

2021-02-21 20:54:33 +01:00
import * as fs from "fs";
import { join as pathJoin, dirname as pathDirname } from "path";
2022-08-16 14:41:06 +07:00
import { assert } from "tsafe/assert";
import type { BuildOptions } from "./BuildOptions";
import type { ThemeType } from "./generateFtl";
2022-08-16 14:41:06 +07:00
export type BuildOptionsLike = {
themeName: string;
2023-06-08 23:09:14 +02:00
extraThemeNames: string[];
2022-08-16 14:41:06 +07:00
groupId: string;
2023-06-21 02:55:44 +02:00
artifactId: string;
2023-04-06 16:38:13 +02:00
themeVersion: string;
2022-08-16 14:41:06 +07:00
};
2023-08-21 05:54:17 +02:00
assert<BuildOptions extends BuildOptionsLike ? true : false>();
2021-02-21 20:54:33 +01:00
2022-04-20 00:39:40 +02:00
export function generateJavaStackFiles(params: {
keycloakThemeBuildingDirPath: string;
implementedThemeTypes: Record<ThemeType | "email", boolean>;
2022-08-16 14:41:06 +07:00
buildOptions: BuildOptionsLike;
2022-04-20 00:39:40 +02:00
}): {
jarFilePath: string;
} {
2022-08-16 14:41:06 +07:00
const {
2023-06-08 23:09:14 +02:00
buildOptions: { groupId, themeName, extraThemeNames, themeVersion, artifactId },
2022-08-16 14:41:06 +07:00
keycloakThemeBuildingDirPath,
implementedThemeTypes
2022-08-16 14:41:06 +07:00
} = params;
2021-02-21 20:54:33 +01:00
{
const { pomFileCode } = (function generatePomFileCode(): {
pomFileCode: string;
} {
2021-02-21 20:54:33 +01:00
const pomFileCode = [
`<?xml version="1.0"?>`,
`<project xmlns="http://maven.apache.org/POM/4.0.0"`,
` xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"`,
` xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">`,
` <modelVersion>4.0.0</modelVersion>`,
` <groupId>${groupId}</groupId>`,
` <artifactId>${artifactId}</artifactId>`,
2023-04-06 16:38:13 +02:00
` <version>${themeVersion}</version>`,
` <name>${artifactId}</name>`,
2021-02-21 20:54:33 +01:00
` <description />`,
`</project>`
2021-02-21 20:54:33 +01:00
].join("\n");
return { pomFileCode };
})();
fs.writeFileSync(pathJoin(keycloakThemeBuildingDirPath, "pom.xml"), Buffer.from(pomFileCode, "utf8"));
2021-02-21 20:54:33 +01:00
}
{
const themeManifestFilePath = pathJoin(keycloakThemeBuildingDirPath, "src", "main", "resources", "META-INF", "keycloak-themes.json");
2021-02-21 20:54:33 +01:00
try {
fs.mkdirSync(pathDirname(themeManifestFilePath));
} catch {}
2021-02-21 20:54:33 +01:00
fs.writeFileSync(
themeManifestFilePath,
Buffer.from(
JSON.stringify(
{
2023-06-10 10:39:47 +02:00
"themes": [themeName, ...extraThemeNames].map(themeName => ({
"name": themeName,
"types": Object.entries(implementedThemeTypes)
.filter(([, isImplemented]) => isImplemented)
.map(([themeType]) => themeType)
2023-06-10 10:39:47 +02:00
}))
},
null,
2
),
"utf8"
)
2021-02-21 20:54:33 +01:00
);
}
return {
2023-04-06 16:38:13 +02:00
"jarFilePath": pathJoin(keycloakThemeBuildingDirPath, "target", `${artifactId}-${themeVersion}.jar`)
};
2021-02-21 20:54:33 +01:00
}