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;
|
2024-12-23 18:34:03 +01:00
|
|
|
isOwnershipAction: boolean;
|
2024-11-09 14:02:19 +01:00
|
|
|
uiModuleDirPath: string;
|
|
|
|
uiModuleName: string;
|
|
|
|
uiModuleVersion: string;
|
|
|
|
}): Promise<Buffer> {
|
|
|
|
const {
|
|
|
|
buildContext,
|
|
|
|
uiModuleDirPath,
|
|
|
|
fileRelativePath,
|
2024-12-23 18:34:03 +01:00
|
|
|
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");
|
|
|
|
|
2024-12-21 14:25:47 +01:00
|
|
|
sourceCode = addCommentToSourceCode({
|
|
|
|
sourceCode,
|
|
|
|
fileRelativePath,
|
2024-12-23 18:34:03 +01:00
|
|
|
commentLines: isOwnershipAction
|
|
|
|
? [
|
|
|
|
`This file was claimed for ownership from ${uiModuleName} version ${uiModuleVersion}.`
|
|
|
|
]
|
2024-11-17 23:50:49 +01:00
|
|
|
: [
|
|
|
|
`WARNING: Before modifying this file run the following command:`,
|
|
|
|
``,
|
2024-12-23 17:55:40 +01:00
|
|
|
`$ npx keycloakify own --path '${fileRelativePath.split(pathSep).join("/")}'`,
|
2024-11-17 23:50:49 +01:00
|
|
|
``,
|
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-17 23:50:49 +01:00
|
|
|
]
|
2024-12-21 14:25:47 +01:00
|
|
|
});
|
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
|
|
|
}
|
2024-12-21 14:25:47 +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 =>
|
2024-12-23 17:55:40 +01:00
|
|
|
` ${line.replace("--path", "-p").replace("Before modifying", "Before modifying or replacing")}`
|
2024-12-21 14:25:47 +01:00
|
|
|
),
|
|
|
|
`-->`
|
|
|
|
].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;
|
|
|
|
}
|