keycloak_theme/src/bin/postinstall/getUiModuleFileSourceCodeReadyToBeCopied.ts

124 lines
3.7 KiB
TypeScript
Raw Normal View History

2024-11-02 22:39:03 +01:00
import { getIsPrettierAvailable, runPrettier } from "../tools/runPrettier";
import * as fsPr from "fs/promises";
import { join as pathJoin, sep as pathSep } from "path";
import { assert } from "tsafe/assert";
import type { BuildContext } from "../shared/buildContext";
2024-11-09 14:02:19 +01:00
import { KEYCLOAK_THEME } from "../shared/constants";
2024-11-02 22:39:03 +01:00
export type BuildContextLike = {
themeSrcDirPath: string;
};
assert<BuildContext extends BuildContextLike ? true : false>();
2024-11-09 14:02:19 +01:00
export async function getUiModuleFileSourceCodeReadyToBeCopied(params: {
2024-11-02 22:39:03 +01:00
buildContext: BuildContextLike;
fileRelativePath: string;
isOwnershipAction: boolean;
2024-11-09 14:02:19 +01:00
uiModuleDirPath: string;
uiModuleName: string;
uiModuleVersion: string;
}): Promise<Buffer> {
const {
buildContext,
uiModuleDirPath,
fileRelativePath,
isOwnershipAction,
2024-11-09 14:02:19 +01:00
uiModuleName,
uiModuleVersion
} = params;
2024-11-02 22:39:03 +01:00
let sourceCode = (
2024-11-09 14:02:19 +01:00
await fsPr.readFile(pathJoin(uiModuleDirPath, KEYCLOAK_THEME, fileRelativePath))
2024-11-02 22:39:03 +01:00
).toString("utf8");
sourceCode = addCommentToSourceCode({
sourceCode,
fileRelativePath,
commentLines: isOwnershipAction
? [
`This file was claimed for ownership from ${uiModuleName} version ${uiModuleVersion}.`
]
: [
`WARNING: Before modifying this file run the following command:`,
``,
`$ npx keycloakify own --path '${fileRelativePath.split(pathSep).join("/")}'`,
``,
2024-11-18 03:19:13 +01:00
`This file comes from ${uiModuleName} version ${uiModuleVersion}.`,
`This file has been copied over to your repo by your postinstall script: \`npx keycloakify postinstall\``
]
});
2024-11-02 22:39:03 +01:00
2024-11-09 14:02:19 +01:00
const destFilePath = pathJoin(buildContext.themeSrcDirPath, fileRelativePath);
2024-11-02 22:39:03 +01:00
format: {
if (!(await getIsPrettierAvailable())) {
break format;
}
sourceCode = await runPrettier({
2024-11-09 14:02:19 +01:00
filePath: destFilePath,
2024-11-02 22:39:03 +01:00
sourceCode
});
}
2024-11-09 14:02:19 +01:00
return Buffer.from(sourceCode, "utf8");
2024-11-02 22:39:03 +01:00
}
function addCommentToSourceCode(params: {
sourceCode: string;
fileRelativePath: string;
commentLines: string[];
}): string {
const { sourceCode, fileRelativePath, commentLines } = params;
const toResult = (comment: string) => {
return [comment, ``, sourceCode].join("\n");
};
for (const ext of [".ts", ".tsx", ".css", ".less", ".sass", ".js", ".jsx"]) {
if (!fileRelativePath.endsWith(ext)) {
continue;
}
return toResult(
[`/**`, ...commentLines.map(line => ` * ${line}`), ` */`].join("\n")
);
}
if (fileRelativePath.endsWith(".properties")) {
return toResult(commentLines.map(line => `# ${line}`).join("\n"));
}
if (fileRelativePath.endsWith(".html") || fileRelativePath.endsWith(".svg")) {
const comment = [
`<!--`,
...commentLines.map(
line =>
` ${line.replace("--path", "-p").replace("Before modifying", "Before modifying or replacing")}`
),
`-->`
].join("\n");
if (fileRelativePath.endsWith(".html") && sourceCode.trim().startsWith("<!")) {
const [first, ...rest] = sourceCode.split(">");
const last = rest.join(">");
return [`${first}>`, comment, last].join("\n");
}
if (fileRelativePath.endsWith(".svg") && sourceCode.trim().startsWith("<?")) {
const [first, ...rest] = sourceCode.split("?>");
const last = rest.join("?>");
return [`${first}?>`, comment, last].join("\n");
}
return toResult(comment);
}
return sourceCode;
}